struts2-通配符和动态方法调用(代码演示)

struts2

    --通配符和动态方法调用

1、jsp

  test.jsp

 1 <%@ page language="java" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
 2 <%@ taglib uri="/struts-tags"   prefix="s"%>
 3 <html>
 4   <head>
 5     <title>My JSP 'index.jsp' starting page</title>
 6     </head>
 7   <body>
 8      访问BookAction_add 测试action标签中的method属性<br> 
 9     <a href="${pageContext.request.contextPath}/pattern/bookAction.action"> 测试</a><br>
10     <br> 
11     <br>
12     
13      
14      通配符映射示例(1):<br>
15     <a href="${pageContext.request.contextPath}/pattern/a_add.action"> 通配符映射示例(1)</a><br>
16     <a href="${pageContext.request.contextPath}/pattern/b_add.action"> 通配符映射示例(1)</a><br>
17     <a href="${pageContext.request.contextPath}/pattern/c_add.action"> 通配符映射示例(1)</a><br>
18     <br> 
19     <br>
20     <br>
21     
22      通配符映射示例(2):<br>
23     <a href="${pageContext.request.contextPath}/pattern/BookAction_add.action"> 图书</a><br>
24     <a href="${pageContext.request.contextPath}/pattern/UserAction_add.action">用户</a><br>
25      <br>
26      <br>
27      
28      通配符映射示例(3):<br>
29     <a href="${pageContext.request.contextPath}/pattern/BookAction_add.action">图书添加</a><br>
30     <a href="${pageContext.request.contextPath}/pattern/BookAction_delete.action">图书删除</a><br>
31      <br>
32      <br>
33     <a href="${pageContext.request.contextPath}/pattern/UserAction_add.action">用户添加</a><br>
34     <a href="${pageContext.request.contextPath}/pattern/UserAction_delete.action">用户删除</a><br>
35     <br>
36      <br>
37    
38    
39      动态方法调用!形式:<br>
40     <a href="${pageContext.request.contextPath}/pattern/BookAction!add.action">图书添加</a><br>
41     <a href="${pageContext.request.contextPath}/pattern/BookAction!delete.action">图书删除</a><br>
42    
43    
44      <br><br>
45      使用通配符定义action<br>
46     <a href="${pageContext.request.contextPath}/pattern/BookAction_add.action">图书添加</a><br>
47     <a href="${pageContext.request.contextPath}/pattern/BookAction_delete.action">图书删除</a><br>
48     
49      <br><br>
50      测试全局和局部的action<br>
51     <a href="${pageContext.request.contextPath}/pattern/BookAction_find.action">图书查找</a><br>
52  
53   </body>
54 </html>

 

  seccess.jsp

<%@ page language="java" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<%@ taglib uri="/struts-tags"   prefix="s"%>
<html>
  <head>
    <title>My JSP 'index.jsp' starting page</title>
    </head>
  <body>
          局部的success.jsp
  </body>
</html>

  BookAction.jsp

<%@ page language="java" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<%@ taglib uri="/struts-tags"   prefix="s"%>
<html>
  <head>
    <title>My JSP 'index.jsp' starting page</title>
    </head>
  <body>
    <form action="${pageContext.request.contextPath}/pattern/xxxAction.action"  name="form1"  method="post">
             图书的名称:<input type="text" name="bookName">
           <input type="submit" value="提交">
    </form>
  </body>
</html>

  UserAction.jsp

<%@ page language="java" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<%@ taglib uri="/struts-tags"   prefix="s"%>
<html>
  <head>
    <title>My JSP 'index.jsp' starting page</title>
    </head>
  <body>
    <form action="${pageContext.request.contextPath}/pattern/xxxAction.action"  name="form1"  method="post">
             用户名:<input type="text" name="username">
           <input type="submit" value="提交">
    </form>
  </body>
</html>

  successGlobal.jsp

<%@ page language="java" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<%@ taglib uri="/struts-tags"   prefix="s"%>
<html>
  <head>
    <title>My JSP 'index.jsp' starting page</title>
    </head>
  <body>
          全局successGlobal.jsp
  </body>
</html>

2、xml

  struts.xml

