环境:myeclipse 14
1 struts1 框架搭建
在myeclipse新建web project 取名为struts1_login,此时是一个空文档就不截图了然后在project上右键->选择myeclipse->add struts capabilities
单击上面install apache struts(1.x)facet
点击next
选择*.do ,改下包名改成与你项目相关的。如我的包名为com.lichang.struts1
点击next
点击完成,在我们的web-inf下就会多出struts-config.xml文件
以上就是让myeclipse帮我们加入框架的大概过程。项目的整体结构如下:
至此我们的struts1 框架搭建完成2 接着我们就开始编程来实现了。
2 接着我们就开始编程来实现了。
web.xml 如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
<?xml version= "1.0" encoding= "utf-8" ?>
<web-app xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xmlns= "http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id= "webapp_id" version= "3.0" >
<display-name>struts1_login</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file> default .html</welcome-file>
<welcome-file> default .htm</welcome-file>
<welcome-file> default .jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>action</servlet-name>
<servlet- class >org.apache.struts.action.actionservlet</servlet- class >
<init-param>
<param-name>config</param-name>
<param-value>/web-inf/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value> 3 </param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value> 3 </param-value>
</init-param>
<load-on-startup> 0 </load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*. do </url-pattern>
</servlet-mapping>
</web-app>
|
然后在struts.xml配置loginaction 代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<?xml version= "1.0" encoding= "utf-8" ?>
<!doctype struts-config public "-//apache software foundation//dtd struts configuration 1.3//en" "http://struts.apache.org/dtds/struts-config_1_3.dtd" >
<struts-config>
<form-beans>
<form-bean name= "loginform" type= "com.lichang.struts1.loginactionform" />
</form-beans>
<action-mappings>
<!-- path:指定访问时的路径 type:指定action所在的类(一般是:包名.类名) name:指定和哪个表单(和jsp中javabean
差不多的东西)对应,该例中name就和com.lichang.struts1.loginactionform类对应-->
<action path= "/login"
type= "com.lichang.struts1.loginaction"
name= "loginform" scope= "request" >
<forward name= "success" path= "/login_success.jsp" />
<forward name= "error" path= "/login_error.jsp" />
</action>
</action-mappings>
<message-resources parameter= "com.lichang.struts1.applicationresources" />
</struts-config>
|
index.jsp代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<%@ page language= "java" import = "java.util.*" pageencoding= "utf-8" %>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" >
<html style= "text-align:center" >
<head style= "text-align:center" >
<title>index page</title>
<meta http-equiv= "pragma" content= "no-cache" >
<meta http-equiv= "cache-control" content= "no-cache" >
<meta http-equiv= "expires" content= "0" >
<!--
<link rel= "stylesheet" type= "text/css" href= "styles.css" rel= "external nofollow" rel= "external nofollow" rel= "external nofollow" >
-->
</head>
<body style= "text-align:center" >
<form action= "login.do" method= "post" >
用户:<input type= "text" name= "username" ><br>
密码:<input type= "password" name= "password" ></br>
</form>
</body>
</html>
|
login_error.jsp代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<%@ page language= "java" import = "java.util.*" pageencoding= "utf-8" %>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" >
<html>
<head>
<title>error page</title>
<meta http-equiv= "pragma" content= "no-cache" >
<meta http-equiv= "cache-control" content= "no-cache" >
<meta http-equiv= "expires" content= "0" >
<!--
<link rel= "stylesheet" type= "text/css" href= "styles.css" rel= "external nofollow" rel= "external nofollow" rel= "external nofollow" >
-->
</head>
<body>
<%--
<%=request.getattribute( "msg" ) %>
--%>
${msg }
</body>
</html>
|
login_success.jsp代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<%@ page language= "java" import = "java.util.*" pageencoding= "utf-8" %>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" >
<html>
<head>
<title>success page</title>
<meta http-equiv= "pragma" content= "no-cache" >
<meta http-equiv= "cache-control" content= "no-cache" >
<meta http-equiv= "expires" content= "0" >
<!--
<link rel= "stylesheet" type= "text/css" href= "styles.css" rel= "external nofollow" rel= "external nofollow" rel= "external nofollow" >
-->
</head>
<body>
${username },登录成功
</body>
</html>
|
loginaction.java代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
package com.lichang.struts1;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import org.apache.struts.action.action;
import org.apache.struts.action.actionform;
import org.apache.struts.action.actionforward;
import org.apache.struts.action.actionmapping;
//这个类充当控制器
public class loginaction extends action {
public actionforward execute(actionmapping mapping, actionform form,
httpservletrequest request, httpservletresponse response)
throws exception {
//从actionform中获取username和password
loginactionform laf = (loginactionform)form;
string username = laf.getusername();
string password = laf.getpassword();
//调用业务逻辑类
usermanager usermanager = new usermanager();
try {
usermanager.login(username, password);
return mapping.findforward( "success" );
} catch (usernotfoundexception e) {
e.printstacktrace();
request.setattribute( "msg" , "用户不能找到,用户名称=" + username );
} catch (passworderrorexception e) {
e.printstacktrace();
request.setattribute( "msg" , "密码错误" );
}
return mapping.findforward( "error" );
}
}
|
loginactionform.java代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
package com.lichang.struts1;
import org.apache.struts.action.actionform;
/**
*
* actionform(表单用于获取用户输入的数据,相当于jsp中的javabean)
* 不过sturts1在底层上实现了一些特别的方法以至于当java程序员定义了javabean并继承actionform并实现setxxx()方法时
* 用户表单中元素的值就被一一赋给我们自己定义的变量。如public void setusername(string username)方法就可把form中username
* 赋给username变量
*
*/
public class loginactionform extends actionform {
private string username;
private string password;
public string getusername() {
return username;
}
public void setusername(string username) {
this .username = username;
}
public string getpassword() {
return password;
}
public void setpassword(string password) {
this .password = password;
}
}
|
usermanager.java代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package com.lichang.struts1;
//这个类就是用来处理用户登录的业务逻辑
public class usermanager {
public void login(string username, string password) {
if (! "admin" .equals(username)) {
throw new usernotfoundexception();
}
if (! "admin" .equals(password)) {
throw new passworderrorexception();
}
}
}
|
usernotfoundexception.java代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package com.lichang.struts1;
public class usernotfoundexception extends runtimeexception {
public usernotfoundexception() {
// todo auto-generated constructor stub
}
public usernotfoundexception(string message) {
super (message);
// todo auto-generated constructor stub
}
public usernotfoundexception(throwable cause) {
super (cause);
// todo auto-generated constructor stub
}
public usernotfoundexception(string message, throwable cause) {
super (message, cause);
// todo auto-generated constructor stub
}
}
|
passworderrorexception.java代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package com.lichang.struts1;
public class passworderrorexception extends runtimeexception {
public passworderrorexception() {
// todo auto-generated constructor stub
}
public passworderrorexception(string message) {
super (message);
// todo auto-generated constructor stub
}
public passworderrorexception(throwable cause) {
super (cause);
// todo auto-generated constructor stub
}
public passworderrorexception(string message, throwable cause) {
super (message, cause);
// todo auto-generated constructor stub
}
}
|
运行部分截图
登录界面
用户名不存在
源代码下载地址:struts1_login.rar
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。