<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    空間站

    北極心空

      BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
      15 Posts :: 393 Stories :: 160 Comments :: 0 Trackbacks
    http://blog.csdn.net/d8111/archive/2008/09/11/2914079.aspx

    struts2雖然繼承了webwork優(yōu)秀的MVC分離,可是有很多地方讓人百思不得其解!最讓人離譜的是,返回的結(jié)果集中居然沒有String,xml這兩種非常常用的類型。還是自己動(dòng)手,豐衣足食:

     

    第一種方式:使用“PlainText Result”

     

        先看官方文檔對plain text結(jié)果的定義:“A result that send the content out as plain text. Usefull typically when needed to display the raw content of a JSP or Html file for example.”這是一個(gè)純扯蛋的說法。。。貌似感覺只能返回jsp頁面似的,最起碼他誤導(dǎo)了我。

     

        其實(shí)使用“PlainText Result” ,返回的結(jié)果是未進(jìn)行格式和編碼定義的字符串。什么意思?就類似于“FreeMarker Result”  ,返回一個(gè)*.ftl格式的模板,你完全可以在*.ftl寫string,那么結(jié)果就是string;也可以在里面寫xml,那么結(jié)果就是xml。

     

       舉例如下:

    1. <?xml version="1.0" encoding="UTF-8" ?>
    2. <!DOCTYPE struts PUBLIC
    3.         "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    4.         "http://struts.apache.org/dtds/struts-2.0.dtd">
    5. <struts>
    6.     <package name="example" namespace="/example"
    7.         extends="struts-default">
    8.         <action name="outputXml"  method="outxml" class="example.OutputXml">
    9.             <result name="xmlMessage" type="plaintext"></result>
    10.         </action>
    11.     </package>
    12. </struts>

    這里定義了xmlMessage為plain text結(jié)果,至于它具體是什么,看下面的Action類:

     

    1. public class OutputXml extends ActionSupport {
    2.     public void outxml() throws Exception {
    3.         HttpServletResponse response = ServletActionContext.getResponse();
    4.         response.setContentType("text/xml ");
    5.         PrintWriter pw = response.getWriter();
    6.         pw.print("<cc>cccccc</cc>");
    7.     }

        在代碼中,我們顯式的給response定義了ContentType。那么返回去的內(nèi)容"<cc>cccccc</cc>"就會(huì)被接收方按xml進(jìn)行解析。

     而如果需要返回的是String類型,那么contentType = "text/plain”。

    如果進(jìn)一步需要指明編碼,那么contentType = "text/plain; charset=UTF-8";

     

        到這里理解“plain text的結(jié)果是未進(jìn)行格式和編碼定義的字符串”應(yīng)該就不困難了。基于http的內(nèi)容傳輸實(shí)際都是字符串型,類型的定義是放在response的contentType 中。

     

     

    第二種方式: 直接擴(kuò)展struts2的結(jié)果集StrutsResultSupport 

     

    代碼如下:

    應(yīng)該很容易懂了。。嘿嘿

    1. package commons.struts2;
    2. import java.io.PrintWriter;
    3. import javax.servlet.http.HttpServletResponse;
    4. import org.apache.struts2.dispatcher.StrutsResultSupport;
    5. import com.opensymphony.xwork2.ActionInvocation;
    6. /**
    7.  * result type for output string in action
    8.  * 
    9.  * @author songwei,yaolei <b>Example:</b>
    10.  * 
    11.  * <pre>
    12.  * <!-- START SNIPPET: example -->
    13.  * <result name="success" type="string">
    14.  *   <param name="stringName">stringName</param>
    15.  * </result>
    16.  * <!-- END SNIPPET: example -->
    17.  * </pre>
    18.  * 
    19.  */
    20. public class StringResultType extends StrutsResultSupport {
    21.     private static final long serialVersionUID = 1L;
    22.     private String contentTypeName;
    23.     private String stringName = "";
    24.     public StringResultType() {
    25.         super();
    26.     }
    27.     public StringResultType(String location) {
    28.         super(location);
    29.     }
    30.     protected void doExecute(String finalLocation, ActionInvocation invocation)
    31.             throws Exception {
    32.         HttpServletResponse response = (HttpServletResponse) invocation
    33.                 .getInvocationContext().get(HTTP_RESPONSE);
    34.         // String contentType = (String)
    35.         // invocation.getStack().findValue(conditionalParse(contentTypeName,
    36.         // invocation));
    37.         String contentType = conditionalParse(contentTypeName, invocation);
    38.         if (contentType == null) {
    39.             contentType = "text/plain; charset=UTF-8";
    40.         }
    41.         response.setContentType(contentType);
    42.         PrintWriter out = response.getWriter();
    43.         // String result = conditionalParse(stringName, invocation);
    44.         String result = (String) invocation.getStack().findValue(stringName);
    45.         out.println(result);
    46.         out.flush();
    47.         out.close();
    48.     }
    49.     public String getContentTypeName() {
    50.         return contentTypeName;
    51.     }
    52.     public void setContentTypeName(String contentTypeName) {
    53.         this.contentTypeName = contentTypeName;
    54.     }
    55.     public String getStringName() {
    56.         return stringName;
    57.     }
    58.     public void setStringName(String stringName) {
    59.         this.stringName = stringName;
    60.     }
    61. }

     

    使用的方法:

    1.Action

    1. package test;
    2. import com.opensymphony.xwork2.ActionSupport;
    3. public class MyAction extends ActionSupport{
    4.     String  result="abc";
    5.     public String ajax() {
    6.         return "ajaxResponse";
    7.     }
    8.     // getter && setter
    9.     public String getResult() {
    10.         return result;
    11.     }
    12.     public void setResult(String result) {
    13.         this.result = result;
    14.     }
    15.     
    16. }

    2.定義struts.xml

    1. <?xml version="1.0" encoding="UTF-8" ?>
    2. <!DOCTYPE struts PUBLIC
    3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    4.     "http://struts.apache.org/dtds/struts-2.0.dtd">
    5. <struts>
    6.     <package name="test" extends="struts-default">
    7.         <result-types>
    8.             <result-type name="string" class="test.StringResultType"></result-type>
    9.         </result-types>
    10.         
    11.         <action name="myAction" class="test.MyAction" >
    12.             <result name="ajaxResponse" type="string">
    13.                 <param name="stringName">result</param>
    14.             </result>
    15.         </action>
    16.     </package>
    17. </struts>

    無非也就是將string結(jié)果集進(jìn)行申明,然后給返回“ajaxResponse”的結(jié)果綁定變量名。這里定義返回Action的String  result變量,即“abc”。

     

     

    第三種方式:利用Velocity Result、FreeMarker Result

    類似第一種方式,這里不再重復(fù)

    posted on 2008-11-24 12:54 蘆葦 閱讀(3373) 評(píng)論(1)  編輯  收藏 所屬分類: Struts

    Feedback

    # Site Promotion 2009-05-18 09:19 Site Promotion
    Good evening. You need only claim the event of your life to make yourself yours. When you truly possess all you have been and done, which may take some time, you are fierce with reality.
    I am from Gabon and learning to read in English, tell me right I wrote the following sentence: "Seo software can help you to acquire targeted traffic interested in your business.Optimize your website get top rankings for."

    Waiting for a reply :o, Ellen.  回復(fù)  更多評(píng)論
      

    主站蜘蛛池模板: 亚洲日韩AV一区二区三区四区| 一二三四影视在线看片免费 | 91视频精品全国免费观看| 亚洲一卡一卡二新区无人区| 亚洲AV无码成人网站久久精品大 | 亚洲成年轻人电影网站www| 亚洲成a人一区二区三区| 免费无码成人AV片在线在线播放| 亚洲一区免费视频| 免费在线中文日本| 精品多毛少妇人妻AV免费久久| 亚洲人成色99999在线观看| 亚洲国产日韩女人aaaaaa毛片在线| 国产亚洲精品无码拍拍拍色欲| 免费大片在线观看网站| 日韩电影免费在线观看视频| 我的小后妈韩剧在线看免费高清版| 一级毛片免费观看| 秋霞人成在线观看免费视频 | 亚洲精品无码专区久久同性男| 毛片免费观看的视频在线| 亚洲免费视频网址| 久久精品一本到99热免费| 免费国产成人午夜在线观看| 一区二区三区AV高清免费波多| 在线观看免费亚洲| 国产精品亚洲天堂| 狠狠综合亚洲综合亚洲色| 精品久久久久亚洲| 免费在线观看亚洲| 免费又黄又爽又猛大片午夜| 美女啪啪网站又黄又免费| 婷婷国产偷v国产偷v亚洲| 免费播放国产性色生活片| 色视频在线观看免费| 猫咪免费人成网站在线观看入口| 国产精品亚洲精品久久精品| 国产亚洲视频在线观看网址 | 亚洲麻豆精品国偷自产在线91| 亚洲国产香蕉人人爽成AV片久久| 亚洲成人一区二区|