1. 程式人生 > >Struts2學習四----------動態方法調用

Struts2學習四----------動態方法調用

rop apach index.jsp 執行 lte mod fin 結構 html4

? 版權聲明:本文為博主原創文章,轉載請註明出處

Struts2動態方法調用

  - 默認:默認執行方法中的execute方法,若指定類中沒有該方法,默認返回success

<package name="default" extends="struts-default" namespace="/">
		<action name="add" class="org.struts.dynamicmethod.action.DynamicMethod">
			<result>/add.jsp</result>					<!-- 若沒有execute方法,默認執行該result -->
			<result name="error">/error.jsp</result>
		</action>
	</package>

  - 指定method屬性:執行method屬性中定義的方法,沒有該方法,頁面報錯

<package name="default" extends="struts-default" namespace="/">
		<action name="add" method="add" class="org.struts.dynamicmethod.action.DynamicMethod">
			<result>/add.jsp</result>
			<result name="error">/error.jsp</result>
		</action>
	</package>

  - 通配符方式:使用*作用通配符,若沒有配置method,默認執行execute,沒有execute方法,默認返回success;

          若配置method,執行method屬性中定義的方法,沒有該方法,則報錯

<package name="default" extends="struts-default" namespace="/">
		<action name="add_*" method="{1}" class="org.struts.dynamicmethod.action.DynamicMethod">
			<result>/{1}.jsp</result>					<!-- 若未配置method屬性,並且沒有execute方法,默認執行該result -->
			<result name="error">/error.jsp</result>
		</action>
	</package>

  - 感嘆號方法:在!(感嘆號)後面指定方法名,需在package中設置strict-method-invocation="false",並且開啟動態方法調用(老版本只需開啟動態方法調用即可)

<package name="default" extends="struts-default" namespace="/" strict-method-invocation="false">
		<action name="add" class="org.struts.dynamicmethod.action.DynamicMethod">
			<result>/add.jsp</result>
			<result name="error">/error.jsp</result>
		</action>
	</package>
	
	<constant name="struts.enable.DynamicMethodInvocation" value="true"/>

實例

1.項目結構

技術分享

2.pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  
  	<modelVersion>4.0.0</modelVersion>
  
	<groupId>org.struts</groupId>
	<artifactId>Struts2-DynamicMethod</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>Struts2-DynamicMethod Maven Webapp</name>
	<url>http://maven.apache.org</url>
	
	<properties>
		<!-- 統一Struts2的版本 -->
		<struts.version>2.5.10</struts.version>
		<!-- 統一源代碼與編譯的編碼格式 -->
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	
	<dependencies>
		<!-- junit -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
		<!-- Struts2 -->
		<dependency>
		    <groupId>org.apache.struts</groupId>
		    <artifactId>struts2-core</artifactId>
		    <version>${struts.version}</version>
		</dependency>
	</dependencies>
  
	<build>
		<finalName>Struts2-DynamicMethod</finalName>
	</build>
	
</project>

3.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0">
  
	<filter>
		<filter-name>struts</filter-name>
		<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>  
	<filter-mapping>
		<filter-name>struts</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
  
</web-app>

4.DynamicMethod.java

package org.struts.dynamicmethod.action;

import com.opensymphony.xwork2.ActionSupport;

public class DynamicMethod extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	public String add() {
		
		return "add";
		
	}

	public String modify() {
		
		return "modify";
		
	}

	public String delete() {
		
		return "delete";
	
	}
	
}

5.struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
	
	<!-- 只有感嘆號動態調用方法時需設置strict-method-invocation="false" -->
	<package name="dynamic" extends="struts-default" namespace="/" strict-method-invocation="false">
		<!-- 默認,無execute方法,執行name="success"的result -->
		<action name="search" class="org.struts.dynamicmethod.action.DynamicMethod">
			<result>/search.jsp</result>
			<result name="error">/error.jsp</result>
		</action>
		<!-- 指定method屬性 -->
		<action name="add" method="add" class="org.struts.dynamicmethod.action.DynamicMethod">
			<result name="add">/add.jsp</result>
			<result name="error">/error.jsp</result>
		</action>
		<!-- 感嘆號方式 -->
		<action name="modify" class="org.struts.dynamicmethod.action.DynamicMethod">
			<result name="modify">/modify.jsp</result>
			<result name="error">/error.jsp</result>
		</action>
		<!-- 通配符方式 -->
		<action name="delete_*" method="{1}" class="org.struts.dynamicmethod.action.DynamicMethod">
			<result name="delete">/{1}.jsp</result>
			<result name="error">/error.jsp</result>
		</action>
	</package>
	
	<!-- 開啟動態方法調用,只有使用感嘆號動態調用方法時需要開啟 -->
	<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
	
</struts>

6.index.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="search">查詢</a><br/>
	<a href="add">新增</a><br/>
	<a href="modify!modify">修改</a><br/>
	<a href="delete_delete">刪除</a>
</body>
</html>

7.delete.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>Insert title here</title>
</head>
<body>
	This is delete.jsp
</body>
</html>

8.add.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>Insert title here</title>
</head>
<body>
	This is add.jsp
</body>
</html>

9.modify.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>Insert title here</title>
</head>
<body>
	This is modify.jsp
</body>
</html>

10.search.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>Insert title here</title>
</head>
<body>
	This is search.jsp
</body>
</html>

11.效果預覽

  11.1 主界面

  技術分享

  11.2 查詢(默認方式,類中無execute方法,默認返回name="success"的result對應的界面)

  技術分享

  11.3 新增(指定method屬性,執行add方法)

  技術分享

  11.4 修改(感嘆號方式,執行modify方法)

  技術分享

  11.5 刪除(通配符方式,執行delete方法)

  技術分享

參考:http://www.imooc.com/video/8999

Struts2學習四----------動態方法調用