1 <?xml version="1.0" encoding="UTF-8"?>
2    <!DOCTYPE struts PUBLIC
3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
4     "http://struts.apache.org/dtds/struts-2.3.dtd">
5 <struts>
6     <!-- 配置动态方法调用,设置成不开启。默认是true是开启状态;false是不开启状态 -->
7     <constant name="struts.enable.DynamicMethodInvocation" value="false"></constant>
8     <include file="cn/zengfansheng/struts/pattern/struts_pattern.xml"></include><!-- 这里用/不用(.)点 -->
9 </struts>

  struts_pattern.xml

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <!DOCTYPE struts PUBLIC
  3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  4     "http://struts.apache.org/dtds/struts-2.3.dtd">
  5 <struts>
  6     <package name="pattern" namespace="/pattern" extends="struts-default">
  7         
  8         <!--   一、
  9             * struts2框架运行时,默认执行action类中的execute()方法 
 10             * 在action标签里的method属性:指定的是要执行action类中的哪个方法
 11         -->
 12         <!-- <action name="bookAction" class="cn.itcast.pattern.BookAction" method="add">
 13             <result name="success">/pattern/success.jsp</result>
 14             <result name="add">/pattern/BookAction.jsp</result>
 15         </action> -->
 16         
 17         <!--     二、
 18                  通配符映射示例(1):
 19             <a href="${pageContext.request.contextPath}/pattern/a_add.action"> 通配符映射示例(1)</a><br>
 20                 <a href="${pageContext.request.contextPath}/pattern/b_add.action"> 通配符映射示例(1)</a><br>
 21                 <a href="${pageContext.request.contextPath}/pattern/c_add.action"> 通配符映射示例(1)</a><br>
 22          -->
 23         <!-- <action name="a_add" class="cn.itcast.pattern.BookAction" method="add">
 24             <result name="success">/pattern/success.jsp</result>
 25             <result name="add">/pattern/BookAction.jsp</result>
 26         </action>
 27         <action name="b_add" class="cn.itcast.pattern.BookAction" method="add">
 28             <result name="success">/pattern/success.jsp</result>
 29             <result name="add">/pattern/BookAction.jsp</result>
 30         </action>
 31         <action name="c_add" class="cn.itcast.pattern.BookAction" method="add">
 32             <result name="success">/pattern/success.jsp</result>
 33             <result name="add">/pattern/BookAction.jsp</result>
 34         </action> -->
 35         <!-- 以上配置可以改写如下: -->
 36         <!-- "*"就是通配符,匹配的是不一样的内容 -->
 37         <!-- <action name="*_add" class="cn.itcast.pattern.BookAction" method="add">
 38             <result name="success">/pattern/success.jsp</result>
 39             <result name="add">/pattern/BookAction.jsp</result>
 40         </action> -->
 41         
 42         <!--        三、
 43                   通配符映射示例(2):<br>
 44                 <a href="${pageContext.request.contextPath}/pattern/BookAction_add.action"> 图书</a><br>
 45                 <a href="${pageContext.request.contextPath}/pattern/UserAction_add.action">用户</a><br>
 46          -->
 47         <!-- <action name="BookAction_add" class="cn.itcast.pattern.BookAction" method="add">
 48             <result name="success">/pattern/success.jsp</result>
 49             <result name="add">/pattern/BookAction.jsp</result>
 50         </action>
 51         <action name="UserAction_add" class="cn.itcast.pattern.UserAction" method="add">
 52             <result name="success">/pattern/success.jsp</result>
 53             <result name="add">/pattern/UserAction.jsp</result>
 54         </action> -->
 55         <!-- 以上配置可以改写如下: -->
 56         <!-- {1}表示的是匹配"*"通配符的第一个子串 -->
 57         <!-- <action name="*_add" class="cn.itcast.pattern.{1}" method="add">
 58             <result name="success">/pattern/success.jsp</result>
 59             <result name="add">/pattern/{1}.jsp</result>
 60         </action> -->
 61         
 62         <!--
 63              四、
 64               通配符映射示例(3):<br>
 65                 <a href="${pageContext.request.contextPath}/pattern/BookAction_add.action">图书添加</a><br>
 66                 <a href="${pageContext.request.contextPath}/pattern/BookAction_delete.action">图书删除</a><br>
 67                  <br>
 68                  <br>
 69                 <a href="${pageContext.request.contextPath}/pattern/UserAction_add.action">用户添加</a><br>
 70                 <a href="${pageContext.request.contextPath}/pattern/UserAction_delete.action">用户删除</a><br>
 71               
 72           -->
 73         <!-- <action name="BookAction_add" class="cn.itcast.pattern.BookAction" method="add">
 74             <result name="add">/pattern/BookAction.jsp</result>
 75         </action>
 76         <action name="BookAction_delete" class="cn.itcast.pattern.BookAction" method="delete">
 77             <result name="success">/pattern/success.jsp</result>
 78         </action>
 79         <action name="UserAction_add" class="cn.itcast.pattern.UserAction" method="add">
 80             <result name="add">/pattern/UserAction.jsp</result>
 81         </action>
 82         <action name="UserAction_delete" class="cn.itcast.pattern.UserAction" method="delete">
 83             <result name="success">/pattern/success.jsp</result>
 84         </action> -->
 85         <!-- 以上配置可以改写如下: -->
 86         <!-- {2}匹配就是"*"通配符的第二个子串 -->
 87         <!-- {0}匹配的是"*"通配符的整个串 -->
 88         <!-- <action name="*_*" class="cn.itcast.pattern.{1}" method="{2}">
 89             <result name="success">/pattern/success.jsp</result>
 90             <result name="add">/pattern/{1}.jsp</result>
 91         </action> -->
 92         
 93         <!--
 94              五、
 95                   动态方法调用!形式:<br>
 96                 <a href="${pageContext.request.contextPath}/pattern/BookAction!add.action">图书添加</a><br>
 97                 <a href="${pageContext.request.contextPath}/pattern/BookAction!delete.action">图书删除</a><br>
 98             
 99                 动态方法调用:
