1 package edu.b.recommender.actions;
2
3 import org.apache.struts2.convention.annotation.Action;
4 import org.apache.struts2.convention.annotation.Actions;
5 import org.apache.struts2.convention.annotation.Result;
6 import org.apache.struts2.convention.annotation.Results;
7
8 import com.opensymphony.xwork2.ActionSupport;
9
10
11 /*
12 * 这里的Results是此Action类中的全局跳转路径
13 * 如果要配置所有的Action类都能够用的全局跳转路径则在struts.xml中配置
14 * <!-- 全局results配置 -->
15 <global-results>
16 <result name="input">/error.jsp</result>
17 </global-results>
18 */
19 @Results({
20 @Result(name="index", location="/WEB-INF/page/index.jsp"),
21 @Result(name="test", location="/WEB-INF/page/test.jsp")
22 })
23 public class HomeAction extends ActionSupport {
24
25
26 public String execute() throws Exception{
27
28 //访问数据库
29 //获取首页中应该显示的数据
30 //怎么从/--->home.action ??????
31 //home.action---->index.jsp
32 return "index";
33 }
34
35 @Action(value="/other/bar",
36 results={@Result(name = "one", location = "www.baidu.com",type="redirect"),
37 @Result(name = "two", location = "/WEB-INF/page/two.jsp")
38 })
39 public String myTest(){
40 String test="dd";
41 if("dd".equals(test)){
42 return "one";
43 }else{
44 return "two";
45 }
46 }
47
48 @Actions({
49 @Action("/different/url"),
50 @Action("/another/url")
51 })
52 public String newTest(){
53 return "three";
54 }
55
56 //使用默认的访问路径,但是要规定返回的页面路径
57 //@Results @Result不允许在方法前面使用
58 //一个Action不写value则其默认的路径是什么???????????
59 @Action(value="home",
60 results={@Result(name = "home",location="home.action",type="redirect")
61 })
62 public String testFour(){
63
64 return "home";
65 }
66
67 }
68
重点关注一下type=redirect,redirectAction,chain的区别
1
package edu.b.recommender.actions;
2
3
import java.util.Enumeration;
4
import java.util.NoSuchElementException;
5
6
import javax.servlet.http.HttpServletRequest;
7
import javax.servlet.http.HttpSession;
8
9
import org.apache.struts2.ServletActionContext;
10
import org.apache.struts2.convention.annotation.Action;
11
import org.apache.struts2.convention.annotation.Actions;
12
import org.apache.struts2.convention.annotation.Result;
13
import org.apache.struts2.convention.annotation.Results;
14
import org.springframework.beans.factory.annotation.Autowired;
15
16
import com.opensymphony.xwork2.ActionSupport;
17
18
import edu.b.recommender.common.GlobalConfigure;
19
import edu.b.recommender.po.User;
20
import edu.b.recommender.service.UserManager;
21
22
/** *//**
23
* 用户登录Action.
24
*
25
* 使用Struts2 convention-plugin annotation定义Action参数.
26
*
27
* @author
28
*/
29
30
//在这里加一个会是什么效果@Action()
31
@Results(
{
32
@Result(name="loginSuccessToMymain", location="mymain",type="redirectAction"),
33
//必须写成/mymain.action
34
//@Result(name="loginSuccessToMymain", location="/mymain.action" ,type="dispatcher"),//也不行,是不是应该使用chain??
35
//使用chain可以,但是地址栏上不是显示新的new.action,chain相当于是action的forward,而dispatcher相当于是jsp的forward
36
//forward是不会更新地址栏的中地址,struts1.0中的直接掉转到某个action其实就是类似于chain,是不会更改地址栏的地址为新的.do
37
//@Result(name="loginSuccessToMymain", location="mymain",type="chain"),
38
@Result(name="loginFailToPrelogin", location="prelogin", type="redirectAction")
39
})
40
public class LoginAction extends ActionSupport
{
41
42
//使用的服务类
43
@Autowired
44
private UserManager userManager;
45
46
// 基本属性
47
private String username;
48
private String password;
49
50
private String returnMessage;
51
52
53
/**//*
54
* 用户登录执行的方法
55
* 表单提交了username ,password两个参数,如何获取这两个参数?
56
* 1,已经将参数封装到user的两个参数中了
57
* 2,通过ActionContext context = ActionContext.getContext()来获取这两个参数
58
* 将用户的参数保存到session中
59
*/
60
//默认应该是login.action--->login.jsp
61
//实际让其跳转到哪里?
62
public String execute() throws Exception
{
63
return login();
64
}
65
66
//默认返回success时login!login.action--->login.jsp
67
//默认返回xxxx时 login!login.action---->login-xxxx.jsp
68
//能不能将其定义为@Action("login")
69
public String login() throws Exception
{
70
User userInfo = new User();
71
userInfo.setUsername(username);
72
userInfo.setPassword(password);
73
System.out.println(username+"---------"+password);
74
//好像这样查询查不到
75
//User tempUser = userManager.checkUser(userInfo);
76
User tempUser = userManager.checkLogin(username, password);
77
if(tempUser!=null)
{
78
System.out.println(tempUser.getUsername());
79
80
//将用户保存到session中
81
82
//是否可以用ActionContext来保存
83
// ActionContext context = ActionContext.getContext();
84
//Map session = context.getSession();
85
86
HttpServletRequest httpRequest = ServletActionContext. getRequest();
87
HttpSession httpSession = httpRequest.getSession();
88
httpSession.setAttribute(GlobalConfigure.USER, tempUser);
89
returnMessage=tempUser.getUsername()+" ,您已经成功登录到系统,欢迎您!";
90
91
//一般的actionMessage是不能在redirect后看到的
92
//actionMessage在redirect后也看得到的,注意我们的struts.xml里的interceptor stack,加了个store,就负责干这个事情
93
//如果直接在重定向的action中获取之前的页面请求中的数据是不能获得的
94
//也可以将returnMessage写在ActionContext的session Map中,然后不用的时候一定要自己从session map中remove调
95
//但是可以将要显示的的字符串信息通过如下方法
96
addActionMessage(returnMessage);
97
98
System.out.println(tempUser);
99
//返回到用户的首页信息,mymain作为类的全局转向
100
return "loginSuccessToMymain";
101
102
}else
{
103
104
// 用户名或者密码错误
105
//需要分开用户名还是密码错误
106
userInfo.setUsername(username);
107
userInfo.setPassword(null);
108
if(userManager.checkUser(userInfo)==null)
{
109
//用户名错误,设置用户名错误信息
110
returnMessage="用户名不存在";
111
addActionMessage(returnMessage);
112
113
}else
{
114
//按用户名查找正确,则一定是密码错误
115
returnMessage="用户名密码输入错误";
116
addActionMessage(returnMessage);
117
}
118
//返回到用户的登录页面prelogin.action,fail作为此类的全局转向
119
return "loginFailToPrelogin";
120
}
121
122
}
123
124
125
/**//*
126
* 注销登录,主要是从session中将此用户的记录给去掉
127
* 为什么type不能用redirect-action
128
*/
129
130
@Action(value="logout",results=
{@Result(name = "home",location="home.action",type="redirect")
131
})
132
public String logout()throws Exception
{
133
134
//ActionContext context = ActionContext.getContext();
135
//Map session = context.getSession();
136
HttpServletRequest httpRequest = ServletActionContext. getRequest();
137
HttpSession httpSession = httpRequest.getSession();
138
try
{
139
//清空session中的内容
140
Enumeration e = httpSession.getAttributeNames();
141
for(;e.hasMoreElements();)
{
142
System.out.println("session element names:"+e.nextElement());
143
String elementName = (String)e.nextElement();
144
httpSession.removeAttribute(elementName);
145
}
146
}catch(NoSuchElementException e)
{
147
148
}catch(Exception e)
{
149
150
}
151
//销毁session
152
httpSession.invalidate();
153
154
// 返回到最开始的登录页面
155
return "home";
156
}
157
158
159
160
/**//*
161
* 用户注册,将用户提交的数据保存到数据库中
162
*/
163
public String register()throws Exception
{
164
return "";
165
}
166
167
168
169
/**//*
170
*主要用户页面的提交转向,没有实际的意思
171
*不得已最好不要用@Action注释
172
*在方法上加@Action相当于是给此方法重新定义了action名,参考此文件最好最后注释
173
*如果某个方法需要转向某个特定的result,则将这个result作为类的全局转向写在类的前面
174
*最好的使用方法还是使用login!prelogin.action来调用此action,但是返回页面需要作为类的全局results
175
*
176
*/
177
@Action(value="prelogin" ,results=
{@Result(name="toprelogin",location="/WEB-INF/page/prelogin.jsp")})
178
public String prelogin() throws Exception
{
179
//这是页面右上角的东西
180
//默认prelogin.action--->prelogin.jsp
181
//prelogin!prelogin.action--->prelogin.jsp
182
//默认login!prelogin.action--->login-toprelogin.jsp
183
return "toprelogin";
184
}
185
186
187
@Action(value="preregister", results=
{@Result(name="topreregister",location="/WEB-INF/page/preregister.jsp")})
188
public String preregister() throws Exception
{
189
//preregister.action-->preregister.jsp
190
//preregister!preregister.action--->preregister.jsp
191
//login!preregister.action--->login-topreregister.jsp
192
return "topreregister";
193
}
194
195
196
197
198
// 基本属性访问函数 //
199
200
/** *//**
201
* @return the returnMessage
202
*/
203
public String getReturnMessage()
{
204
return returnMessage;
205
}
206
207
/** *//**
208
* @param returnMessage the returnMessage to set
209
*/
210
public void setReturnMessage(String returnMessage)
{
211
this.returnMessage = returnMessage;
212
}
213
214
215
/** *//**
216
* @return the password
217
*/
218
public String getPassword()
{
219
return password;
220
}
221
222
223
/** *//**
224
* @param password the password to set
225
*/
226
public void setPassword(String password)
{
227
this.password = password;
228
}
229
230
231
/** *//**
232
* @return the username
233
*/
234
public String getUsername()
{
235
return username;
236
}
237
238
239
/** *//**
240
* @param username the username to set
241
*/
242
public void setUsername(String username)
{
243
this.username = username;
244
}
245
246
247
248
249
250
}
******************************************************************************
1》redirect:action处理完后重定向到一个视图资源(如:jsp页面),请求参数全部丢失,action处理结果也全部丢失。
2》redirect-action:action处理完后重定向到一个action,请求参数全部丢失,action处理结果也全部丢失。
3》chain:action处理完后转发到一个action,请求参数全部丢失,action处理结果不会丢失。
怎么我自己实验的请求参数没有丢失了???? ${username},请求数据应该不会丢失
******************************************************************************
Redirect Action Result:
这个Result使用ActionMapperFactory提供的ActionMapper来重定位浏览器的URL来调用指定的action和(可选的)namespace.
这个Result比ServletRedirectResult要好.因为你不需要把URL编码成xwork.xml中配置的ActionMapper提供的模式.
这就是说你可以在任意点上改变URL模式而不会影响你的应用程序. 因此强烈推荐使用这个Result而不是标准的redirect result来解决重定位到某个action的情况.
ActionName (默认) - 重定位到的action名
namespace - action的名称空间. 如果为null,则为当前名称空间
Redirect Result
调用{@link HttpServletResponse#sendRedirect(String) sendRedirect}方法来转到指定的位置.
HTTP响应被告知使浏览器直接跳转到指定的位置(产生客户端的一个新请求). 这样做的结果会使刚刚执行的action(包括action实例,action中的错误消息等)丢失, 不再可用.
这是因为action是建立在单线程模型基础上的. 传递数据的唯一方式就是通过Session或者可以为Ognl表达式的web参数(url?name=value)
location (默认) - action执行后跳转的地址.
parse - 默认为true. 如果设置为false, location参数不会被当作Ognl表达式解析.
<result name="success" type="redirect">/displayCart.action?userId=${userId}</result>
<action name= "delete " class= "com.zeng.action.UserManageAction " method= "delete ">
<result type= "redirect "> list.action?pageBean.pageNumber=${pageBean.pageNumber} </result>
</action>
*********************************************************************************************************
今天在用struts2在做项目时候,从一个action我想跳转到另一个action,并且呢得带上值。说说我的做法吧,首先你得在你的第一个action中这个id必须要有set、get方法。
跳转时你的struts.xml:
(方法一):
<result name="topic" type="redirect">/topicAction!findTopics.do?topicId=${topicId}</result>
(方法二):
<result name="topic" type="redirect-action">
<param name="actionName">findTopics</param>
<param name="topicId">${topicId}</param>
</result>
如果是多个参数的话,继续再加几个<param>就行了,对于(方法一)如果是多个参数的怎么办?
<result name="topic" type="redirect">/topicAction!findTopics.do?topicId=${topicId}&elementId=${elementId}</result>
这不就行了。
********************************************************************************
使用redirect重置链接需要后缀名,使用了redirect——action就不能使用了,
就例如使用chain一样,只需要写action的配置名,如果加入后缀名.action,就会报出异常,action未配置正确。
键字: struts2 redirect-action 传递 参数
????? 在做一个系统,使用struts2框架,在提交一个请求后,将获取的数据对象再要生成一个序列号,为了防止刷新生成冗余序列号,就在请求处理完成后,直接重定向到显示该信息的action中:
<action name="enterpreinfo" class="preinfoBusinessAction" method="enterPreinfoSub">
<result name="success" type="redirect-action">
showpreinfo?preinfo.order_number=${preinfo.order_number}&preinfo.company_name=${preinfo.company_name}
</result>
<result name="error" type="redirect">
<param name="location">/error.jsp</param>
</result>
</action>
?因为使用了redirect-action,所以要注意不能将showpreinf?preinfo.order_number=${preinfo.order_number}写成showpreinf.action?preinfo.order_number=${preinfo.order_number}
在这个配置文件里,多个参数的连接符使用了"&",但XML的语法规范,应该使用"&"代替"&",原理和HTML中的转义相同,开始没有注意,在struts分析配置文件时,总是报出这样的错误:
The reference to entity "preinfo" must end with the ';' delimiter.
?
进行上面说明的替换后,就正常了。
************************************************************************************
This is how I should do it
@Results({
@Result(name="input", type="redirectAction", params = {"actionName" , "resend"})
})
*************************************************************************************
http://corradignw.javaeye.com/blog/355717
struts2.1.6无论是xml还是annotation配置redirectAction时,如果要传一些参数,
可是这些参数在ServletActionRedirectResult并没有声明,这时ognl会抛异常出来。
但实际上传值是成功的。详见struts2的jira:
例:
Java代码
@Results({
@Result(name="reload",type="redirectAction"
,params={"actionName","hello_world"
,"namespace","/center/part1"
,"id","09"
,"count","90"})
})
@Results({
@Result(name="reload",type="redirectAction"
,params={"actionName","hello_world"
,"namespace","/center/part1"
,"id","09"
,"count","90"})
})
把日志级别调高也不管用,好像还没有什么解决办法。
****************************************************************************************
dispatcher 结果类型为缺省的result类型,用于返回一个视图资源(如:jsp)
Xml代码 :
<result name="success">/main.jsp</result>
<result name="success">/main.jsp</result>
以上写法使用了两个默认,其完整的写法为:
<result name="success" type="dispatcher">
<param name="location">/maini.jsp</param>
</result>
location只能是页面,不能是另一个action(可用type="chain"解决)。
redirect 结果类型用于重定向到一个页面,另一个action或一个网址。
Xml代码:
<result name="success" type="redirect">aaa.jsp</result>
<result name="success" type="redirect">bbb.action</result>
<result name="success" type="redirect">www.baidu.com</result>
redirect-action 结果类型使用ActionMapperFactory提供的ActionMapper来重定向请求到另外一个action
Xml代码:
<result name="err" type="redirect-action">
<param name="actionName">重定向的Action名</param>
<param name="namespace">重定向Action所在的名字空间</param>
</result>
redirect和redirect-action两种结果类型在使用上其实并没有什么区别,只是写法不同而已。
chain 用于把相关的几个action连接起来,共同完成一个功能。
Xml代码:
<action name="step1" class="test.Step1Action">
<result name="success" type="chain">step2.action</result>
</action>
<action name="step2" class="test.Step2Action">
<result name="success">finish.jsp</result>
</action>
处于chain中的action属于同一个http请求,共享一个ActionContext
plaintextj 结果类型用于直接在页面上显示源代码
Xml代码:
<result name="err" type="plaintext">
<param name="location">具体的位置</param>
<param name="charSet">字符规范(如GBK)</param>
</result>
posted on 2009-05-03 20:09
Frank_Fang 阅读(25213)
评论(6) 编辑 收藏 所属分类:
SSH+JQuery+DWR