Posted on 2006-12-28 19:21
路易 閱讀(569)
評論(0) 編輯 收藏 所屬分類:
STRUTS
??1
此struts的配置文件的編寫
??2
struts
-
config.xml
??3
??4
<?
xml?version
=
"
1.0
"
?encoding
=
"
UTF-8
"
?>
??5
<!
DOCTYPE?struts
-
config?PUBLIC?
"
-//Apache?Software?Foundation//DTD?Struts?Configuration?1.2//EN
"
?
"
http://struts.apache.org/dtds/struts-config_1_2.dtd
"
>
??6
??7
<
struts
-
config
>
??8
?
<
data
-
sources
>
??9
????????
<
data
-
source?key
=
"
oracleDB1
"
?type
=
"
org.apache.commons.dbcp.BasicDataSource
"
>
?10
????????????
<
set
-
property?property
=
"
driverClassName
"
????
?11
????????????????value
=
"
oracle.jdbc.driver.OracleDriver
"
?
/>
?12
????????????
<
set
-
property?property
=
"
url
"
?13
????????????????value
=
"
jdbc:oracle:thin:@localhost:1521:你的SID
"
?
/>
?14
????????????
<
set
-
property?property
=
"
maxActive
"
?15
????????????????value
=
"
5
"
/>
?16
????????????
<
set
-
property?property
=
"
username
"
?17
????????????????value
=
"
數據庫用戶名
"
/>
?18
????????????
<
set
-
property?property
=
"
password
"
?19
????????????????value
=
"
數據庫密碼
"
/>
?20
????????????
<
set
-
property?property
=
"
autoCommit
"
?21
????????????????value
=
"
true
"
/>
?22
????????
</
data
-
source
>
?23
????
</
data
-
sources
>
?24
????
?25
?26
??
<
form
-
beans?
>
?27
????
<
form
-
bean?name
=
"
userForm
"
?type
=
"
classmate.UserForm
"
?
/>
?28
??
</
form
-
beans
>
?29
?30
??
<
global
-
exceptions?
/>
?31
??
<
global
-
forwards?
>
?32
??????????
<
forward?name
=
"
failed
"
?path
=
"
/error.jsp
"
/>
?33
????????
<
forward?name
=
"
successed
"
?path
=
"
/right.jsp
"
/>
?34
??
</
global
-
forwards
>
?35
?36
??
<
action
-
mappings?
>
?37
??
<
action?path
=
"
/login
"
?type
=
"
classmate.LoginAction
"
?name
=
"
userForm
"
?scope
=
"
request
"
?validate
=
"
true
"
????input
=
"
/error.jsp
"
?
/>
?38
??
<
action?path
=
"
/regist
"
?forward
=
"
/regist.jsp
"
/>
?39
??
</
action
-
mappings
>
?40
?41
??
<
controller?processorClass
=
"
classmate.MyRequestProcessor
"
?
/>
?42
??
<
message
-
resources?parameter
=
"
classmate.MyResource
"
/>
????
?43
??
</
struts
-
config
>
?44
?45
在添加MyResource_zh.properties?
/
MyResource_zh.properties文件?其實標簽用。我不太愛用標簽?因為?有的是時候有問題?也不知道什么地方錯了!
?46
?47
在用標簽的時候還要在web.xml中寫出標簽文件struts
-
bean.tld
/
?struts
-
html.tld的路徑。下面我會給出web.xml的代碼。
?48
?49
MyResource_zh.properties
?50
?51
MyResource.properties
?52
title.login?
=
?登錄界面
?53
title.welcome?
=
?歡迎,
?54
title.failure?
=
?抱歉,登錄失敗
!
?55
label.login?
=
?請輸入用戶名和密碼
?56
label.deny?
=
?您無權訪問本頁面
!
?57
item.submit?
=
?提交
?58
item.reset?
=
?重置
?59
item.user?
=
?用戶
?60
item.password?
=
?密碼
?61
link.relative?
=
?友情鏈接
?62
link.loginAgain?
=
?重新登錄
?63
?64
error.name.required?
=
?用戶名不能為空
?65
error.psw.required?
=
?密碼不能為空
?66
?67
?68
web.xml
?69
<?
xml?version
=
"
1.0
"
?encoding
=
"
ISO-8859-1
"
?>
?70
?71
<!
DOCTYPE?web
-
app
?72
??PUBLIC?
"
-//Sun?Microsystems,?Inc.//DTD?Web?Application?2.3//EN
"
?73
??
"
http://java.sun.com/dtd/web-app_2_3.dtd
"
>
?74
?75
<
web
-
app
>
?76
?77
??
<!--
?Action?Servlet?Configuration?
-->
?78
??
<
servlet
>
?79
????
<
servlet
-
name
>
actionServlet
</
servlet
-
name
>
?80
????
<
servlet
-
class
>
org.apache.struts.action.ActionServlet
</
servlet
-
class
>
?81
??
</
servlet
>
?82
?83
??
<!--
?Action?Servlet?Mapping?
-->
?84
??
<
servlet
-
mapping
>
?85
????
<
servlet
-
name
>
actionServlet
</
servlet
-
name
>
?86
????
<
url
-
pattern
>*
.
do
</
url
-
pattern
>
?87
??
</
servlet
-
mapping
>
?88
?89
??
<!--
?The?Welcome?File?List?
-->
?90
??
<
welcome
-
file
-
list
>
?91
????
<
welcome
-
file
>
login.jsp
</
welcome
-
file
>
?92
??
</
welcome
-
file
-
list
>
?93
??
?94
<!--
?Struts?Tag?Library?Descriptors?
-->
?95
??
<
taglib
>
?96
????
<
taglib
-
uri
>/
WEB
-
INF
/
struts
-
bean.tld
</
taglib
-
uri
>
?97
????
<
taglib
-
location
>/
WEB
-
INF
/
struts
-
bean.tld
</
taglib
-
location
>
?98
??
</
taglib
>
??
?99
</
web
-
app
>
100
101
到這就一切都搞定了?至于?struts
-
config.xml?配置文件和其他?的對應關系自己在細看看就可以了。
工具:??MyEclipse?Eclipse
數據庫:Oracle
服務器:Tomcat
其他包:???commons-pool-1.3
???commons-dbcp-1.2.1
???jdbc2_0-stdext.jar
?classes12.jar
1.????首先建立數據庫
Create?TABLE?classuser(????
????username????VARCHAR2?(20)?PRIMARY?KEY,
????password????VARCHAR2?(20)?
);
2.??新建立一個項目
MyEclipse
??????????---J2EE?PROJECT
????????????????????????????---WEB?PROJECT
這里給項目起名為:validate_login
然后給項目添加:struts
????????????????????????tomcat
接著把其他包也都添加到項目中
3.????始寫struts?代碼
?
建立一個?Form,Action?&?Jsp
分別建立java文件?:
LoginAction.java
??????????????????UserForm.java
??????????????????DBUser.java
Jsp文件:
Login.jsp
Right.jsp
Error.jsp
?1
LoginAction.java
?2
?3
import?org.apache.struts.action.Action;
?4
import?org.apache.struts.action.ActionForm;
?5
import?org.apache.struts.action.ActionForward;
?6
import?org.apache.struts.action.ActionMapping;
?7
?8
import?org.apache.struts.action.ActionMessages;
?9
import?org.apache.struts.action.ActionMessage;
10
11
import?javax.servlet.ServletContext;
12
import?javax.sql.DataSource;
13
import?javax.servlet.http.*;
14
15
public?final?class?LoginAction?extends?Action
{??
16
????public?ActionForward?execute(
17
????????ActionMapping?mapping,
18
????????ActionForm?form,
19
????????HttpServletRequest?request,??
20
????????HttpServletResponse?response)?throws?Exception?
{
21
????????
22
????
23
???????????UserForm?userform?=?(UserForm)?form;?????????
24
????????String?name?=?userform.getName();
25
????????String?psw?=?userform.getPsw();
26
????????
27
28
???????????ServletContext?context?=?servlet.getServletContext();
29
DataSource
30
dataSource=(DataSource)context.getAttribute("oracleDB1");
31
????????????????????????
32
????????DBUser?dbuser?=?new?DBUser(dataSource);
33
????????HttpSession?session?=?request.getSession();
34
????????
35
????????if?(!dbuser.checkUser(name,psw))?
{
36
????????????ActionMessages?errors?=?new?ActionMessages();
37
????????????errors.add(ActionMessages.GLOBAL_MESSAGE,
38
????????????????new?ActionMessage("label.deny"));
39
????????????????
40
????????????if?(!errors.isEmpty())?
{
41
????????????????saveErrors(request,?errors);
42
????????????}?
43
??????????????return??mapping.findForward("failed");??//登陸失敗
44
????????}
45
????????else
{
46
????????????????return?(mapping.findForward("successed"));//登陸成功????????????????????
47
????????}????
48
????}
49
}
50
?1
UserForm.java
?2
?3
import?org.apache.struts.action.ActionForm;
?4
import?org.apache.struts.action.ActionMapping;
?5
import?javax.servlet.http.HttpServletRequest;
?6
import?org.apache.struts.action.ActionErrors;
?7
import?org.apache.struts.action.ActionMessage;
?8
?9
public?class?UserForm?extends?ActionForm?
{
10
11
12
????private?String?psw;
13
14
????private?String?name;
15
16
17
18
????public?ActionErrors?validate(ActionMapping?mapping,
19
????????????HttpServletRequest?request)?
{
20
????????????ActionErrors?errors?=?new?ActionErrors();
21
????????????if?((name?==?null)?||?(name.equals("")))
{
22
????????????????errors.add(ActionErrors.GLOBAL_MESSAGE,
23
????????????????????new?ActionMessage("error.name.required"));
24
????????????}
25
????????????if((psw?==?null)?||?(psw.equals("")))
{
26
????????????????errors.add(ActionErrors.GLOBAL_MESSAGE,new?ActionMessage("error.psw.required"));
27
????????????}
28
????????????return?errors;
29
????????}
30
31
????
32
?????*?@param?mapping
33
????
34
????public?void?reset(ActionMapping?mapping,?HttpServletRequest?request)?
{
35
36
????????????}
37
38
????
39
????public?String?getPsw()?
{
40
????????return?psw;
41
????}
42
43
????
44
????public?void?setPsw(String?psw)?
{
45
????????this.psw?=?psw;
46
????}
47
????public?String?getName()?
{
48
????????return?name;
49
????}
50
51
????
52
????public?void?setName(String?name)?
{
53
????????this.name?=?name;
54
????}
55
56
}
57
?1
DBUser.java
?2
?3
import?javax.sql.DataSource;
?4
import?java.sql.Connection;
?5
import?java.sql.Statement;
?6
import?java.sql.ResultSet;
?7
import?java.sql.SQLException;
?8
?9
public?class?DBUser?
{
10
11
????DataSource?dataSource;
12
????
13
????public?DBUser(DataSource?dataSource)?
{
14
????????
15
????????????this.dataSource?=?dataSource;
16
????}
17
????
18
????
19
????public?boolean?checkUser(String?name,String?psw)?throws?Exception
{
20
????????Connection?connect?=?null;
21
????????String?strSql;
22
????????ResultSet?rs;
23
????????boolean?result=false;
24
????????strSql?=?"select?*?from?classuser?where?username='"
25
????????????????????+?name?+?"'?and?password='"?+?psw?+?"'";
26
????????try?
{
27
????????????connect?=?dataSource.getConnection();
28
????????????Statement?stmt?=?connect.createStatement();
29
????????????rs?=?stmt.executeQuery(strSql);
30
????????????if?(?rs.next())?
{
31
????????????????result=true;
32
????????????}
33
????????}?
34
????????catch(SQLException?ex)?
{?
35
????????????ex.printStackTrace();
36
????????}
37
????????finally
{
38
????????????if(connect!=null)
39
????????????????connect.close();
40
????????}
41
????????return?result;
42
????
43
????}
44
????
45
}
46
?1
Login.jsp
?2
?3
<%@?page?contentType="text/html;charset=GBK"?language="java"?%>
?4
<body?vLink="#006666"?link="#003366"?bgColor="#E0F0F8">
?5
<img?height="33"?src="enter.gif"?width="148">
?6
<form?action="login.do"?method="post">
?7
用戶名:?<input?size="15"?name="name"><p>
?8
密??碼:?<input?type="password"?size="15"?name="psw"><p>
?9
<a?href="regist.do">新用戶注冊</A>|
10
<input?type="submit"?value="登錄">
11
</form>
12
1
Error.jsp
2
3
<%@?page?contentType="text/html;charset=GBK"?language="java"?%>?????
4
<h1><p><img?src="cry.gif">對不起,登錄失敗!
5
</p></h1>
6
<a?href="login.jsp">重新登錄</a>||
7
<a?href="regist.do">新用戶注冊</a>
8
?1
Right.jsp
?2
?3
?4
<%@?page?contentType="text/html;charset=GBK"?language="java"?%>
?5
<%@?page?import?=?"classmate.*"?%>????
?6
<%
?7
????UserForm?formBean1?=?(UserForm)request.getAttribute("userForm");
?8
????if(formBean1?!=?null?&&?formBean1.getName()!=null)
{
?9
%>
10
<img?src="smile.gif">
11
熱烈的歡迎您,
12
<%=formBean1.getName()%>?用戶!
13
<%
14
????}else
{
15
%>
16
您無權訪問本頁面!
17
<%}
18
%>
19
<br><br><br>
20
<a?href="login.jsp">重新登錄</a>||
21
<a?href="regist.do">新用戶注冊</a>
22