1. 程式人生 > >Struts2-結果和結果型別

Struts2-結果和結果型別

結果

在struts中,action封裝請求引數,處理請求,處理結束後僅僅返回一個字串,這個字串就是結果(檢視資源的名字),在struts.xml檔案中,配置了檢視資源。

  1. result節點:
    1. name指定檢視資源的名稱,對應action的返回字元type
    2. type指定結果型別
  2. param節點(result的子節點):不同結果型別可指定不同的引數。

全域性結果與區域性結果

<global-results>
            <result name=""></result>
            <result name=""
></result> </global-results>

全域性的結果型別,這個結果是全域性作用的,如果action返回了和全域性結果相同的檢視名稱,首先會在本action的區域性查詢結果,沒有匹配的話,才會查詢全域性結果中是否有。

動態結果

動態結果就是使用萬用字元或者OGNL表示式來定位檢視資源。
例如:
1.

<action name="CRUD_*">
            <result>/WEB-INF/{1}.jsp</result>
        </action>

這種方式就是使用萬用字元來匹配資源名稱達到動態結果的目的。
2.

<action name="MyAction" class="com.tuxianchao.MyAction">
            <result>/WEB-INF/${target}.jsp</result>
        </action>

這種方式,就是使用OGNL表示式,action需要target屬性,這個屬性值機會決定實際的檢視資源。

結果型別

Struts支援多種檢視型別,當action處理處理完成請求後,就會根據配置檔案中配置的結果型別類來決定使用哪種檢視資源來呈現處理結果。

struts提供了許多預設的結果型別,在struts-default.xml中:

<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-type name="postback" class="org.apache.struts2.dispatcher.PostbackResult" />
        </result-types>

也可以自定義結果型別,實現com.opensymphony.xwork2.Result介面,然後再配置檔案中新增自定義的結果型別,然後就可以使用了

struts內建支援的結果型別

  1. dispatcher:預設的結果型別,指定jsp作為檢視資源。
  2. redirect:用於重定向到其他資源(jsp,html…)的結果型別。
  3. redirectAction:用於重定向到其他action的結果型別。
  4. freemarker:指定freemarker模板作為檢視的結果型別。
  5. velocity:指定velocity模板作為檢視的結果型別。
  6. chain:action鏈式處理的結果型別。
  7. plainText:直接輸出原始碼的結果型別。
  8. httpheader:用於控制特殊HTTP的行為的結果型別。
  9. xslt:用於XML/XSTL整合的結果型別。
  10. stream: 返回一個流,可用於檔案下載。

plainText結果型別

配置結果型別為plainText:
struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />

    <package name="default" namespace="/" extends="struts-default">
        <action name="index">
            <!--指定結果型別為plainText -->
            <result type="plainText">
                <!-- 設定為plainText,請求的時候會直接輸出原始碼 -->
                <!-- location指定檢視資源的位置 -->
                <param name="location">/WEB-INF/jsp/index.jsp</param>
                <!-- 指定引數charSet -->
                <param name="charSet">UTF-8</param>
            </result>
        </action>

        <action name="">
            <result name="檢視名字">
                <param name="location">返回結果的路徑</param>
            </result>
        </action>
    </package>
</struts>

index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h1>This is index.jsp</h1>
    <h1>這裡是 index.jsp</h1>
</body>
</html>

結果直接輸出了原始碼(將Content-Type設定成了text/plain):
這裡寫圖片描述

redirect結果型別

將結果型別配置為redirect:

<result type="redirect">
                <!-- redirect和redirectAction都屬於重定向,是新發一個請求,原先的引數都會丟失,所以不能重定向到WEB-INF目錄下的資源 -->
                <param name="location">/redirect.jsp</param>
                <param name="charSet">UTF-8</param>
            </result>

redirectAction

redirectAction和redirect類似,redirect結果型別是重定向到JSP,html等資源,而redirectAction是重定向到一個action。