struts2(2)

搭建struts2环境

struts.xml配置

1
2
3
4
5
6
7
8
9
10
11
12
<?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="struts_test" namespace="/test" extends="struts-default">
<action name="TestAction" class="me.yanrs.server.TestAction" method="struts_test">
<result name="success">/aaa.jsp</result>
</action>
</package>
</struts>

web.xml配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?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>struts</display-name>
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <!-- ctrl+shift+t 搜索 strutspre 即可查询到,赋值即可 -->
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<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>
</web-app>

TestAction.java配置

1
2
3
4
5
6
7
package me.yanrs.server;
public class TestAction {
public String struts_test(){
System.out.println(1111);
return "success";
}
}

Struts2访问Servlet API

通过ActionContext【重点】

生命周期:每次请求都会创建一个与请求对应的ActionContext对象。请求处理完毕后,ActionContext销毁。
如何获得ActionContext:在Struts2中,会将ActionContext与当前线程绑定,只需从ThreadLocal获取即可。

TestAction.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 me.yanrs.server;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;

public class TestAction {
public String struts_test(){

// 通过ActionContext获得Session域 ———— map
Map<String, Object> session = ActionContext.getContext().getSession();
session.put("session_test", "session_test");

// 通过ActionContext获得Application域 ———— map
Map<String, Object> application = ActionContext.getContext().getApplication();
application.put("application_test", "application_test");

// 通过ActionContext获得Request域 [Struts2并不推荐使用原生的Request域,所以获取方式有所不同] ———— map
Map<String, Object> request = (Map<String, Object>)ActionContext.getContext().get("request");

// Struts2并不推荐使用原生的Request域,推荐使用
ActionContext.getContext().put("actioncontext", "actioncontext");

return "success";
}
}

aaa.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>aaa</h1>
session_test: ${session_test}
application_test: ${application_test}
actioncontext: ${actioncontext}
</body>
</html>

通过ServletActionContext

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
package me.yanrs.server;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

public class TestAction {
public String struts_test(){

// 通过ActionContext获得Session域 ———— map
//Map<String, Object> session = ActionContext.getContext().getSession();
//session.put("session_test", "session_test");

// 通过ActionContext获得Application域 ———— map
//Map<String, Object> application = ActionContext.getContext().getApplication();
//application.put("application_test", "application_test");

// 通过ActionContext获得Request域 [Struts2并不推荐使用原生的Request域,所以获取方式有所不同] ———— map
//Map<String, Object> request = (Map<String, Object>)ActionContext.getContext().get("request");

// Struts2并不推荐使用原生的Request域,推荐使用
//ActionContext.getContext().put("actioncontext", "actioncontext");

//第二种方式,使用ServletActionContext
//获取原生request
HttpServletRequest request = ServletActionContext.getRequest();

//获取原生session
HttpSession session = request.getSession();

//获取原生response
HttpServletResponse response = ServletActionContext.getResponse();

//获取原生servletContext
ServletContext servletContext = ServletActionContext.getServletContext();

return "success";
}
}

通过实现ServletRequestAware接口

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
package me.yanrs.server;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;


public class TestAction implements ServletRequestAware{
private HttpServletRequest request;
public String struts_test(){

// 通过ActionContext获得Session域 ———— map
//Map<String, Object> session = ActionContext.getContext().getSession();
//session.put("session_test", "session_test");

// 通过ActionContext获得Application域 ———— map
//Map<String, Object> application = ActionContext.getContext().getApplication();
//application.put("application_test", "application_test");

// 通过ActionContext获得Request域 [Struts2并不推荐使用原生的Request域,所以获取方式有所不同] ———— map
//Map<String, Object> request = (Map<String, Object>)ActionContext.getContext().get("request");

// Struts2并不推荐使用原生的Request域,推荐使用
//ActionContext.getContext().put("actioncontext", "actioncontext");

//第二种方式,使用ServletActionContext
//获取原生request
//HttpServletRequest request = ServletActionContext.getRequest();

//获取原生session
//HttpSession session = request.getSession();

//获取原生response
//HttpServletResponse response = ServletActionContext.getResponse();

//获取原生servletContext
//ServletContext servletContext = ServletActionContext.getServletContext();

return "success";
}

@Override
public void setServletRequest(HttpServletRequest request) {
// TODO Auto-generated method stub
this.request = request;
}
}

Struts2中的结果结果跳转方式

转发

地址栏不会发生改变
struts.xml配置

1
2
3
<action name="TestAction" class="me.yanrs.server.TestAction" method="struts_test">
<result name="success" type="dispatcher">/aaa.jsp</result>
</action>

重定向

地址栏会发生改变,属于页面的重定向,所以页面地址会变为….aaa.jsp
struts.xml配置

1
2
3
<action name="TestAction" class="me.yanrs.server.TestAction" method="struts_test">
<result name="success" type="redirect">/aaa.jsp</result>
</action>

转发到Action

地址栏不会发生改变
struts.xml配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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="struts_test2" namespace="/test2" extends="struts-default">
<action name="TestAction2" class="me.yanrs.server.TestAction2" method="struts_test2">
<result name="success">/aaa.jsp</result>
</action>
</package>

