下载需要文档
创建web工程
创建【Dynamic Web project】工程,命名
Struts2.5
引入相关jar包
解压下载的【struts-2.5.16-min-lib.zip】,里面只有一个lib的文件夹。将lib文件夹里的jar包复制到工程WebContent\WEB-INF\lib下,并选中刚刚复制过来的jar包,右键-》Build Path-》Add to Build Path
修改或创建web.xml
文件路径:WebContent\WEB-INF。
<?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>
Struts2.5
</display-name>
<!-- 欢迎页 -->
<welcome-file-list>
<welcome-file>
login.jsp
</welcome-file>
</welcome-file-list>
<!-- 设置过滤器,核心控制器 -->
<filter>
<filter-name>
struts2
</filter-name>
<filter-class>
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>
struts2
</filter-name>
<url-pattern>
/*
</url-pattern>
</filter-mapping>
</web-app>
|
编写Action类
Struts2的核心功能是action类,action类是一段特定的URL请求是执行的代码,过滤器根据请求的URL不同,执行相应的Action类,action类执行的结果一般对应一个result展现给用户。result通过字符串名字来标识,过滤器根据action返回的结果字符串,选中对应的result展示给用户,action与其对应的result在struts.xml文件中进行配置。
一般Action类会继承com.opensymphony.xwork2.ActionSupport类,或者实现com.opensymphony.xwork2.Action接口的execute方法。
首先在src下穿件包com.rl.action。然后创建类LoginAction。
package
com.rl.action;
import
com.opensymphony.xwork2.ActionContext;
import
com.opensymphony.xwork2.ActionSupport;
public class
LoginAction extends ActionSupport {
/**
* serialVersionUID
*/
private static final long
serialVersionUID
= 1L;
// 定义封装请求参数的username和password属性
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
;
}
// 定义处理用户请求的execute方法
public
String execute()
throws
Exception {
if
(
"super"
.equals(getUsername()) &&
"123456"
.equals(getPassword())) {
// 在session中保存user对象
ActionContext.getContext().getSession().put(
"user"
, getUsername());
return
SUCCESS
;
}
else
{
return
ERROR
;
}
}
}
|
创建页面视图
login.jsp登录界面
<%@
page
language
=
"java"
contentType
=
"text/html; charset=UTF-8"
pageEncoding
=
"UTF-8"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=UTF-8"
>
<title>
登录页面
</title>
</head>
<body>
<!-- action的属性值对应struts.xml中action的name值 -->
<form
action
=
"login"
method
=
"post"
>
<p>
用户登录:
</p>
<p>
用户名:
<input
type
=
"text"
name
=
"username"
>
</p>
<p>
密 码:
<input
type
=
"password"
name
=
"password"
>
</p>
<p>
<input
type
=
"submit"
value
=
"登录"
>
</p>
</form>
</body>
</html>
|
welcome.jsp欢迎界面/登录成功界面
<%@
page
language
=
"java"
contentType
=
"text/html; charset=UTF-8"
pageEncoding
=
"UTF-8"
%>
<!-- struts的标签库 -->
<%@
taglib
prefix
=
"s"
uri
=
"/struts-tags"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=UTF-8"
>
<title>
欢迎页面
</title>
</head>
<body>
恭喜
<s:property
value
=
"username"
/>
登录成功
</body>
</html>
|
error.jsp登录失败界面
<%@
page
language
=
"java"
contentType
=
"text/html; charset=UTF-8"
pageEncoding
=
"UTF-8"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=UTF-8"
>
<title>
登录失败页面
</title>
</head>
<body>
<p>
登录失败!
</p>
</body>
</html>
|
创建struts.xml
注意文件名称不要错。
文件路径:src文件夹根目录。
<?xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<!-- 启用开发模式 -->
<constant
name
=
"struts.devMode"
value
=
"true"
/>
<!-- package提供了将多个Action组织为一个模块的方式
package的名字必须是唯一的 package可以扩展 当一个package扩展自
另一个package时该package会在本身配置的基础上加入扩展的package
的配置 父package必须在子package前配置
name:package名称
extends:继承的父package名称
abstract:设置package的属性为抽象的 抽象的package不能定义action 值true:false
namespace:定义package命名空间 该命名空间影响到url的地址
例如此命名空间为/test那么访问是的
地址为http://localhost:8080/struts2/test/XX.action
-->
<package
name
=
"default"
extends
=
"struts-default"
namespace
=
"/"
>
<!-- 关键地方 struts2.5 为了提升安全性,添加了 allomethod-->
<global-allowed-methods>
regex:.*
</global-allowed-methods>
<!-- 异常处理 -->
<global-exception-mappings>
<exception-mapping
result
=
"exception"
exception
=
"java.lang.Exception"
/>
</global-exception-mappings>
<!-- Action配置 一个Action可以被多次映射(只要action配置中的name不同)
name:action名称
class: 对应的类的路径
method: 调用Action中的方法名 -->
<action
name
=
"login"
class
=
"com.rl.action.LoginAction"
>
<!-- 节点配置
name : result名称 和Action中返回的值相同
type : result类型 不写则选用superpackage的type
struts-default.xml中的默认为dispatcher -->
<result
name
=
"error"
>
/WEB-INF/view/error.jsp
</result>
<result
name
=
"success"
>
/WEB-INF/view/welcome.jsp
</result>
</action>
</package>
</struts>
|
在实际应用开发或者是产品部署的时候,对应着两种模式:开发模式(devMode);此时 devMode=ture;产品模式(proMode);此时 devMode=false;
启动工程
在浏览器地址栏输入:
http://localhost:8080/Struts2.5/login.jsp
输入错误的用户名和密码:
输入正确的用户名和密码(super 123456):
工程目录结构