可以在struts2-core-{version}.jar中找到struts-default.xml,裡面列舉瞭當前可以使用的所有result-type以及對應的class
此處是struts2.2.3的
view plaincopy to clipboardprint?
<result-types>
<result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
<result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
<result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
<result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
<result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
<result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
<result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
<result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
<result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
<result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
</result-types>
<result-types>
<result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
<result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
<result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
<result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
<result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
<result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
<result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
<result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
<result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
<result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
</result-types>
1.chain
該類型是請求轉發給其他的action,如果為jsp則會報錯
需要註意的就是與redirect的區別,請求轉發是還在當前請求,
而redirect會響應一次瀏覽器然後瀏覽器再根據響應請求重定向的資源,註意看url的變化就明白瞭!
下面是apache給的示例,這個比較簡單,沒什麼說的,
view plaincopy to clipboardprint?<package name="public" extends="struts-default"> <!– Chain creatAccount to login, using the default parameter –> <action name="createAccount" class="…"> <result type="chain">login</result> </action> <action name="login" class="…"> <!– Chain to another namespace –> <result type="chain"> <param name="actionName">dashboard</param> <param name="namespace">/secure</param> </result> </action> </package> <package name="secure" extends="struts-default" namespace="/secure"> <action name="dashboard" class="…"> <result>dashboard.jsp</result> </action> </package> <package name="public" extends="struts-default">
<!– Chain creatAccount to login, using the default parameter –>
<action name="createAccount" class="…">
<result type="chain">login</result>
</action>
<action name="login" class="…">
<!– Chain to another namespace –>
<result type="chain">
<param name="actionName">dashboard</param>
<param name="namespace">/secure</param>
</result>
</action>
</package>
<package name="secure" extends="struts-default" namespace="/secure">
<action name="dashboard" class="…">
<result>dashboard.jsp</result>
</action>
</package>2.dispatcher請求轉發視圖資源,比如jsp,html,如果請求action會找不到資源,請求轉發的問題看1中的說明apache給的示例。。也沒啥說的view plaincopy to clipboardprint?<result name="success" type="dispatcher"> <param name="location">foo.jsp</param> </result> <result name="success" type="dispatcher">
<param name="location">foo.jsp</param>
</result>
3.httpheader這個挺有意思的,可以隨便更改響應狀態比如下面的這個示例view plaincopy to clipboardprint?<action name="test" class="com.iss.action.TestAction"> <result name="success" type="httpheader"> <param name="status">400</param> </result> </action> <action name="test" class="com.iss.action.TestAction">
<result name="success" type="httpheader">
<param name="status">400</param>
</result>
</action>當action返回SUCCESS的時候,會將響應狀態修改為400,客戶端錯誤的請求,還有其他的狀態可以自行嘗試,比如為100時,瀏覽器會在請求一段時間之後繼續當前頁面,500則為服務器內部錯誤等等具體為什麼會產生這樣的結果,當然得把源碼翻出來瞭下面是HttpHeaderResult的一些成員變量,private的可以作為param的name屬性view plaincopy to clipboardprint?private static final long serialVersionUID = 195648957144219214L; /** The default parameter */ public static final String DEFAULT_PARAM = "status"; private boolean parse = true; private Map<String,String> headers; private int status = -1; private int error = -1; private String errorMessage; private static final long serialVersionUID = 195648957144219214L;
/** The default parameter */
public static final String DEFAULT_PARAM = "status";
private boolean parse = true;
private Map<String,String> headers;
private int status = -1;
private int error = -1;
private String errorMessage;下面是具體執行操作過程,可以看到response.setStatus等操作view plaincopy to clipboardprint?public void execute(ActionInvocation invocation) throws Exception { HttpServletResponse response = ServletActionContext.getResponse(); ValueStack stack = ActionContext.getContext().getValueStack(); if (status != -1) { response.setStatus(status); } else if (error != -1) { if (errorMessage != null) { String finalMessage = parse ? TextParseUtil.translateVariables( errorMessage, stack) : errorMessage; response.sendError(error, finalMessage); } else response.sendError(error); } if (headers != null) { for (Iterator iterator = headers.entrySet().iterator(); iterator.hasNext();) { Map.Entry entry = (Map.Entry) iterator.next(); String value = (String) entry.getValue(); String finalValue = parse ? TextParseUtil.translateVariables(value, stack) : value; response.addHeader((String) entry.getKey(), finalValue); } } } public void execute(ActionInvocation invocation) throws Exception {
HttpServletResponse response = ServletActionContext.getResponse();
ValueStack stack = ActionContext.getContext().getValueStack();
if (status != -1) {
response.setStatus(status);
} else if (error != -1) {
if (errorMessage != null) {
String finalMessage = parse ? TextParseUtil.translateVariables(
errorMessage, stack) : errorMessage;
response.sendError(error, finalMessage);
} else
response.sendError(error);
}
if (headers != null) {
for (Iterator iterator = headers.entrySet().iterator();
iterator.hasNext();) {
Map.Entry entry = (Map.Entry) iterator.next();
String value = (String) entry.getValue();
String finalValue = parse ? TextParseUtil.translateVariables(value, stack) : value;
response.addHeader((String) entry.getKey(), finalValue);
}
}
}4.redirect
重定向操作,與請求轉發的區別看1的介紹
讓客戶端請求另外的網絡資源,可以為action,也可以為視圖資源
下面是apache的示例
view plaincopy to clipboardprint?
<result name="success" type="redirect">
<param name="location">foo.jsp</param>
<param name="parse">false</param>
</result>
<result name="success" type="redirect">
<param name="location">foo.jsp</param>
<param name="parse">false</param>
</result>
5.redirectAction
重定向至Action,與redirect的區別找瞭好久,但是也沒發現比較權威的說明
最明顯的區別當然是redirectAction隻能請求action,如果請求視圖資源會報錯
然後還有個小區別就是redirectAction會為url添加.action後綴而redirect不會
有說redirectAction無法通過url傳參,但是我測試時完全可以通過request獲取到,
此處區別如果有哪位知道的麻煩告知,Thank you!
下面是apache的傳參的示例
view plaincopy to clipboardprint?
<package name="passingRequestParameters" extends="struts-default" namespace="/passingRequestParameters">
<– Pass parameters (reportType, width and height) –>
<!–
The redirect-action url generated will be :
/genReport/generateReport.action?reportType=pie&width=100&height=100
–>
<action name="gatherReportInfo" class="…">
<result name="showReportResult" type="redirect-action">
<param name="actionName">generateReport</param>
<param name="namespace">/genReport</param>
<param name="reportType">pie</param>
<param name="width">100</param>
<param name="height">100</param>
</result>
</action>
</package>
<package name="passingRequestParameters" extends="struts-default" namespace="/passingRequestParameters">
<– Pass parameters (reportType, width and height) –>
<!–
The redirect-action url generated will be :
/genReport/generateReport.action?reportType=pie&width=100&height=100
–>
<action name="gatherReportInfo" class="…">
<result name="showReportResult" type="redirect-action">
<param name="actionName">generateReport</param>
<param name="namespace">/genReport</param>
<param name="reportType">pie</param>
<param name="width">100</param>
<param name="height">100</param>
</result>
</action>
</package>
6.stream
這個返回類型主要用作下載文件或者在瀏覽器上顯示PDF等文檔
此處給一個顯示PDF文檔示例
項目web.xml中
view plaincopy to clipboardprint?
<mime-mapping>
<extension>pdf</extension>
<mime-type>application/pdf</mime-type>
</mime-mapping>
<mime-mapping>
<extension>pdf</extension>
<mime-type>application/pdf</mime-type>
</mime-mapping>
struts.xml中
view plaincopy to clipboardprint?
<action name="test" class="com.iss.action.TestAction">
<result name="success" type="stream">
<param name="contentType">application/pdf</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">filename="a.pdf"</param>
</result>
</action>
<action name="test" class="com.iss.action.TestAction">
<result name="success" type="stream">
<param name="contentType">application/pdf</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">filename="a.pdf"</param>
</result>
</action>
TestAction.java中
view plaincopy to clipboardprint?
public class TestAction extends ActionSupport
{
private InputStream inputStream;
public InputStream getInputStream()
{
return inputStream;
}
@Override
public String execute() throws Exception
{
inputStream = new FileInputStream("xxxxx/a.pdf");//要下載或顯示的文件路徑
return SUCCESS;
}
}
public class TestAction extends ActionSupport
{
private InputStream inputStream;
public InputStream getInputStream()
{
return inputStream;
}
@Override
public String execute() throws Exception
{
inputStream = new FileInputStream("xxxxx/a.pdf");//要下載或顯示的文件路徑
return SUCCESS;
}
}需要註意的地方是struts.xml中inputName的值要與TestAction中的屬性名相同,在此處就是inputStream
7.plainText
響應以plain形式返回給客戶端
截取源碼的最重要的一部分
view plaincopy to clipboardprint?
if (charset != null) {
response.setContentType("text/plain; charset="+charSet);
}
else {
response.setContentType("text/plain");
}
if (charset != null) {
response.setContentType("text/plain; charset="+charSet);
}
else {
response.setContentType("text/plain");
}
8.freemarker,velocity,xslt
沒有嘗試過,有興趣的可以嘗試下
作者“周紹禹-whuiss”