Struts(七):action配置文件之通配符映射

  • 通配符映射:一个Web应用可能有成百上千个action声明,可以使用struts提供的通配符映射机制把多个彼此相似的映射关系简化为一个映射关系。
  • 通配符映射规则:
  1. 若找到多个匹配,没有通配符的那个将胜出;
  2. 若指定的动作不存在,Struts将会尝试把这URI与任何一个包含着通配符*的动作名进行匹配;
  3. 被通配符匹配的URI字符串的字串可以通过{1}、{2}来引用。{1}匹配第一个子字符串,{2}匹配第二个字串。。。;
  4. {0}匹配整个URI
  5. 若Struts找到的带有通配符的匹配不止一个,则按先后顺序进行匹配;
  6. *可以匹配零个或多个字符,但不包括/字符,如果想把/字符包括在内,需要使用**。如果需要对某个字符进行转义,需要使用\。

如果我们需要一个user的添加,修改,删除,查询操作的struts2的实现,一般情况下我们在struts.xml文件中,需要配置action的映射关系,最直接的配置关系是这样子:

 

 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 
 6 <struts>
 7     <!-- action路径的扩展名:*.action,*.do,无扩展名 -->
 8     <constant name="struts.action.extension" value="action,do," />
 9     <constant name="struts.enable.DynamicMethodInvocation" value="false" />
10     <constant name="struts.devMode" value="false" />
11 
12     <package name="default" namespace="/" extends="struts-default">
13         <action name="user-create" class="com.dx.actions.TestAction" method="create">
14             <result name="create-success">/WEB-INF/testResult.jsp</result>
15         </action>
16         <action name="user-update" class="com.dx.actions.TestAction" method="update">
17             <result name="update-success">/WEB-INF/testResult.jsp</result>
18         </action>
19         <action name="user-delete" class="com.dx.actions.TestAction" method="delete">
20             <result name="delete-success">/WEB-INF/testResult.jsp</result>
21         </action>
22         <action name="user-search" class="com.dx.actions.TestAction" method="search">
23             <result name="search-success">/WEB-INF/testResult.jsp</result>
24         </action>
25     </package>
26     
27 </struts>

 

我们的index.jsp是一个导航页面:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <a href="user-create">user-create</a><br/>
11     <a href="user-update">user-update</a><br/>
12     <a href="user-delete">user-delete</a><br/>
13     <a href="user-search">user-search</a><br/>
14 </body>
15 </html>

testResult.jsp是一个测试显示通用页面:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     ${requestScope.actionName}
11 </body>
12 </html>

我们的com.dx.actions.TestAction是这样来写:

 1 package com.dx.actions;
 2 
 3 import org.apache.struts2.ServletActionContext;
 4 
 5 public class TestAction {
 6     public String create(){
 7         ServletActionContext.getRequest().setAttribute("actionName", "create-success");
 8         return "create-success";
 9     }
10     
11     public String update(){
12         ServletActionContext.getRequest().setAttribute("actionName", "update-success");
13         return "update-success";
14     }
15     
16     public String delete(){
17         ServletActionContext.getRequest().setAttribute("actionName", "delete-success");
18         return "delete-success";
19     }
20     
21     public String search(){
22         ServletActionContext.getRequest().setAttribute("actionName", "search-success");
23         return "search-success";
24     }
25 }

测试:

访问:http://localhost:8080/MyStruts003/user-create,页面显示:user-create

访问:http://localhost:8080/MyStruts003/user-update,页面显示:user-update

访问:http://localhost:8080/MyStruts003/user-delete,页面显示:user-delete

访问:http://localhost:8080/MyStruts003/user-search,页面显示:user-search

一切都是我们预期的结果。

但是,我们学习了action配置通配符映射关系之后,发现上边实际上就可以简化很多。

 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 
 6 <struts>
 7     <!-- action路径的扩展名:*.action,*.do,无扩展名 -->
 8     <constant name="struts.action.extension" value="action,do," />
 9     <constant name="struts.enable.DynamicMethodInvocation" value="false" />
