Posted on 2006-12-28 19:21
路易 阅读(566)
评论(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
1LoginAction.java
2
3import org.apache.struts.action.Action;
4import org.apache.struts.action.ActionForm;
5import org.apache.struts.action.ActionForward;
6import org.apache.struts.action.ActionMapping;
7
8import org.apache.struts.action.ActionMessages;
9import org.apache.struts.action.ActionMessage;
10
11import javax.servlet.ServletContext;
12import javax.sql.DataSource;
13import javax.servlet.http.*;
14
15public 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();
29DataSource
30dataSource=(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 1UserForm.java
2
3import org.apache.struts.action.ActionForm;
4import org.apache.struts.action.ActionMapping;
5import javax.servlet.http.HttpServletRequest;
6import org.apache.struts.action.ActionErrors;
7import org.apache.struts.action.ActionMessage;
8
9public 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 1DBUser.java
2
3import javax.sql.DataSource;
4import java.sql.Connection;
5import java.sql.Statement;
6import java.sql.ResultSet;
7import java.sql.SQLException;
8
9public 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 1Login.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 1Error.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 1Right.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