zoukankan      html  css  js  c++  java
  • struts2 中redirectAction如何传递参数!

    在struts2中,初学者因为参数传递的问题往往会出现一些错误。

    比如页面跳转的问题,在用户注册中,以一下代码作为案例:

    <struts>
        <constant name="struts.action.extension" value="action,,"></constant>
        <constant name="struts.devMode " value="true"></constant>
         <constant name="struts.enable.DynamicMethodInvocation" value="true" />  
        <package name="user" namespace="/" extends="struts-default">
            <action name="*" class="user.TotalAction" method="{1}" >
                <result name="success">/WEB-INF/user/userlist.jsp</result>
                <result name="modify">/WEB-INF/user/modifyit.jsp</result>
                <result name="list" type="redirectAction">userlist</result>
            </action>
        </package>
    </struts>
    1     public String userlist() throws Exception {
    2         // TODO Auto-generated method stub
    3         UserDAO dao= new UserDAO();
    4         ArrayList<User> list= dao.getUser();
    5     //    System.out.println("dao::"+list.size());
    6         ActionContext acx= ActionContext.getContext();
    7         acx.put("USERLIST", list);
    8         
    9         return "success";
    1     public String save() throws ClassNotFoundException, SQLException
    2     {
    3         UserDAO dao= new UserDAO();
    4         dao.modifyUser(user);
    5         return "list";
    6     }

    笔者如果想要通过save()方法直接将值传递到>/WEB-INF/user/userlist.jsp。如果不借助redirectAction,最笨的方法就是将userlist()方法重写一遍,然后还回success。

    但是借助redirectAction,可以直接重定向到新的地址。就是说通过配置一个struts中的rusult方法:加入redirectAction对象,可以在action中通过返回list方法,重新流入<action配置中,重定向,然后再找到userlist方法,以此传递数据。《初次学习,总结如有不足或不对之处请指出》

    struts2 中chain、redirect、redirectaction的区别 :

    Chain Result:
    这个result调用另外的一个action,连接自己的拦截器栈和result。

    • actionName (默认) - 被调用的action的名字
    • namespace - 被调用的action的名称空间. 如果名称空间为空,这默认为当前名称空间
    • method - 用于指定目标action的另一个方法被调用. 如果空,默认为excute方法

    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表达式解析.

    二。当使用type=“redirectAction” 或type=“redirect”提交到一个action并且需要传递一个参数时。这里是有区别的:
    a.使用type=“redirectAction”时,结果就只能写Action的配置名,不能带有后缀:“.action”
    Java代码

    <action name="Login" class="steven.actions.LoginAction">
    <result name="success" type="redirectAction">User?u_id=${loginBean.u_id}</result>
    </action>


    b.使用type=“redirect”时,结果应是action配置名+后缀名
    Java代码
     

    <action name="Login" class="steven.actions.LoginAction">
    <result name="success" type="redirect">User.action?u_id=${loginBean.u_id}</result>
    </action> 


    ps:1 redirect:action处理完后重定向到一个视图资源(如:jsp页面),请求参数全部丢失,action处理结果也全部丢失。
         2 redirect-action:action处理完后重定向到一个action,请求参数全部丢失,action处理结果也全部丢失。
         3 chain:action处理完后转发到一个action,请求参数全部丢失,action处理结果不会丢失。

     
     
  • 相关阅读:
    HDOJ1213 并查集
    poj 3070 Fibonacci
    csu 1102 Palindrome
    C#格式化数值结果表
    正则表达式基础知识
    C#验证Email是否真正存在
    【翻译】Scott Mitchell的ASP.NET2.0数据教程中文版索引
    分块下载,测试文件 3.8GB
    asp.net的TextBox回车触发事件
    Cookie加密
  • 原文地址:https://www.cnblogs.com/xiadongqing/p/5246754.html
Copyright © 2011-2022 走看看