10     <constant name="struts.devMode" value="false" />
11 
12     <package name="default" namespace="/" extends="struts-default">
13     <!-- 
14         <action name="user-create" class="com.dx.actions.TestAction" method="create">
15             <result name="create-success">/WEB-INF/testResult.jsp</result>
16         </action>
17         <action name="user-update" class="com.dx.actions.TestAction" method="update">
18             <result name="update-success">/WEB-INF/testResult.jsp</result>
19         </action>
20         <action name="user-delete" class="com.dx.actions.TestAction" method="delete">
21             <result name="delete-success">/WEB-INF/testResult.jsp</result>
22         </action>
23         <action name="user-search" class="com.dx.actions.TestAction" method="search">
24             <result name="search-success">/WEB-INF/testResult.jsp</result>
25         </action>
26          -->
27          <action name="user-*" class="com.dx.actions.TestAction" method="{1}">
28             <result name="{1}-success">/WEB-INF/testResult.jsp</result>
29         </action>
30     </package>
31     
32 </struts>

那如果我们就是要对user-list同样规则符合以上规则的页面,但是需要跳转到另外一个页面\WEB-INF\testListResult.jsp进行配置,该怎么配置呢?

index.jsp中添加一行链接html:

1     <a href="user-list">user-search</a><br/>

在\WEB-INF下添加页面:testListResult.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <h4>This is testListResult.jsp page</h4>
11     ${requestScope.actionName}
12 </body>
13 </html>

修改com.dx.actions.TestAction类,添加list方法:

1 public String list(){
2         ServletActionContext.getRequest().setAttribute("actionName", "list-success");
3         
4         return "list-success";
5     }

修改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 
 6 <struts>
 7     <!-- action路径的扩展名:*.action,*.do,无扩展名 -->
 8     <constant name="struts.action.extension" value="action,do," />
 9     <constant name="struts.enable.DynamicMethodInvocation" value="false" />
10     <constant name="struts.devMode" value="false" />
11 
12     <package name="default" namespace="/" extends="struts-default">
13     <!-- 
14         <action name="user-create" class="com.dx.actions.TestAction" method="create">
15             <result name="create-success">/WEB-INF/testResult.jsp</result>
16         </action>
17         <action name="user-update" class="com.dx.actions.TestAction" method="update">
18             <result name="update-success">/WEB-INF/testResult.jsp</result>
19         </action>
20         <action name="user-delete" class="com.dx.actions.TestAction" method="delete">
21             <result name="delete-success">/WEB-INF/testResult.jsp</result>
22         </action>
23         <action name="user-search" class="com.dx.actions.TestAction" method="search">
24             <result name="search-success">/WEB-INF/testResult.jsp</result>
25         </action>
26          -->
27          <action name="user-*" class="com.dx.actions.TestAction" method="{1}">
28             <result name="{1}-success">/WEB-INF/testResult.jsp</result>
29         </action>
30          <action name="user-list" class="com.dx.actions.TestAction" method="list">
31             <result name="list-success">/WEB-INF/testListResult.jsp</result>
32         </action>
33     </package>
34     
35 </struts>

访问页面:http://localhost:8080/MyStruts003/user-list

测试结果:

基于上边的例子,我们是把user-list同user-*位置互换

1  <action name="user-list" class="com.dx.actions.TestAction" method="list">
2             <result name="list-success">/WEB-INF/testListResult.jsp</result>
3         </action>
4          <action name="user-*" class="com.dx.actions.TestAction" method="{1}">
5             <result name="{1}-success">/WEB-INF/testResult.jsp</result>
6         </action>

,测试的结果和上边的结果是一样的。

 

posted @ 2016-06-21 23:24  cctext  阅读(632)  评论(0编辑  收藏  举报