Struts2的配置和一个简单的例子

什么是Struts2

Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的数据交互。Struts 2是Struts的下一代产品,是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架。其全新的Struts 2的体系结构与Struts 1的体系结构差别巨大。Struts 2以WebWork为核心,采用拦截器的机制来处理用户的请求,这样的设计也使得业务逻辑控制器能够与ServletAPI完全脱离开,所以Struts 2可以理解为WebWork的更新产品。虽然从Struts 1到Struts 2有着太大的变化,但是相对于WebWork,Struts 2的变化很小。

环境

idea
tomcat
jdk
maven

步骤

pom.xml 配置 Struts2

点击前往获取自己想要的版本对应的XML

1
2
3
4
5
6
7
8
9
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.16.3</version>
</dependency>

</dependencies>

创建 User 类 用于展现提取对象的数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.entity;

public class User {
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;
}
}

创建 LoginAction 类 代替以往的 servlet 层

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.action;


import com.entity.User;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import java.util.Map;

//继承 ActionSupport 用于实现 servlet层 数据校验
public class LoginAction extends ActionSupport implements Action {
//接收页面提交数据 属性名 为 input 标签 name 属性
private String username;
private String password;

//提供 get set 接收页面的数据 get 为提取 set 为接收
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;
}

//实现Action 的方法 为 struts.xml action 标签 method=""的默认方法入口 可以不写该属性
public String execute() throws Exception {
return null;
}

//用于实现 servlet层 数据校验
//在页面跳转到login 方法前会先进来这里面
@Override
public void validate() {
if(username.length()==0 || password.length()==0){
//如果在validate 方法里面 调用addFieldError 则后面不进入 login 方法 并如同login 默认返回 Action 接口提供的INPUT常量字符串
addFieldError("username","用户名不能为空");
addFieldError("password","密码不能为空");
}
}

//自定义入口 login
public String login() {
User user = new User();
user.setUsername("simon");
user.setPassword("simon");

//得到上下文对象
ActionContext context = ActionContext.getContext();

//得到request对象
Map<String, Object> request = (Map<String, Object>) context.get("request");

//得到session对象
Map<String, Object> session = context.getSession();

//得到application对象
Map<String, Object> application = context.getApplication();

if (user.getUsername().equals(getUsername()) && user.getPassword().equals(getPassword())) {

//登录成功存一个User对象
session.put("user", user);

//使用自定义字符串返回 success Action 接口提供的常量为 SUCCESS
return "success";
}

//定义errorinfo 错误信息到request
request.put("errorinfo", "登录失败");
//使用Action 接口提供的常量字符串
return ERROR;
}
}

创建页面 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 contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--从request里面找errorinfo的数据--%>
<h1>${requestScope.errorinfo}</h1>
<%--显示全部 fielderror 错误信息--%>
<h1><s:fielderror/></h1>
<%--显示某个 fielderror 错误信息--%>
<h1><s:fielderror fieldName="password"/></h1>
<hr/>
<%--Login 为 struts.xml 定义 action name 的名称 .action 为 Struts 标识--%>
<form action="Login.action" method="post">
<%--name属性要和LoginAction对象的属性一致--%>
用户名: <input type="text" name="username"/><br/>
密 码: <input type="password" name="password"/><br/>
<input type="submit" value="登录"/>
</form>
</body>
</html>

创建页面 success.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>登录成功</h1>
<h2>欢迎您,<s:property value="username"/></h2>
<h1>session对象的User对象数据</h1>
<h2>用户名: ${sessionScope.user.username}</h2>
<h2>密 码: ${sessionScope.user.password}</h2>
</body>
</html>

配置 web.xml 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

配置 struts.xml 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<package name="default" extends="struts-default">
<!--method 默认值为execute 也可以自定义login方法-->
<action name="Login" class="com.action.LoginAction" method="login">
<!--表示程序处理正常,并返回给用户成功后的结果-->
<result name="success">success.jsp</result>
<!--表示处理结果失败-->
<result name="error">index.jsp</result>
<!--表示数据校验处理失败-->
<result name="input">index.jsp</result>
</action>
</package>
</struts>

参考网址

Struts2官方文档