<package name="struts_test" namespace="/test" extends="struts-default">
<action name="TestAction" class="me.yanrs.server.TestAction" method="struts_test">
<result name="success" type="chain">
<param name="actionName">TestAction2</param>
<param name="namespace">/test2</param>
</result>
</action>
</package>
</struts>

TestAction2.java配置

1
2
3
4
5
6
7
8
package me.yanrs.server;

public class TestAction2 {
public String struts_test2(){
System.out.println("TestAction2");
return "success";
}
}

重定向到Action

地址栏会发生改变,属于Action的重定向,地址会变为重定向的那个【http://localhost:8080/struts2_02/test2/TestAction2.action】
struts.xml配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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="struts_test2" namespace="/test2" extends="struts-default">
<action name="TestAction2" class="me.yanrs.server.TestAction2" method="struts_test2">
<result name="success">/aaa.jsp</result>
</action>
</package>

<package name="struts_test" namespace="/test" extends="struts-default">
<action name="TestAction" class="me.yanrs.server.TestAction" method="struts_test">
<result name="success" type="redirectAction">
<param name="actionName">TestAction2</param>
<param name="namespace">/test2</param>
</result>
</action>
</package>
</struts>

TestAction2.java配置

1
2
3
4
5
6
7
8
package me.yanrs.server;

public class TestAction2 {
public String struts_test2(){
System.out.println("TestAction2");
return "success";
}
}

获取参数

Action的生命周期

每个请求到来的时候,都会创建一个新的Action实例。Action是线程安全的,可以使用成员变量来接收参数。

属性驱动

表单字段名称即为属性的名称,属性需要创建对应的get和set方法
aaa.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/test/TestAction">
姓名: <input type="text" name="name">
年龄: <input type="text" name="age">
生日: <input type="text" name="birthday">
<input type="submit" value="提交">
</form>
</body>
</html>

TestAction.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
package me.yanrs.server;

public class TestAction{

private String name;
private String age;
private String birthday;

public String struts_test(){
System.out.println(name);
System.out.println(age);
System.out.println(birthday);
return "success";
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAge() {
return age;
}

public void setAge(String age) {
this.age = age;
}

public String getBirthday() {
return birthday;
}

public void setBirthday(String birthday) {
this.birthday = birthday;
}

}

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

<struts>
<package name="struts_test2" namespace="/test2" extends="struts-default">
<action name="TestAction2" class="me.yanrs.server.TestAction2" method="struts_test2">
<result name="success">/aaa.jsp</result>
</action>
</package>

<package name="struts_test" namespace="/test" extends="struts-default">
<action name="TestAction" class="me.yanrs.server.TestAction" method="struts_test">
<result name="success">/aaa.jsp</result>
</action>
</package>
</struts>

对象驱动

表单字段名称为对象名称.属性,对象需要创建对应的get和set方法
aaa.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/test/TestAction">
姓名: <input type="text" name="user.name">
年龄: <input type="text" name="user.age">
生日: <input type="text" name="user.birthday">
<input type="submit" value="提交">
</form>

</body>
</html>

TestAction.java

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

import me.yanrs.dao.User;

public class TestAction{

private User user;

public String struts_test(){
System.out.println(user);
return "success";
}

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

}

struts.xml

1
同上

模型驱动

继承 ModelDriven 传入对象,复写getModel方法,并且返回对象
aaa.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/test/TestAction">
姓名: <input type="text" name="name">
年龄: <input type="text" name="age">
生日: <input type="text" name="birthday">
<input type="submit" value="提交">
</form>

</body>
</html>

TestAction.java

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

import com.opensymphony.xwork2.ModelDriven;

import me.yanrs.dao.User;

// 实现接口,参数为对象
public class TestAction implements ModelDriven<User>{
//准备user成员变量
private User user = new User();

public String struts_test(){
System.out.println(user);
return "success";
}

@Override
public User getModel() {
// 返回对象
return user;
}

}

struts.xml

1
同上

集合类型的参数

list

aaa.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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/test/TestAction">
list: <input type="text" name="list">
<input type="submit" value="提交">
</form>

</body>
</html>

TestAction.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
package me.yanrs.server;

import java.util.List;

import com.opensymphony.xwork2.ModelDriven;

import me.yanrs.dao.User;

public class TestAction {

private List<String> list;

public String struts_test(){
System.out.println(list);
return "success";
}

public List<String> getList() {
return list;
}

public void setList(List<String> list) {
this.list = list;
}

}

struts.xml

1
同上

map

aaa.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"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/test/TestAction">
map: <input type="text" name="map['key']">
<input type="submit" value="提交">
</form>
</body>
</html>

TestAction.java

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

import java.util.Map;

public class TestAction {

private Map<String, String> map;

public String struts_test(){
System.out.println(map);
return "success";
}

public Map<String, String> getMap() {
return map;
}

public void setMap(Map<String, String> map) {
this.map = map;
}
}

struts.xml
`
同上