---
title: "《研磨struts2》第五章 Result 之 5.4 使用通配符 "
author: "研磨struts2"
date: "2012-05-02T08:43:46Z"
tags: ["Struts2"]
categories: ["企业架构"]
slug: "struts2-result-5-4"
draft: false
lastmod: "2012-05-02T08:43:46Z"
id: 1506619
url: "https://rubbing-struts2.iteye.com/blog/1506619"
---

## 5.4  使用通配符

### []()5.4.1  使用通配符

前面学过，可以在定义&lt;action&gt;元素的name属性的时候使用通配符，使这个&lt;action&gt;元素不仅仅可以匹配一个URL，还可以匹配一类URL。而action的class属性和method属性能使用通配符的值。如果使用以下&lt;action&gt;配置：

 

java代码：

[查看](http://sishuok.com/forum/blogPost/list/4053.html)[复制到剪贴板](http://sishuok.com/forum/blogPost/list/4053.html)[打印](http://sishuok.com/forum/blogPost/list/4053.html)

1. &lt;action name="\*\_\*" class="cn.javass.action.action.{1}Action" method="{2}"&gt;
2. &lt;result name="toWelcome"&gt;/s2impl/welcome.jsp&lt;/result&gt;
3. &lt;/action&gt;

在上面的配置中：

       name属性的值中“\*”代表长度不为0的任意字符串，因此，它可以响应的action只需要名称中间有一个下划线即可。

       如果使用HelloWorld\_create.action作为访问的action名称的话，struts.xml中action名称为HelloWorld\_create，第一个通配符匹配HelloWorld，第二个通配符匹配create。因此，由cn.javass.action.action.HelloWorldAction的create方法来响应。

       那么，对于&lt;result&gt;元素，能不能也使用&lt;action&gt;元素的name属性定义的通配符呢？答案是可以的，假如有如下的配置：

 

java代码：

[查看](http://sishuok.com/forum/blogPost/list/4053.html)[复制到剪贴板](http://sishuok.com/forum/blogPost/list/4053.html)[打印](http://sishuok.com/forum/blogPost/list/4053.html)

1. &lt;action name="\*\_\*\_\*\_\*" class="cn.javass.action.action.{1}Action" method="{2}"&gt;
2. &lt;result name="{3}"&gt;/${folder}/{4}.jsp&lt;/result&gt;
3. &lt;/action&gt;

当然，此时的Action也要做相应的变化，给Action添加一个login的方法，同样要在这个方法里面为folder赋值。示例代码如下：

 

java代码：

[查看](http://sishuok.com/forum/blogPost/list/4053.html)[复制到剪贴板](http://sishuok.com/forum/blogPost/list/4053.html)[打印](http://sishuok.com/forum/blogPost/list/4053.html)

01. public class HelloWorldAction extends ActionSupport {
02. private String account;
03. private String password;
04. private String submitFlag;
05. private String folder;
06. public void setFolder(String folder){
07. this.folder = folder;
08. }
09. public String getFolder(){
10. return folder;
11. }
12. public String login(){
13. System.out.println("进行Login的处理");
14. this.folder = "s2impl";
15. return "toLoginOk";
16. }
17. public String execute() throws Exception {
18. this.businessExecute();
19. this.folder = "s2impl";
20. return "toWelcome";
21. }
22. /\**
23. * 示例方法，表示可以执行业务逻辑处理的方法
24. \*/
25. public void businessExecute(){
26. System.out.println("用户输入的参数为==="+"account="+account+",password="+password+",submitFlag="+submitFlag);
27. }
28. //属性对应的getter/setter方法，省略了
29. }

struts.xml的配置发生了变化，尤其是Action名称配置发生了变化，那么登录页面也需要相应的变化，示例代码如下：

 

java代码：

[查看](http://sishuok.com/forum/blogPost/list/4053.html)[复制到剪贴板](http://sishuok.com/forum/blogPost/list/4053.html)[打印](http://sishuok.com/forum/blogPost/list/4053.html)

1. &lt;form action="/helloworld/HelloWorld\_login\_toLoginOk\_welcome.action" method="post"&gt;
2. &lt;input type="hidden" name="submitFlag" value="login"/&gt;
3. 账号:&lt;input type="text" name="account"&gt;&lt;br&gt;
4. 密码:&lt;input type="password" name="password"&gt;&lt;br&gt;
5. &lt;input type="submit" value="提交"&gt;
6. &lt;/form&gt;

注意：上面配置的“HelloWorld\_login\_toLoginOk\_welcome.action”，对应到struts.xml的时候，会被分成四个参数来匹配。其中：${1}对应HelloWorld，${2}对应login，${3}对应toLoginOk，${4}对应welcome。

       去测试一下看看，是不是好用呢。其实，上面使用通配符的配置，同如下的配置是等价的，示例如下：

 

java代码：

[查看](http://sishuok.com/forum/blogPost/list/4053.html)[复制到剪贴板](http://sishuok.com/forum/blogPost/list/4053.html)[打印](http://sishuok.com/forum/blogPost/list/4053.html)

1. &lt;action name="HelloWorld\_login\_toLoginOk\_welcome" class="cn.javass.action.action.HelloWorldAction" method="login"&gt;
2. &lt;result name="toLoginOk"&gt;/${folder}/welcome.jsp&lt;/result&gt;
3. &lt;/action&gt;

也就是把相应的参数设置到对应的位置。

 

私塾在线网站原创《[研磨struts2](http://sishuok.com/forum/blogCategory/showByCategory.html?categories_id=81&user_id=5948)》系列

转自请注明出处：【[http://sishuok.com/forum/blogPost/list/0/4053.html](http://sishuok.com/forum/blogPost/list/0/4053.html)】

欢迎访问[http://sishuok.com](http://sishuok.com/)获取更多内容