穷则思变,差则思勤!没有比人更高的山没有比脚更长的路。 ——我们共勉
一个action默认存在一个方法,若一个action有多个方法,就需要用动态方法调用(Dynamic Method Invocation)。文章最后有完整代码。
三种方法可以实现动态方法调用。
①指定method属性
②感叹号方式(官方不推荐)
③使用通配符(官网推荐使用)
1.指定method属性
直接在action中加一个method=”xxx”即可。
<struts>
<package name="demo" namespace="/demo" extends="struts-default">
<action name="begin" class="com.lee.action.DynamicMethodAction" method="hello">
<result name="success">/welcome.jsp</result>
<allowed-methods>hello</allowed-methods>
</action>
</package>
</struts>
访问的URL:http://localhost:8080/aDynamicMethod/demo/begin
2.感叹号方式(官方不推荐)
使用这种方法需要在访问时URL后面加上(!方法名)
用这种方式需要先开启一个开关
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
将此常量设置为true,这种方式才能使用。
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<package name="demo" namespace="/demo" extends="struts-default">
<action name="begin" class="com.lee.action.DynamicMethodAction" >
<result name="method1">/welcome1.jsp</result>
<result name="method2">/welcome2.jsp</result>
<result name="method3">/welcome3.jsp</result>
<allowed-methods>method1,method2,method3</allowed-methods>
</action>
</package>
</struts>
访问的url:
http://localhost:8080/aDynamicMethod/demo/begin!method1
http://localhost:8080/aDynamicMethod/demo/begin!method2
http://localhost:8080/aDynamicMethod/demo/begin!method3
3.使用通配符(官网推荐使用)
首先得关闭开关
<constant name="struts.enable.DynamicMethodInvocation"
value="false" />
在action的name属性中可以使用 * 去命名,那么其他属性中需要使用{1},{2}取出URL的路径。
<action name="begin_*" class="com.lee.action.DynamicMethodAction" method="{1}">
<result name="method1">/welcome1.jsp</result>
<result name="method2">/welcome2.jsp</result>
<result name="method3">/welcome3.jsp</result>
<allowed-methods>method1,method2,method3</allowed-methods>
</action>
begin_* :
如果 * 换成method1,那么name=”begin_method1”。
如果 * 换成method2,那么name=”begin_method2”。
访问的URL就是:http://localhost:8080/aDynamicMethod/demo/begin_method1
或者http://localhost:8080/aDynamicMethod/demo/begin_method2
完整代码
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>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<package name="demo" namespace="/demo" extends="struts-default">
<action name="begin_*" class="com.lee.action.DynamicMethodAction" method="{1}">
<result name="method1">/welcome1.jsp</result>
<result name="method2">/welcome2.jsp</result>
<result name="method3">/welcome3.jsp</result>
<allowed-methods>method1,method2,method3</allowed-methods>
</action>
</package>
</struts>
action代码:
package com.lee.action;
public class DynamicMethodAction {
public String method1() {
return "method1";
}
public String method2() {
return "method2";
}
public String method3() {
return "method3";
}
}
welcome1.jsp代码:
<%@ page language="java" pageEncoding="UTF-8" %>
<%@ page contentType="text/html; charset=UTF-8" %>
<!DOCTYPE html >
<html>
<head>
<meta charset="UTF-8">
<title>动态方法调用</title>
</head>
<body>
<h1>我是动态方法调用</h1>
<h2>method111</h2>
</body>
</html>
web.xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<filter>
<filter-name>ApacheStruts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ApacheStruts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>