目录结构
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
<?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_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Struts2MVCDemo</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>
<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>
</web-app>
struts.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
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<!-- 不需要默认Spring容器管理struts -->
<constant name="struts.objectFactory"
value="com.opensymphony.xwork2.ObjectFactory"></constant>
<package name="person" namespace="/person" extends="struts-default">
<action name="hello1" class="com.bbs.action.PersonAction">
<result name="success">/WEB-INF/success.jsp</result>
</action>
<action name="hello2" class="com.bbs.action.PersonAction" method="zhangxueyou">
<result name="success">/WEB-INF/success.jsp</result>
<result name="error">/WEB-INF/error.jsp</result>
</action>
<action name="hello3" class="com.bbs.action.PersonAction" method="liming">
<result name="success">/WEB-INF/success.jsp</result>
<result name="error">/WEB-INF/error.jsp</result>
</action>
</package>
</struts>
PersonAction.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
39
40
41
package com.bbs.action;
// struts2 Action层
public class PersonAction {
private String name;
// 视图从值栈中获取name
public String getName() {
System.out.println("调用getName方法");
return name;
}
public void setName(String name) {
System.out.println("调用setName方法,在actionContext中记录值栈信息");
this.name = name;
}
// 默认执行
public String execute() {
// 返回result结果,视图名字
return "success";
}
public String zhangxueyou() {
if (true) {
return "success";
} else {
return "error";
}
}
public String liming() {
System.out.println("获取页面参数:" + name);
if (name == null || name.equals("")) {
return "error";
}
return "success";
}
}
success.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ 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>Success页面</title>
</head>
<body>
success获取值栈属性值:${name }
<s:debug></s:debug>
</body>
</html>
index.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<%@ 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>Index页面</title>
</head>
<body>
index
<a href="http://localhost:8080/Struts2MVCDemo/person/hello1">
请求链接-------PersonAction中的方法
</a>
</body>
</html>
error.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
<%@ 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>Error页面</title>
</head>
<body>
success
</body>
</html>