转载于:http://www.wozaishuo.com.cn/article.asp?id=259
struts2 的重定向和struts1 在使用方法上有所不同。
如在一个登录的action中验证成功后,重定向为显示用户信息的action: showInfo.do
一、在struts1 中实现:
程序代码
public class LoginAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
//一些处理……
//重定向
ActionForward forward = new ActionForward("showInfo.do");
forward.setRedirect(true);
return forward ;
}
}
二、在struts2 中,因为执行函数返回结果不再是ActionForward ,而是一个字符串,所以不能再像struts1中那样跳转了。
在struts2中,重定向要在struts.xml中配置:
程序代码
<action name="login" class="LoginAction">
<result name="success" type="velocity">/pages/logok.vm</result>
<result name="redirect_1" type="redirect">showInfo.do</result>
<result name="redirect_2" type="redirect">showInfo.do?name=yangzi</result>
<result name="redirect_3" type="redirect">showInfo.do?name=${name}</result>
<result name="redirect_4" type= "redirect">
<param name="actionName">showInfo</param>
<param name="name">${name}</param>
</result>
</action>
对应的LoginAction:
程序代码
public class LoginAction extends ActionSupport{
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String execute() throws Exception {
//一些处理……
name=xiaowang ; //给要传递的参数赋值
return SUCCESS; //默认页面
//return "redirect_1" ; //重定向(不带参数) showInfo.do
//return "redirect_2" ; //重定向(带固定参数yangzi) showInfo.do?name=yangzi
//重定向(带动态参数,根据struts.xml的配置将${name}赋值为xiaowang)最后为 showInfo.do?name=xiaowang
// return "redirect_3" ;
//return "redirect_4" ; //这个是重定向到 一个action
}
}
三、说明
struts2 重定向分重定向到url和重定向到一个action。
实现重定向,需在struts.xml中定义返回结果类型。
type="redirect" 是重定向到一个URL。type="redirect-action" 是重定向到一个action。
参数也是在这里指定,action中所做的就是给参数赋值,并return 这个结果。
个人认为:由于大家极度抱怨“action臃肿”,所以struts2中尽量减少了action中的代码。
分享到:
相关推荐
struts2 重定向struts2 重定向struts2 重定向struts2 重定向struts2 重定向
struts result类型中redirect与redirectAction的使用 包括参数传递。
在struts中测试登录的redirect,transmit 一个测试重定向和转发的demo。演示转发到jsp和action的过程。 供参考。
struts2防止表单重复提交--重定向
struts2中result类型之redirect,重定向
通过两个自定义结果集,解决struts2请求转发或重定向时,一个结果码无法对应多个视图的问题。以前的做法,是在配置Action时使用通配符解决,但这样需要JSP页面名称和Action的方法名称一致,如果命名不一致或不规范,...
1.概述strust2中的拦截器 28 2.自定义拦截器 28 方式一,实现Interceptor接口。 28 方式二、继承AbstractInterceptor抽象类 29 方式三、继承MethodFilterInteceptor类 30 3.使用来MethodFilterInterceptor灵活拦截 ...
在struts2中可以在方法上传递相应的参数,不需要使用注解,直接使用参数名进行界面传递参数信息,并且可以在界面进行访问
struts2中result类型之redirectAction
1.3. Struts 框架中的组件.3 1.3.1. Struts配置文件.3 1.4. ActionServlet类6 1.4.1. ActionServlet配置6 1.4.2. ActionServlet方法7 1.5. ActionMapping 类8 1.6. Action 类.8 1.6.1. Action 类的方法.9 1.7. ...
用户可以构造http://host/struts2-blank/example/X.action?action:%25{(new+java.lang.ProcessBuilder(new+java.lang.String[]{'command','goes','here'})).start()}链接,command goes here可以换成是破坏脚本的...
关于Apache Struts 2软件存在高危漏洞的紧急通报003
用tomcat服务器,通过struts.xml写的action来操作网页之间的重定向和对login数据的校验。(分为admin和user的login网页)
目录 概述 4 Framework概念 4 Struts的概念和体系结构 5 Struts的与Web App的关系 5 ...Struts的体系结构 5 ...struts中的Forms 45 struts:介绍ActionForm 49 分离Book和BookForm的一个好方法 51
概述 4 Framework概念 4 Struts的概念和体系结构 5 Struts的与Web App的关系 5 Struts的体系结构 5 从视图角度(View) 6 ...struts中的Forms 45 struts:介绍ActionForm 49 分离Book和BookForm的一个好方法 51
1.3 Struts框架中的组件 5 1.3.1 Struts配置文件 5 1.3.2 ActionServlet类 8 1.3.3 ActionMapping类 10 1.3.4 Action类 10 1.3.5 ActionForm类 11 1.3.6 ActionForward类 12 1.3.7 错误处理 12 2 Struts标记库 14 ...
概述 4 Framework概念 4 Struts的概念和体系结构 5 Struts的与Web App的关系 5 Struts的体系结构 5 从视图角度(View) 6 ...struts中的Forms 45 struts:介绍ActionForm 49 分离Book和BookForm的一个好方法 51
修订后的,点击路径不会重定向log网站,内容丰富,包含了struts2、js、jstl、ajax常用函数、spring标签等
2. struts体系结构(模型-视窗-控制器)…………………………………………………3 3. struts框架的组件………………………………………………………………………4 3.1 struts配置文件…………………………………………...
概述 4 Framework概念 4 Struts的概念和体系结构 5 Struts的与Web App的关系 5 Struts的体系结构 5 从视图角度(View) 6 ...struts中的Forms 45 struts:介绍ActionForm 49 分离Book和BookForm的一个好方法 51