100                     * 页面中请求连接:namespace+actionName+"!"+执行方法名
101                     * 在struts.xml文件中,不用配置method属性,而是通过页面的连接动态执行指定方法
102                     * 动态方法调用,不经常使用。经常使用的是通配符
103                     * 动态方法调用,默认是开启状态
104          -->
105          <!-- 以上配置可以改写如下: -->
106         <!-- <action name="BookAction" class="cn.itcast.pattern.BookAction">
107             <result name="success">/pattern/success.jsp</result>
108             <result name="add">/pattern/BookAction.jsp</result>
109         </action> -->
110         
111         <!--  
112              六、
113                   使用通配符定义action<br>
114                 <a href="${pageContext.request.contextPath}/pattern/BookAction_add.action">图书添加</a><br>
115                 <a href="${pageContext.request.contextPath}/pattern/BookAction_delete.action">图书删除</a><br>
116           -->
117         <!-- <action name="*_*" class="cn.itcast.pattern.{1}" method="{2}">
118             <result name="success">/pattern/success.jsp</result>
119             <result name="add">/pattern/{1}.jsp</result>
120         </action> -->
121         
122         <!--
123              七、
124                   测试全局和局部的action<br>
125                     <a href="${pageContext.request.contextPath}/pattern/BookAction_find.action">图书查找</a><br>  
126          -->
127         <action name="BookAction_find" class="cn.itcast.pattern.BookAction" method="find">
128             <!-- 
129                 如果局部结果类型和全局结果类型,同时存在
130                     * 局部结果类型会覆盖了全局的结果类型
131                 
132                 局部结果类型和全局结果类型的作用范围:
133                     * 全局结果类型:作用于整个package
134                     * 局部结果类型:作用于某个action
135              -->
136             <result name="success">/pattern/success.jsp</result>
137         </action>
138         
139         <!-- 
140             八、配置全局结果类型 -->
141         <global-results>
142             <result name="success">/pattern/successGlobal.jsp</result>
143         </global-results>
144         
145     </package>
146 </struts> 

3、java

   BookAction.java

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class BookAction extends ActionSupport {

	@Override
	public String execute() throws Exception {
		System.out.println("BookAction ********* execute()");
		return "success";
	}
	
	/*
	 * 在struts2框架中,action是多实例
	 */
	public BookAction() {
		System.out.println("BookAction的构造方法");
	}

	/*
	 * 自定义方法:
	 * 		* 由public来修饰的
	 * 		* 必须是String返回类型
	 * 		* 不能传参数
	 * 		* 方法名自定义
	 * 
	 * 	* 总之,一句话:除了方法名与execute()不一样,其他所有内容都一样。
	 * 
	 */
	public String add() throws Exception {
		System.out.println("BookAction ********* add()");
		return "add";
	}
	
	public String delete() throws Exception {
		System.out.println("BookAction ********* delete()");
		return "success";
	}
	
	public String find() throws Exception {
		System.out.println("BookAction ********* find()");
		return "success";
	}
	
}

   UserAction.java

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class UserAction extends ActionSupport {

	@Override
	public String execute() throws Exception {
		System.out.println("UserAction ********* execute()");
		return "success";
	}
	
	/*
	 * 自定义方法:
	 * 		* 由public来修饰的
	 * 		* 必须是String返回类型
	 * 		* 不能传参数
	 * 		* 方法名自定义
	 * 
	 * 	* 总之,一句话:除了方法名与execute()不一样,其他所有内容都一样。
	 * 
	 */
	public String add() throws Exception {
		System.out.println("UserAction ********* add()");
		return "add";
	}
	
	public String delete() throws Exception {
		System.out.println("UserAction ********* delete()");
		return "success";
	}
	
}

 

posted @ 2013-05-15 00:17  hacket520  阅读(386)  评论(0编辑  收藏  举报