struts2雖然繼承了webwork優秀的MVC分離,可是有很多地方讓人百思不得其解!最讓人離譜的是,返回的結果集中居然沒有String,xml這兩種非常常用的類型。還是自己動手,豐衣足食:
第一種方式:使用“PlainText Result”
先看官方文檔對plain text結果的定義:“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.”這是一個純扯蛋的說法。。。貌似感覺只能返回jsp頁面似的,最起碼他誤導了我。
其實使用“PlainText Result” ,返回的結果是未進行格式和編碼定義的字符串。什么意思?就類似于“FreeMarker Result” ,返回一個*.ftl格式的模板,你完全可以在*.ftl寫string,那么結果就是string;也可以在里面寫xml,那么結果就是xml。
舉例如下:
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <package name="example" namespace="/example"
- extends="struts-default">
- <action name="outputXml" method="outxml" class="example.OutputXml">
- <result name="xmlMessage" type="plaintext"></result>
- </action>
- </package>
- </struts>
這里定義了xmlMessage為plain text結果,至于它具體是什么,看下面的Action類:
- public class OutputXml extends ActionSupport {
- public void outxml() throws Exception {
- HttpServletResponse response = ServletActionContext.getResponse();
- response.setContentType("text/xml ");
- PrintWriter pw = response.getWriter();
- pw.print("<cc>cccccc</cc>");
- }
在代碼中,我們顯式的給response定義了ContentType。那么返回去的內容"<cc>cccccc</cc>"就會被接收方按xml進行解析。
而如果需要返回的是String類型,那么contentType = "text/plain”。
如果進一步需要指明編碼,那么contentType = "text/plain; charset=UTF-8";
到這里理解“plain text的結果是未進行格式和編碼定義的字符串”應該就不困難了。基于http的內容傳輸實際都是字符串型,類型的定義是放在response的contentType 中。
第二種方式: 直接擴展struts2的結果集StrutsResultSupport :
代碼如下:
應該很容易懂了。。嘿嘿
- package commons.struts2;
- import java.io.PrintWriter;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.struts2.dispatcher.StrutsResultSupport;
- import com.opensymphony.xwork2.ActionInvocation;
- /**
- * result type for output string in action
- *
- * @author songwei,yaolei <b>Example:</b>
- *
- * <pre>
- * <!-- START SNIPPET: example -->
- * <result name="success" type="string">
- * <param name="stringName">stringName</param>
- * </result>
- * <!-- END SNIPPET: example -->
- * </pre>
- *
- */
- public class StringResultType extends StrutsResultSupport {
- private static final long serialVersionUID = 1L;
- private String contentTypeName;
- private String stringName = "";
- public StringResultType() {
- super();
- }
- public StringResultType(String location) {
- super(location);
- }
- protected void doExecute(String finalLocation, ActionInvocation invocation)
- throws Exception {
- HttpServletResponse response = (HttpServletResponse) invocation
- .getInvocationContext().get(HTTP_RESPONSE);
- // String contentType = (String)
- // invocation.getStack().findValue(conditionalParse(contentTypeName,
- // invocation));
- String contentType = conditionalParse(contentTypeName, invocation);
- if (contentType == null) {
- contentType = "text/plain; charset=UTF-8";
- }
- response.setContentType(contentType);
- PrintWriter out = response.getWriter();
- // String result = conditionalParse(stringName, invocation);
- String result = (String) invocation.getStack().findValue(stringName);
- out.println(result);
- out.flush();
- out.close();
- }
- public String getContentTypeName() {
- return contentTypeName;
- }
- public void setContentTypeName(String contentTypeName) {
- this.contentTypeName = contentTypeName;
- }
- public String getStringName() {
- return stringName;
- }
- public void setStringName(String stringName) {
- this.stringName = stringName;
- }
- }
使用的方法:
1.Action
- package test;
- import com.opensymphony.xwork2.ActionSupport;
- public class MyAction extends ActionSupport{
- String result="abc";
- public String ajax() {
- return "ajaxResponse";
- }
- // getter && setter
- public String getResult() {
- return result;
- }
- public void setResult(String result) {
- this.result = result;
- }
- }
2.定義struts.xml
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <package name="test" extends="struts-default">
- <result-types>
- <result-type name="string" class="test.StringResultType"></result-type>
- </result-types>
- <action name="myAction" class="test.MyAction" >
- <result name="ajaxResponse" type="string">
- <param name="stringName">result</param>
- </result>
- </action>
- </package>
- </struts>
無非也就是將string結果集進行申明,然后給返回“ajaxResponse”的結果綁定變量名。這里定義返回Action的String result變量,即“abc”。
第三種方式:利用Velocity Result、FreeMarker Result
類似第一種方式,這里不再重復