Struts1.2中ActionForward使用说明
2015-05-17 18:34
337 查看
一、ActionForward的作用:
封装转发路径,通俗点说就是说完成页面的跳转和转向。那它既然是转向,到底是转发还是重定向呢?默认的情况下,ActionForward采用的是转发的方式进行页面跳转的。
A.转发和重定向的区别:最大的区别就是转发时页面的url地址不变,而重定向的时候页面的url地址会发生变化。简单说明一下原因,因为转发的时候是采用的一个request(请求),既然页面跳转前后是同一个request,页面url当然不会变了。而重定向采用的是2个request,页面跳转前后的url当然会不同了。
B.ActionForward跳转的方式默认的是转发,那如果非要用重定向的方式,该如何设置呢?可以在struts-config.xml中添加配置<forward name="login" path="/login.jsp" redirect="true"/>,通常都没写redirect属性,而是采取的是它的默认值false,表示的是转发,如果设置为true,即为重定向。
二、全局和局部ActionForward:
在编程的时候,我们有时候会遇到这种情况:当增加一条记录成功时,跳转到成功页面,在struts-config.xml我们 对“增加”这个action加一个<forward name="success" path="/login_success.jsp"/>;在修改一条记录成功时,我们也会跳转到成功页面,在struts-config.xml我们对“修改”这个action加一个<forward
name="success" path="/login_success.jsp"/>;而同样当删除一条记录成功时,跳转到成功页面,在struts-config.xml我们又对“删除”这个action加一个同样的<forward name="success" path="/login_success.jsp"/>。相信大家会对同样的代码重复地写感到反感吧,那有没有办法解决呢?有,那就是全局ActionForward,针对上面这种情况,我们在<action-mappings>标签的上面加上:
A.如果不想对原有的struts-config.xml文件进行修改而修改跳转方式,可以在java文件的action中修改。
如下:
B.也可以不依赖struts的ActionForward进行转向,可以通过response进行转向,即在action中覆写execute方法,如下:
三、动态ActionForward:
动态的ActionForward可以在运行期修改。有这么个场景:页面有一个输入域,我输入1的时候,跳转到1的页面,输入2跳转到2的页面,我们利用之前学过的知识来实现下:
XML:
jsp页面:
<form action="dynaactionforward.do" method="post">
页面:<input type="text" name="page"><br>
<input type="submit" value="提交">
</form>
action:
public class DynaActionForwardTestAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
String page = request.getParameter("page");
ActionForward af = null;
if ("1".equals(page)) {
af = mapping.findForward("page1");
}else if ("2".equals(page)) {
af = mapping.findForward("page2");
}
return af;
}
}
但是如果我想在此基础上实现当我输入3,跳转到3的页面,输入4,5……以此类推下去,怎么办呢,如果还是采用这种方式,很麻烦。此时我们可以考虑采用动态ActionForward,说白了就是我们自己构造一个ActionForward,通过new的方式,看一下改后的action:
public class DynaActionForwardTestAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) throws Exception{
String page = request.getParameter("page");
ActionForward af = new ActionForward();
af.setPath("/page" + page + ".jsp?name=Tom");
return af;
}
}
之后再把xml中的<forward name="page1" path="/page1.jsp"/>,<forward name="page2" path="/page2.jsp"/>删除即可。大家还会发现动态ActionForward还有个好处,就是可以跟参数,此例中传递了name=tom的参数。
四、ActionForward对象定义:
假如路径为String url="index.jsp"
1.ActionForward af = new ActionForward();
af.setPath("url");
2.ActionForward af = new ActionForward("url");
3.ActionForward af= actionMapping.findForward("param");
封装转发路径,通俗点说就是说完成页面的跳转和转向。那它既然是转向,到底是转发还是重定向呢?默认的情况下,ActionForward采用的是转发的方式进行页面跳转的。
A.转发和重定向的区别:最大的区别就是转发时页面的url地址不变,而重定向的时候页面的url地址会发生变化。简单说明一下原因,因为转发的时候是采用的一个request(请求),既然页面跳转前后是同一个request,页面url当然不会变了。而重定向采用的是2个request,页面跳转前后的url当然会不同了。
B.ActionForward跳转的方式默认的是转发,那如果非要用重定向的方式,该如何设置呢?可以在struts-config.xml中添加配置<forward name="login" path="/login.jsp" redirect="true"/>,通常都没写redirect属性,而是采取的是它的默认值false,表示的是转发,如果设置为true,即为重定向。
二、全局和局部ActionForward:
在编程的时候,我们有时候会遇到这种情况:当增加一条记录成功时,跳转到成功页面,在struts-config.xml我们 对“增加”这个action加一个<forward name="success" path="/login_success.jsp"/>;在修改一条记录成功时,我们也会跳转到成功页面,在struts-config.xml我们对“修改”这个action加一个<forward
name="success" path="/login_success.jsp"/>;而同样当删除一条记录成功时,跳转到成功页面,在struts-config.xml我们又对“删除”这个action加一个同样的<forward name="success" path="/login_success.jsp"/>。相信大家会对同样的代码重复地写感到反感吧,那有没有办法解决呢?有,那就是全局ActionForward,针对上面这种情况,我们在<action-mappings>标签的上面加上:
<global-forwards> <forward name="success" path="/login_success.jsp"/> </global-forwards>上面就是对全局ActionForward的一种配置,而我们之前写的那些就是局部ActionForward。那么如果局部ActionForward和全局ActionForward同时出现,到底是按照哪个配置进行页面跳转呢?规则很简单,采用的是就近原则,就是说如果有局部ActionForward,就按照局部ActionForward就行跳转,如果没有就按照全局的跳转。
A.如果不想对原有的struts-config.xml文件进行修改而修改跳转方式,可以在java文件的action中修改。
如下:
ActionForward af = mapping.findForward("login"); af.setRedirect(false);记住要重启服务器,因为struts-config.xml文件不允许动态修改。
B.也可以不依赖struts的ActionForward进行转向,可以通过response进行转向,即在action中覆写execute方法,如下:
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { //重定向 response.sendRedirect(request.getContextPath() + "/login.jsp"); return null; }注意:return null是必须的。
三、动态ActionForward:
动态的ActionForward可以在运行期修改。有这么个场景:页面有一个输入域,我输入1的时候,跳转到1的页面,输入2跳转到2的页面,我们利用之前学过的知识来实现下:
XML:
<action-mappings> <action path="/dynaactionforward" type="com.bjsxt.struts.DynaActionForwardTestAction"> <forward name="page1" path="/page1.jsp"/> <forward name="page2" path="/page2.jsp"/> </action> </action-mappings>
jsp页面:
<form action="dynaactionforward.do" method="post">
页面:<input type="text" name="page"><br>
<input type="submit" value="提交">
</form>
action:
public class DynaActionForwardTestAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
String page = request.getParameter("page");
ActionForward af = null;
if ("1".equals(page)) {
af = mapping.findForward("page1");
}else if ("2".equals(page)) {
af = mapping.findForward("page2");
}
return af;
}
}
但是如果我想在此基础上实现当我输入3,跳转到3的页面,输入4,5……以此类推下去,怎么办呢,如果还是采用这种方式,很麻烦。此时我们可以考虑采用动态ActionForward,说白了就是我们自己构造一个ActionForward,通过new的方式,看一下改后的action:
public class DynaActionForwardTestAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) throws Exception{
String page = request.getParameter("page");
ActionForward af = new ActionForward();
af.setPath("/page" + page + ".jsp?name=Tom");
return af;
}
}
之后再把xml中的<forward name="page1" path="/page1.jsp"/>,<forward name="page2" path="/page2.jsp"/>删除即可。大家还会发现动态ActionForward还有个好处,就是可以跟参数,此例中传递了name=tom的参数。
四、ActionForward对象定义:
假如路径为String url="index.jsp"
1.ActionForward af = new ActionForward();
af.setPath("url");
2.ActionForward af = new ActionForward("url");
3.ActionForward af= actionMapping.findForward("param");
相关文章推荐
- 使用Struts1.2 Validator验证框架的简单例子
- struts1.2的Validator验证框架使用
- 在struts1.2中的action路径内使用通配符
- struts1.2中表单标签的应用---(form及text使用)源码解析
- struts中ActionForward 使用mapping.findForward如何传递get参数
- Struts html标签的使用及说明
- struts1.2中ActionForm和ServletFileUpload.parseRequest(request)不能同时使用
- Weblogic8.1 + struts1.2 中使用JSTL标签和EL表达式
- struts标签使用简单说明
- 你对MVC的理解,MVC有什么优缺点?结合Struts,说明在一个Web应用如何去使用?
- 在struts1.2中使用拦截器
- 使用struts、hibernate、spring进行框架整合流程详细说明(完整版)
- struts中ActionForward 使用mapping.findForward如何传递get参数
- struts的action中使用ActionForward来转发,redirect
- struts中ActionForward 使用mapping.findForward如何传递get参数
- Struts1.2中actionForm使用索引:ArrayList改用HashMap,避免错乱顺序
- 使用struts、hibernate、spring进行框架整合流程详细说明
- 你对MVC的理解,MVC有什么优缺点?结合Struts,说明在一个Web应用如何去使用?
- struts1 使用说明
- ActionErrors 使用说明 struts1 validate 处理流程 详细教程(转)