structs2學習(四):動態生成結果集

structs.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>
	<constant name="struts.devMode" value="true" /><!-- 開發模式,修改xml文件後不需要重啓tomcat,立即生效 -->
	
	<package name="actions" namespace="/actions" extends="struts-default">
        <!-- global-results 是指公用的一個結果集 -->
        <global-results>
        	<result name="query">/helloquery.jsp</result>
        </global-results>
        <action name="helloadd" class="com.test.TestAction" method="add">
            <result name="success">${r}</result><!-- 動態生成結果集,在Action中爲r賦值,${r}用來取r的值, -->
            <result name="del">${r}</result>
        </action>
	
    </package>
</struts>

TestAction

public class TestAction extends ActionSupport {
	private int age;
	private String r;
	

	public String getR() {
		return r;
	}

	public void setR(String r) {
		this.r = r;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	
	public String add() throws Exception {
		System.out.println(age);
		if(age==1){
			r="/helloadd.jsp";//爲r賦值
		}else if(age==2){
			r="/hellodel.jsp";
		}
			return "success";
		
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章