Struts2的處理結果(三)——動態配置結果,struts2動態

來源:互聯網
上載者:User

Struts2的處理結果(三)——動態配置結果,struts2動態

Struts2的處理結果(三)

    ——動態配置結果

1.使用運算式文法

樣本:

<struts>    <constant name="struts.enable.DynamicMethodInvocation" value="false" />    <constant name="struts.devMode" value="true" />    <package name="package_a" extends="struts-default">        <!-- 此action匹配所有為test_*.action模式的請求 -->        <action name="test_*" class="testAction.TestAction" method="{1}">            <result name="error">error.jsp</result>            <result>{1}.jsp</result>                    </action>    </package></struts>

 現有test_success.action請求,符合test_*的格式,匹配的方法為success()方法,其邏輯視圖映射的物理視圖資源為success.jsp。

 

 

2.由Action的屬性值確定物理視圖資源

  通過使用${屬性名稱.屬性名稱....}的方式確定物理視圖資源。

樣本:

(1)寫一個Action類,其中包含一條屬性name:

 

package testAction;import com.opensymphony.xwork2.ActionSupport;public class Test1Action extends ActionSupport{    //封裝的請求參數    private String name;    //setter、getter方法    public void setName(String name){        this.name = name;    }    public String getName(){        return this.name;    }    //處理請求的邏輯方法    public String execute()throws Exception{        if(name.equals("jiagoushi")){            return SUCCESS;        }else{            return ERROR;        }    }}

 

(2)提交Action的頁面welcome.jsp

<form action="test1" method="post">    <input type="text" name="name">    <input type="submit" value="提交"></form>

(3)物理視圖資源jiagoushi.jsp

(4)配置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="package_a" extends="struts-default">        <action name="test1" class="testAction.Test1Action">            <result name="error">error.jsp</result>            <result>${name}.jsp</result>        </action>    </package></struts>

通過了Action的屬性name找到了對應的物理視圖資源。

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.