---
title: "Struts2 urlrewrite 多个参数 outbound-rule"
author: "cy913417"
date: "2012-05-15T15:06:14Z"
tags: []
categories: ["Web前端"]
slug: "struts2-urlrewrite-outbound-rule"
draft: false
lastmod: "2012-05-15T15:06:14Z"
id: 1530256
url: "https://it456.iteye.com/blog/1530256"
---

struts2 在页面结合其自身的标签， 使用urlrewrite 相当方便，鄙人也是初次使用。google一下 一搜一箩筐，基本都是a抄b b复制c c 粘贴a  d收藏c,(再次吐槽那些只会ctrl+c and ctrl+v的XXX)完了都是只有

```
<rule>
 		 <note>ContentInfo_contentId_page</note>
        <from>^/app/ContentInfo/([0-9]+)_([0-9]+)</from>
        <to>/app/ContentInfo.action?contentId=$1&amp;intPage=$2</to>
 </rule> 
```

然后对于&lt;outbound-rule&gt;&lt;outbound-rule/&gt; 标签的介绍几乎全都一提略过。

 其实该标签也就是上面规则的逆运算，将页面中符合outbound-rule 的url 转换成您自定义的 觉得美观的url,

 多次尝试发现，

```
<outbound-rule>
		<from>/app/ContentInfo.action\?contentId=([0-9]+)&amp;intPage=([0-9]+)</from>
        <to>/app/ContentInfo/$1_$2</to>
</outbound-rule>
```

 

 这里虽然将"&" 在XML中做了转义，但是 仍然无效，因为struts在服务端生成url时已将用于连接参数的“&”转义为“&amp;”了 所以在rewrite时他只是一个字符串了。所以上面的规则修改为

```
<outbound-rule>
		<from>/app/ContentInfo.action\?contentId=([0-9]+)(.*)intPage=([0-9]+)</from>
        <to>/app/ContentInfo/$1_$3</to>
</outbound-rule>
```

   
用(.\*) 匹配“&amp;”  至此，struts下urlrewrite 重写服务端地址的多个参数传入问题顺利得到解决，

 

注：此处还有另外一个方法就是先重写一个无参的相同名称的url 然后在按规则匹配参数，也能解决，就是麻烦点。