`

Webwork_action重定向

 
阅读更多

一个Action中的两个方法之间的跳转

 <action name="editCmacGrade" class="cmacGradeAction" method="editCmacGrade">  

        <result name="input">/WEB-INF/templates/cmac/grade_edit.ftl</result>    

        <result name="success" type="redirectAction">  

            <param name="namespace">/basesetting</param>  

            <param name="actionName">mangrade</param>  

       </result>  

 </action>

第一个result,是editCmacGrade方法执行完了直接跳转到grade_edit.ftl页面, 

第二个result,是editCmacGrade方法执行了跳转到两个方法执行的,相当于跳到了/basesetting/mangrade.com这个URl,如果直接跳转的这个URL对应的.ftl页面,而这个页面需要从Action返回的参数,这样就会出错了。 

比如新增或者修改完成之后要跳转到列表页面就需要第二个result了,因为列表页面需要Action返回一个List对象。 

 

给一个Action的result传多个参数

<action name="blahblahAction" class="blahAction" method="blah">

   <result name="success" type="redirect">/some.action?field1=${field1}&field2=${field2}</result>

</action>

运行时候出现如下提示:

The reference to entity "field2" must end with the ';' delimiter.

使用"&amp;"代替"&",原理和HTML中的转义相同

配置如下: 

<action name="blahblahAction" class="blahAction" method="blah">

   <result name="success" type="redirect">/some.action?field1=${field1}&amp;field2=${field2}</result>

</action>

 

webwork不同namespace之间action的跳转传值

使用ognl传值,利用redirect跳转,在action中给returnUrl属性赋值(注意带上命名空间)

  <result name="success" type="redirect">

    ${returnUrl}

  </result>

 

Webwork2 之Result Type

和Webwork1.x不同,Webwork2的Action执行完后,其Result对应一个 Result Type,而这个Result Type完全可以根据具体应用或环境自己进行 定义,只需实现com.opensymphony.xwork.Result接口。Result Type使得Action的执行结果表现形式可以灵活多变!下面这会举例说明,这里先看看Webwork2提供的几种Result Type的定义,该定义在webwork-default.xml中,xwork.xml文件包含了该文件,自定义的Result Type可以直接写在 xwork.xml中:
  <result-types>
    <result-type name="dispatcher" class="com.opensymphony.webwork.dispatcher.ServletDispatcherResult" default="true"/>
    <result-type name="redirect" class="com.opensymphony.webwork.dispatcher.ServletRedirectResult"/>
    <result-type name="velocity" class="com.opensymphony.webwork.dispatcher.VelocityResult"/>
    <result-type name="chain" class="com.opensymphony.xwork.ActionChainResult"/>
    <result-type name="xslt" class="com.opensymphony.webwork.views.xslt.XSLTResult"/>
    <result-type name="jasper" class="com.opensymphony.webwork.views.jasperreports.JasperReportsResult"/>
    <result-type name="freemarker" class="com.opensymphony.webwork.views.freemarker.FreemarkerResult"/>
  </result-types>
  其大多都有location和parse两个参数,location指明action执行后接着去哪里,parse指明是否对location进行OGNL表达式解析。
  
  1) dispatcher
    action执行完后,请求会导向对应的View,Webwork2幕后其实是用RequestDispatcher来处理的,所以原Request/Response对象会接着传递,原Request中的Atrributes不会丢失,这点与下面的redirect是不同的。

    参数  必需 描述

   location 是         执行后跳转的位置(例如: jsp)

   parse 否         缺省为true. 如果设为false, location参数将不作为Ognl表达式进行解析

   <result name="success" type="dispatcher">

            <param name="location">/helloworld/sucess.jsp</param>

   </result>

   这个可以简写成

   <result name="success" type="dispatcher">/helloworld/sucess.jsp</result>

  2) redirect
    对上次的响应将重定向到指定的位置,redirect是重新产生一个新的Request,原来Request保存的东西将不再有效,比如不能通过requet.getAtrribute 取得原来set的对象,也不能取得action的实例,errors,field errors等,因为     Action是建立在Single-thread model上的。这意味着刚才执行的活动(活动实例, 活动错误, 字段错误等)将丢失不再可用. 这是因为活动基于单线程模式. 传递参数的唯一办法是使用session或使用OGNL表达式作为web参数(url?name=value)

 

   location  是 执行后跳转的位置

   parse 否 缺省为true. 如果设为false, location参数将不作为Ognl表达式进行解析

 

   <result name="success" type="redirect">

            <param name="location">/helloworld/sucess.jsp</param>

            <param name="parse">false</param>

   </result>

 

  3) chain
    action链,特殊的View调用方式,一个action执行完接着调用另一个action。有个必须的参数actionName,指明紧接着调用的另一action对象。

    actionName 是 要链接的活动名

    namespace 否 设置链接活动的名空间. 如果名空间为空, 使用当前名空间[或缺省名空间""].

    <result name="success" type="chain">
        <param name="actionName">bar</param>
        <param name="namespace">/foo</param>
    </result>
  执行后接着调用下面的action:
    <action name="bar" class="myPackage.barAction">
        ...
    </action>
  4) velocity  该结果模拟JSP运行环境, 显示Velocity模版结果并直接输出到servlet输出流.(跳转到一个vm)

 

    location是 执行后跳转的位置

    parse 否 缺省为true. 如果设为false, location参数将不作为Ognl表达式进行解析

 

    <result name="success" type="velocity">

       <param name="location">foo.vm</param>

    </result>

 

  5) freemarker FreeMarker是一个纯Java模板引擎;一个普通的基于模板生成文本的工具,它只能应用在Web应用环境中(跳转到一个ftl页面)

 

    location 是 执行后跳转的位置

    parse            否     默认值true,当为false时, location的参数将不会被Ognl表达式解析

    contentType  否    默认为"text/html" 

    <result name="success" type="freemarker">foo.ftl</result>

  6) jasperreports  将jsp通过JasperReports报表的形式输出,默认是PDF(输出PDF)

    location                  是   执行完成之后转向的位置

    parse                     否   默认值true,当为false时, location的参数将不会被Ognl表达式解析

    dataSource            是   它是Action的一个字段(通常是一个List),OGNL表达式被用来去value stack(OgnlValueStack)重新找回这个dataSource

    format                    否   格式化报表,默认是PDF

    contentDisposition 否   除非你使用了documentName,否则默认是"inline"

    documentName     否   你的http页面头(head)"Content-disposition = <contentDisposition>; filename=<documentName>.<format>"

    <result name="success" type="jasper">

      <param name="location">foo.jasper</param>

      <param name="dataSource">mySource</param>

      <param name="format">CSV</param>

    </result>

    或者PDF

   <result name="success" type="jasper">

      <param name="location">foo.jasper</param>

      <param name="dataSource">mySource</param>

   </result>

  7) xslt  与XSLT接口

    location是 执行后跳转的位置

    parse        否 缺省为true. 如果设为false, location参数将不作为Ognl表达式进行解析

    <result name="success" type="xslt">foo.xslt</result>

  8) HttpHeader Result: 使用值栈定义HTTP头信息.

    status    否 HTTP响应的状态码

    parse    否 缺省为true. 如果设为false, location参数将不作为Ognl表达式进行解析

    headers   否 头信息的值

    <result name="success" type="header">

       <param name="status">204</param>

       <param name="headers.a">a custom header value</param>

       <param name="headers.b">another custom header value</param>

    </result>

  9) Stream Result: 直接向HttpServletResponse发送(通过InputStream)原始(raw)数据流. 用户下载正文时非常有用(直接输出流)

 

    inputName            否 inputStream 活动中类型为InputStream的属性的名字(如getInputStream())

    contentType            否 text/plain 返回的正文类型

    contentDisposition   否 inline 响应的头信息Content-disposition的值. 典型的值是filename="doc.pdf"

    bufferSize            否 1024 缓冲大小(字节数)

    <result name="success" type="stream">

       <param name="inputName">inputStream</param>

       <param name="contentType">${contentType}</param>

       <param name="contentDisposition">filename="${filename}"</param>

       <param name="bufferSize">2024</param>

    </result>

 

    以上都是用不同技术的产生不同的View。

  10) 自定义Result Type 示例,假如有个Action testSendmail,根据处理结果将给指用户发送一份email。自定义一个Result Type,实现Result接口。
    com.mycompany.webwork.example.SendmailResult
    有三个必须参数:from ,to, subject,一个可选参数 body。
  在xwork.xml中定义如下:
  <result-types>
    <result-type name="sendmail" class="com.mycompany.webwork.example.SendmailResult"/>
  </result-types>
  
  action定义:
  <action name="testSendmail" class="com.mycompany.webwork.example.TestSendMailAction">
    <result name="success" type="sendmail">
        <param name="from">root@sina.com</param>
        <param name="to">user@sina.com</param>
        <param name="subject">hello,webwork!</param>
    </result>
    <result name="error" type="dispatcher">
        <param name="location">error.jsp</param>
    </result>
  </action>
  
  SendmailResult.java
  package com.opensymphony.webwork.example;
  import com.opensymphony.xwork.ActionInvocation;
  import com.opensymphony.xwork.Result;

  public class SendmailResult implements Result {
    private String to;
    private String from;
    private String subject;
    private String body;

    public void execute(ActionInvocation invocation) throws Exception {
    //TODO 实现Email发送部分
    System.out.println("sending mail....");
    System.out.println("   To:" + to);
    System.out.println("   From:" + from);
    System.out.println("Subject:" + subject);
    }
    public String getBody() {
    return body;
    }
    public void setBody(String body) {
    this.body = body;
    }
    public String getFrom() {
    return from;
    }
    public void setFrom(String from) {
    this.from = from;
    }
    public String getSubject() {
    return subject;
    }
    public void setSubject(String subject) {
    this.subject = subject;
    }
    public String getTo() {
    return to;
    }
    public void setTo(String to) {
    this.to = to;
    }
  }

  写个简单的测试Action:
  package com.opensymphony.webwork.example;
  import com.opensymphony.xwork.ActionSupport;
  public class TestSendMailAction extends ActionSupport {
    public String execute() throws Exception {
      return SUCCESS;
    }
  }

  测试jsp,把它放在webwork-example/下:
  testsendmail.jsp
  <%@ taglib prefix="ww" uri="webwork" %>
  <html>
  <head><title>Test sendmail restul type</title></head>
  <body>
  <form action="testSendmail.action">
    <input type="Submit" value="Submit"/>
  </form>
  </body>
  </html>

打开http://localhost:8080/webwork-example/testsendmail.jsp,提交页面,控制台输出:
sending mail....
      To:user@sina.com
    From:root@sina.com
  Subject:hello,webwork

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics