1. 程式人生 > >Struts2DynamicResult動態接收結果集

Struts2DynamicResult動態接收結果集

UserAction.java

package org.bigjava.web.action;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

	private String dr;
	private int userType;	// 使用者型別 1:管理員 0:普通使用者
	
	public String getDr() {
		return dr;
	}

	public void setDr(String dr) {
		this.dr = dr;
	}
public int getUserType() { return userType; } public void setUserType(int userType) { this.userType = userType; } @Override public String execute() throws Exception { if (userType == 0) { dr = "/admin.jsp"; } else if (userType == 1) { dr = "/manager.jsp"; } return SUCCESS;
} }

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<package name="user" extends="struts-default" namespace="/">
		<action name=
"login"
class="org.bigjava.web.action.UserAction">
<result> <!-- ${}ONGL表示式,${dr}獲取UserAction值棧中dr的值 --> ${dr}; </result> </action> </package> </struts>

login.jsp

<body>
  	<h2>普通使用者0,管理員1</h2>
    <form action="login" method="post">
    	<input type="text" name="usertype" />
    	<input type="submit" />
    </form>
  </body>

manager.jsp

<body>
    This is Administrator page. <br>
</body>

admin.jsp

<body>
    This is user page. <br>
</body>