Struts2中動態的result

UserAction.java

package com.zhang;

public class UserAction
{
	private String num;
	private int type;
	
	public String getNum()
	{
		return num;
	}
	public void setNum(String num)
	{
		this.num = num;
	}
	public int getType()
	{
		return type;
	}
	public void setType(int type)
	{
		this.type = type;
	}
	
	public String execute() throws Exception
	{
		if (type == 1)
			num="/Hello.jsp";
		else if (type == 2)
			num="Hello2.jsp";
		return "success";
	}
	
}

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.devMode" value="true" />
	<package name="default" namespace="/" extends="struts-default">
		      
       <action name="user" class="com.zhang.UserAction">
            <result >${num}</result>
        </action>
        
    </package>
    
</struts>

Hello.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head></head>
  
  <body>
     Hello <br>
  </body>
</html>

調用時:

http://localhost:8080/Struts2_01/user?type=1 調用Hello.jsp

http://localhost:8080/Struts2_01/user?type=2 調用Hello2.jsp


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