标签:struts2 通配符
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:指定的是struts2框架底层提供的default.properties资源文件中配置的常量 *value:指定的是配置常量的值 *struts.xml文件中,配置的常量的值会覆盖底层提供的default.properties资源文件中配置的常量的值 配置struts2框架中请求连接的后缀名,如果是指定多个的话,用逗号隔开 如果在struts.xml中和struts.properties资源文件中同时进行配置,struts.properties的配置起作用 * 因为常量可以在多个配置文件中进行定义,所以我们需要了解下struts2加载常量的搜索顺序: 1 struts-default.xml 2 struts-plugin.xml 3 struts.xml 4 struts.properties(自己创建) 5 web.xml --> <!-- --> <!-- <constant name="struts.action.extension" value="do,love"></constant> --> <!-- 配置国际化资源文件修改时,是否重新加载。默认是false,为不加载。true为加载 --> <!-- <constant name="struts.i18n.reload" value="true"></constant> --> <!-- 配置struts2框架的配置文件修改时,是否重新加载,默认是false为不加载,true是加载 --> <!-- <constant name="struts.configuration.xml.reload" value="true"></constant> --> <!-- 配置struts2框架的模式 *默认是false,是生产模式 *true是开发模式,需要更多的调试信息 ### includes: ### - struts.i18n.reload = true ### - struts.configuration.xml.reload = true --> <constant name="struts.devMode" value="true"></constant> <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant> <!-- 引入自定义配置文件 --> <include file="cn/itcast/primer/struts_primer.xml"></include> <include file="cn/itcast/resulttype/struts_resulttype.xml"></include> <include file="cn/itcast/pattern/struts_pattern.xml"></include> </struts>
局部xml:struts_pattern.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> <package name="pattern" namespace="/pattern" extends="struts-default"> <default-action-ref name="bookAction"></default-action-ref> <!-- 全局结果类型 --> <global-results> <result name="success">/pattern/successGlobal.jsp</result> </global-results> <!-- *struts2框架默认执行action类中的execute方法 *在action标签里的method属性:指定的是要执行的action类中的那个方法 --> <!-- <action name="bookAction" class="cn.itcast.pattern.BookAction" method="execute"> <result name="success"> /pattern/success.jsp </result> <result name="add"> /pattern/BookAction.jsp </result> </action> --> <!-- <action name="a_add" class="cn.itcast.pattern.BookAction" method="add" > <result name="success"> /pattern/success.jsp </result> <result name="add"> /pattern/BookAction.jsp </result> </action> <action name="b_add" class="cn.itcast.pattern.BookAction" method="add" > <result name="success"> /pattern/success.jsp </result> <result name="add"> /pattern/BookAction.jsp </result> </action> <action name="c_add" class="cn.itcast.pattern.BookAction" method="add" > <result name="success"> /pattern/success.jsp </result> <result name="add"> /pattern/BookAction.jsp </result> </action>--> <!-- 以上配置可以改写如下: --> <!-- ‘*‘就是通配符,匹配的是不一样的内容 --> <!-- <action name="*_add" class="cn.itcast.pattern.BookAction" method="add" > <result name="success"> /pattern/success.jsp </result> <result name="add"> /pattern/BookAction.jsp </result> </action> --> <!--<action name="BookAction_add" class="cn.itcast.pattern.BookAction" method="add"> <result name="success">/pattern/success.jsp</result> <result name="add">/pattern/BookAction.jsp</result> </action> <action name="UserAction_add" class="cn.itcast.pattern.UserAction" method="add"> <result name="success">/pattern/success.jsp</result> <result name="add">/pattern/UserAction.jsp</result> </action> --> <!-- 以上配置可以改写如下: --> <!-- {1}表示的是匹配"*"通配符的第一个字串 --> <!-- <action name="*_add" class="cn.itcast.pattern.{1}" method="add"> <result name="success">/pattern/success.jsp</result> <result name="add">/pattern/{1}.jsp</result> </action> --> <!-- <action name="BookAction_add" class="cn.itcast.pattern.BookAction" method="add"> <result name="add">/pattern/BookAction.jsp</result> </action> <action name="BookAction_delete" class="cn.itcast.pattern.BookAction" method="delete"> <result name="success">/pattern/success.jsp</result> </action> <action name="UserAction_add" class="cn.itcast.pattern.UserAction" method="add"> <result name="add">/pattern/UserAction.jsp</result> </action> <action name="UserAction_delete" class="cn.itcast.pattern.UserAction" method="delete"> <result name="success">/pattern/success.jsp</result> </action> --> <!-- 以上配置可以改写如下: --> <!-- {2}匹配就是"*" 通配符的第二个字串 --> <!-- {0}匹配的就是"*" 通配符的整个串 --> <!-- <action name="*_*" class="cn.itcast.pattern.{1}" method="{2}"> <result name="success">/pattern/success.jsp</result> <result name="add">/pattern/{1}.jsp</result> </action>--> <!-- 动态方法调用: *页面中请求连接:namespace+actionName+"!"+执行方法名 *在struts.xml文件中,不用配置method属性,而是通过页面的连接动态执行指定方法 *动态方法调用,不经常使用.经常使用的是通配符 *动态方法调用,需要开启,默认是关闭状态(下面是开启的语句,卸载struts.xml) <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant> --> <!-- <action name="BookAction" class="cn.itcast.pattern.BookAction"> <result name="success">/pattern/success.jsp</result> <result name="add">/pattern/BookAction.jsp</result> </action> --> <action name="*_*" class="cn.itcast.pattern.{1}" method="{2}"> <result name="success">/pattern/success.jsp</result> <result name="add">/pattern/{1}.jsp</result> </action> <action name="BookAction_find" class="cn.itcast.pattern.BookAction" method="find"> <!-- 如果局部结果类型和全局结果类型,同事存在,局部结果类型会覆盖了全局的结果类型 局部结果类型和全局结果类型的作用范围, *全局结果类型:作用于整个package *局部结果类型:作用于某个action --> <!-- <result name="success">/pattern/success.jsp</result> --> </action> </package> </struts>
BookAction.java
package cn.itcast.pattern; import com.opensymphony.xwork2.ActionSupport; public class BookAction extends ActionSupport { @Override public String execute() throws Exception { // TODO Auto-generated method stub System.out.println("BookAction ******** execute()"); return "success"; } /* * 在struts2框架中action是多实例的比单实例安全,单实例慢线程不安全 */ public BookAction(){ System.out.println("BookAction 的构造方法"); } /* * 自定义方法: * *:由public来修饰 * *:必须是String返回类型 * *:不能传参数 * *:方法名自定义 * *总之,一句话:除了方法名与execute()不一样,其他所有内容都一样 * * * * */ public String add() { System.out.println("BookAction ********** add()"); return "add"; } public String delete() { System.out.println("BookAction ********** delete()"); return "success"; } public String find() { System.out.println("BookAction ********** find()"); return "success"; } }
UserAction.java
package cn.itcast.pattern; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport { @Override public String execute() throws Exception { // TODO Auto-generated method stub System.out.println("UserAction ******** execute()"); return "success"; } /* * 自定义方法: * *:由public来修饰 * *:必须是String返回类型 * *:不能传参数 * *:方法名自定义 * *总之,一句话:除了方法名与execute()不一样,其他所有内容都一样 * * * * */ public String add() { System.out.println("UserAction ********** add()"); return "add"; } public String delete() { System.out.println("UserAction ********** delete()"); return "success"; } }
test.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> 访问BookAction_add 测试action标签中的method属性<br> <a href="${pageContext.request.contextPath}/pattern/bookAction.action"> 测试</a><br> <br> <br> 通配符映射示例(1):<br> <a href="${pageContext.request.contextPath}/pattern/a_add.action"> 通配符映射示例(1)</a><br> <a href="${pageContext.request.contextPath}/pattern/b_add.action"> 通配符映射示例(1)</a><br> <a href="${pageContext.request.contextPath}/pattern/c_add.action"> 通配符映射示例(1)</a><br> <br> <br> <br> 通配符映射示例(2):<br> <a href="${pageContext.request.contextPath}/pattern/BookAction_add.action"> 图书</a><br> <a href="${pageContext.request.contextPath}/pattern/UserAction_add.action">用户</a><br> <br> <br> 通配符映射示例(3):<br> <a href="${pageContext.request.contextPath}/pattern/BookAction_add.action">图书添加</a><br> <a href="${pageContext.request.contextPath}/pattern/BookAction_delete.action">图书删除</a><br> <br> <br> <a href="${pageContext.request.contextPath}/pattern/UserAction_add.action">用户添加</a><br> <a href="${pageContext.request.contextPath}/pattern/UserAction_delete.action">用户删除</a><br> <br> <br> 动态方法调用!形式:<br> <a href="${pageContext.request.contextPath}/pattern/BookAction!add.action">图书添加</a><br> <a href="${pageContext.request.contextPath}/pattern/BookAction!delete.action">图书删除</a><br> <br><br> 使用通配符定义action<br> <a href="${pageContext.request.contextPath}/pattern/BookAction_add.action">图书添加</a><br> <a href="${pageContext.request.contextPath}/pattern/BookAction_delete.action">图书删除</a><br> <br><br> 测试全局和局部的action<br> <a href="${pageContext.request.contextPath}/pattern/BookAction_find.action">图书查找</a><br> </body> </html>
标签:struts2 通配符
原文地址:http://vincent1992.blog.51cto.com/4621739/1583481