Skip to content

Commit 5cdd5a8

Browse files
committed
Struts2 action简介、path开始、actionmethod配置
1 parent b5798a2 commit 5cdd5a8

14 files changed

+359
-12
lines changed

WebContent/actionintroduction.jsp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<%@ page language="java" contentType="text/html; charset=UTF-8"
2+
pageEncoding="UTF-8"%>
3+
<html>
4+
<head>
5+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
6+
<title>action示例</title>
7+
</head>
8+
<body>
9+
具体视图的返回可以由用户自己定义的Action来决定
10+
<br /> 具体的手段是根据返回的字符串找到对应的配置项,来决定视图的内容
11+
<br /> 具体Action的实现可以是一个普通的java类,里面有public String execute方法即可
12+
<br /> 或者实现Action接口
13+
<br /> 不过最常用的是从ActionSupport继承,好处在于可以直接使用Struts2封装好的方法
14+
<br />
15+
</body>
16+
</html>

WebContent/index.jsp

+28
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,39 @@
11
<%@ page language="java" contentType="text/html; charset=UTF-8"
22
pageEncoding="UTF-8"%>
3+
<%
4+
String path = request.getContextPath();
5+
String basePath = request.getScheme() + "://"
6+
+ request.getServerName() + ":" + request.getServerPort()
7+
+ path + "/";
8+
%>
9+
<%
10+
String context = request.getContextPath();
11+
%>
312
<html>
413
<head>
514
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
615
<title>页面未找到</title>
716
</head>
817
<body>
18+
路径问题path开始
19+
<br>
20+
<a href="path/path.action">路径问题</a>
21+
<br> 路径问题path结束
22+
<br>
23+
<br> ActionMethod开始
24+
<br> Action执行的时候并不一定要执行execute方法
25+
<br /> 可以在配置文件中配置Action的时候用method=来指定执行哪个方法
26+
也可以在url地址中动态指定(动态方法调用DMI)(推荐)
27+
<br />
28+
<a href="<%=context%>/user/userAdd">添加用户 不推荐</a>
29+
<br />
30+
<a href="<%=context%>/user/user!add">添加用户 推荐动态DMI</a>
31+
<br /> 前者会产生太多的action,所以不推荐使用;但是使用之前把struts.xml里面一句话修改成为 constant
32+
name="struts.enable.DynamicMethodInvocation" value="true"
33+
<br> ActionMethod结束
34+
<br>
35+
936
<p>Loading ...</p>
37+
1038
</body>
1139
</html>

WebContent/namespace.jsp

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
<%@ page language="java" contentType="text/html; charset=UTF-8"
22
pageEncoding="UTF-8"%>
3+
<%
4+
String path = request.getContextPath();
5+
String basePath = request.getScheme() + "://"
6+
+ request.getServerName() + ":" + request.getServerPort()
7+
+ path + "/";
8+
%>
39
<html>
410
<head>
511
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

WebContent/path.jsp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<%@ page language="java" contentType="text/html; charset=UTF-8"
2+
pageEncoding="UTF-8"%>
3+
<%
4+
String path = request.getContextPath();
5+
String basePath = request.getScheme() + "://"
6+
+ request.getServerName() + ":" + request.getServerPort()
7+
+ path + "/";
8+
%>
9+
<html>
10+
<head>
11+
<base href="<%=basePath%>" />
12+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
13+
<title>Path问题</title>
14+
</head>
15+
<body>
16+
struts2中的路径问题是根据action的路径而不是jsp路径来确定,所以尽量不要使用相对路径。
17+
<br />
18+
<a href="index.jsp">index.jsp</a>
19+
<br /> 虽然可以用redirect方式解决,但redirect方式并非必要。
20+
<br /> 解决办法非常简单,统一使用绝对路径。(在jsp中用request.getContextRoot方式来拿到webapp的路径)
21+
<br /> 或者使用myeclipse经常用的,指定basePath
22+
</body>
23+
</html>

WebContent/user_add.jsp

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<%@ page language="java" contentType="text/html; charset=UTF-8"
2+
pageEncoding="UTF-8"%>
3+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4+
<html>
5+
<head>
6+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7+
<title>Insert title here</title>
8+
</head>
9+
<body>User Add Success!
10+
</body>
11+
</html>

