1. 程式人生 > >struts2 2.5(動態Action struts.xml web.xml 配置)

struts2 2.5(動態Action struts.xml web.xml 配置)

動態Action

要使用動態Action的時候,需要在struts.xml中加入

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

這句程式碼起到了說明動態Action的作用

還需要宣告動態Action中做使用的方法,程式碼如下:

    		<allowed-methods>add,update</allowed-methods>

以下是一個動態Action的小例子

動態Action程式碼如下:

package action;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String info;
	public String add() throws Exception{
		info = "新增使用者資訊";
		return "add";
	}
	public String update() throws Exception{
		info = "更新使用者資訊";
		return "update";
	}
	public String getInfo(){
		return info;
	}
	public void setInfo(String info){
		this.info = info;
	}
}

struts.xml

<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
	"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
	<!-- 宣告包 -->
    <package name="myPackage" extends="struts-default">
    	<!-- 定義action -->
    	<action name="userAction" class="action.UserAction">
    		<!-- 新增成功的對映頁面 -->
    		<result name="add">user_add.jsp</result>
    		<!-- 更新成功的對映頁面 -->
    		<result name="update">user_update.jsp</result>
    		<allowed-methods>add,update</allowed-methods>
    	</action>
    </package>
</struts>

三個jsp頁面(index.jsp  user_add.jsp  user_update.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>首頁</title>
</head>
<body>
	<a href="userAction!add">新增使用者</a>
	<br>
	<a href="userAction!update">更新使用者</a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>新增使用者資訊</title>
</head>
<body>
	<font color="red"> 
		<s:property value="info"/>
	</font>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>更新使用者資訊</title>
</head>
<body>
	<font color="red"> 
		<s:property value="info"/>
	</font>
</body>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd"
	id="WebApp_ID" version="3.1">
	<display-name>動態Action</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
	<!-- Struts2過濾器 -->
	<filter>
		<!-- 過濾器名稱 -->
		<filter-name>struts2</filter-name>
		<!-- 過濾器類 -->
		<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<!-- Struts2過濾器對映 -->
	<filter-mapping>
		<!-- 過濾器名稱 -->
		<filter-name>struts2</filter-name>
		<!-- 過濾器對映 -->
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>