WebContent/user_add_success.jsp

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<%@ page language="java" contentType="text/html; charset=UTF-8"
2+
pageEncoding="UTF-8"%>
3+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4+
<html>
5+
<head>
6+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7+
<title>Insert title here</title>
8+
</head>
9+
<body>User Add Success!
10+
</body>
11+
</html>

build/classes/.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
/struts.xml
1+
/com.xh.struts2.front.action/
2+
/IndexAction1/
3+
/com/

build/classes/struts.xml

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!DOCTYPE struts PUBLIC
3+
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
4+
"http://struts.apache.org/dtds/struts-2.3.dtd">
5+
6+
<struts>
7+
8+
<!-- <constant name="struts.enable.DynamicMethodInvocation" value="false"
9+
/> <constant name="struts.devMode" value="true" /> <package name="default"
10+
namespace="/" extends="struts-default"> <default-action-ref name="index"
11+
/> <global-results> <result name="error">/WEB-INF/jsp/error.jsp</result>
12+
</global-results> <global-exception-mappings> <exception-mapping exception="java.lang.Exception"
13+
result="error" /> </global-exception-mappings> <action name="index"> <result
14+
type="redirectAction"> <param name="actionName">HelloWorld</param> <param
15+
name="namespace">/example</param> </result> </action> </package> <include
16+
file="example.xml" /> -->
17+
18+
<!-- Add packages here -->
19+
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
20+
<!-- 开发模式 -->
21+
<constant name="struts.devMode" value="true" />
22+
23+
<!-- 介绍 -->
24+
<package name="default" namespace="/" extends="struts-default">
25+
<action name="hello">
26+
<result>
27+
/Hello.jsp
28+
</result>
29+
</action>
30+
</package>
31+
32+
<!-- namespace -->
33+
<package name="front" extends="struts-default" namespace="/front">
34+
<action name="namespace">
35+
<result>
36+
/namespace.jsp
37+
</result>
38+
</action>
39+
</package>
40+
41+
<!-- namespace为空的时候 package name 不可相同否则出错 -->
42+
<package name="main" extends="struts-default" namespace="">
43+
<action name="namespace">
44+
<result>
45+
/namespace.jsp
46+
</result>
47+
</action>
48+
</package>
49+
50+
<!-- action开始 -->
51+
<package name="action" extends="struts-default" namespace="/">
52+
<action name="actionintroduction" class="com.struts2.front.action.IndexAction3">
53+
<result name="success">
54+
/actionintroduction.jsp
55+
</result>
56+
<result name="index">
57+
/index.jsp
58+
</result>
59+
<result name="namespace">
60+
/namespace.jsp
61+
</result>
62+
</action>
63+
</package>
64+
<!-- action结束 -->
65+
66+
<!-- path开始 -->
67+
<package name="path" extends="struts-default" namespace="/path">
68+
<action name="path" class="com.struts2.front.path.PathAction">
69+
<result name="path">
70+
/path.jsp
71+
</result>
72+
</action>
73+
</package>
74+
<!-- path结束 -->
75+
76+
<!-- ActionMethod开始 -->
77+
<package name="user" extends="struts-default" namespace="/user">
78+
<action name="userAdd" class="com.struts2.front.action.UserAction"
79+
method="add">
80+
<result>
81+
/user_add.jsp
82+
</result>
83+
</action>
84+
<action name="user" class="com.struts2.front.action.UserAction">
85+
<result>
86+
/user_add.jsp
87+
</result>
88+
</action>
89+
</package>
90+
<!-- ActionMethod结束 -->
91+
92+
93+
</struts>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*************************************************************************************************
2+
* 版权所有 (C)2015
3+
*
4+
* 文件名称:IndexAction1.java
5+
* 内容摘要:IndexAction1.java
6+
* 当前版本:TODO
7+
* 作 者:李加蒙
8+
* 完成日期:2016年8月16日 上午9:52:54
9+
* 修改记录:
10+
* 修改日期:2016年8月16日 上午9:52:54
11+
* 版 本 号:
12+
* 修 改 人:
13+
* 修改内容:
14+
************************************************************************************************/
15+
package com.struts2.front.action;
16+
17+
/**
18+
* @filename 文件名称:IndexAction1.java
19+
* @contents 内容摘要:
20+
*/
21+
public class IndexAction1 {
22+
public String execute() {
23+
return "index";
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*************************************************************************************************
2+
* 版权所有 (C)2015
3+
*
4+
* 文件名称:IndexAction2.java
5+
* 内容摘要:IndexAction2.java
6+
* 当前版本:TODO
7+
* 作 者:李加蒙
8+
* 完成日期:2016年8月16日 下午1:06:35
9+
* 修改记录:
10+
* 修改日期:2016年8月16日 下午1:06:35
11+
* 版 本 号:
12+
* 修 改 人:
13+
* 修改内容:
14+
************************************************************************************************/
15+
package com.struts2.front.action;
16+
17+
import com.opensymphony.xwork2.Action;
18+
19+
/**
20+
* @filename 文件名称:IndexAction2.java
21+
* @contents 内容摘要:
22+
*/
23+
public class IndexAction2 implements Action {
24+
@Override
25+
public String execute() throws Exception {
26+
return "namespace";
27+
}
28+
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*************************************************************************************************
2+
* 版权所有 (C)2015
3+
*
4+
* 文件名称:IndexAction3.java
5+
* 内容摘要:IndexAction3.java
6+
* 当前版本:TODO
7+
* 作 者:李加蒙
8+
* 完成日期:2016年8月16日 上午10:59:31
9+
* 修改记录:
10+
* 修改日期:2016年8月16日 上午10:59:31
11+
* 版 本 号:
12+
* 修 改 人:
13+
* 修改内容:
14+
************************************************************************************************/
15+
package com.struts2.front.action;
16+
17+
import com.opensymphony.xwork2.ActionSupport;
18+
19+
/**
20+
* @filename 文件名称:IndexAction3.java
21+
* @contents 内容摘要:只用IndexAction3 不用IndexAction1与2; 因为ActionSupport已经封装好了
22+
*/
23+
public class IndexAction3 extends ActionSupport {
24+
@Override
25+
public String execute() throws Exception {
26+
return super.execute();
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*************************************************************************************************
2+
* 版权所有 (C)2015
3+
*
4+
* 文件名称:UserAction.java
5+
* 内容摘要:UserAction.java
6+
* 当前版本:TODO
7+
* 作 者:李加蒙
8+
* 完成日期:2016年8月16日 下午2:15:36
9+
* 修改记录:
10+
* 修改日期:2016年8月16日 下午2:15:36
11+
* 版 本 号:
12+
* 修 改 人:
13+
* 修改内容:
14+
************************************************************************************************/
15+
package com.struts2.front.action;
16+
17+
import com.opensymphony.xwork2.ActionSupport;
18+
19+
/**
20+
* @filename 文件名称:UserAction.java
21+
* @contents 内容摘要:
22+
*/
23+
public class UserAction extends ActionSupport {
24+
public String add() {
25+
return SUCCESS;
26+
}
27+
28+
}
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
/*************************************************************************************************
22
* 版权所有 (C)2015
33
*
4-
* 文件名称:T.java
5-
* 内容摘要:T.java
4+
* 文件名称:PathAction.java
5+
* 内容摘要:PathAction.java
66
* 当前版本:TODO
77
* 作 者:李加蒙
8-
* 完成日期:2016年8月15日 下午5:10:15
8+
* 完成日期:2016年8月16日 下午1:31:30
99
* 修改记录:
10-
* 修改日期:2016年8月15日 下午5:10:15
10+
* 修改日期:2016年8月16日 下午1:31:30
1111
* 版 本 号:
1212
* 修 改 人:
1313
* 修改内容:
1414
************************************************************************************************/
15+
package com.struts2.front.path;
1516
/**
16-
@filename 文件名称:T.java
17-
@contents 内容摘要:
17+
@filename 文件名称:PathAction.java
18+
@contents 内容摘要:
1819
*/
19-
public class T {
20-
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter f;
20+
public class PathAction {
21+
public String execute() {
22+
return "path";
23+
}
2124
}
2225

0 commit comments

Comments
 (0)