Struts2 无后缀action请求

本文介绍了如何配置Struts2以实现无后缀名访问Action。通过在`struts.properties`中设置`struts.action.extension`为`,`,可以使得URL如`http://localhost:8080/test`直接访问Action。同时,提到了在`web.xml`中配置拦截器路径`/*`以支持无后缀请求,并讨论了可能出现的问题及其解决方案。" 110531988,9351719,自动微分基础与实践,"['Python', '神经网络', '自动微分', 'TensorFlow', '数据结构']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.youkuaiyun.com/jiangjunshow

               

struts.properties 中默认 设置为:

### Used by the DefaultActionMapper
### You may provide a comma separated list, e.g. struts.action.extension=action,jnlp,do
struts.action.extension=action

以action为后缀,注释掉,

web.xml中配置拦截为 /* 即可无后缀访问


struts2是可以配置默认的后缀名的,如http://localhost:8080/test.action,这个是默认的,但是也可以通过配置去修改这个.action为别的。

这里是通过一个常量配置改变的。

 

Java代码   收藏代码
  1. <constant name="struts.action.extension" value="do"/>  

 这样的就会变为http://localhost:8080/test.do来访问。

 

但是我很不喜欢有后缀,百度了很多大家的结论一般都是使用SmartUrls和urlrewritefilter这两个解决,但是我只是想把后缀去掉而已,就让我增加别的组件这个实在很不爽,最后终于找到了个方法:

 

Java代码   收藏代码
  1. <constant name="struts.action.extension" value=","/>  

 在这里把value配置为,就可以使用http://localhost:8080/test完成访问了

 

把这段注释加上吧,大家可以看看

 

Java代码   收藏代码
  1. ### Used by the DefaultActionMapper  
  2. ### You may provide a comma separated list, e.g. struts.action.extension=action,jnlp,do  
  3. ### The blank extension allows you to match directory listings as well as pure action names  
  4. ### without interfering with static resources, which can be specified as an empty string  
  5. ### prior to a comma e.g. struts.action.extension=, or struts.action.extension=x,y,z,,  
  6. struts.action.extension=action,,  


如果将Struts2的filter-mapping配置成

 
  
< filter-mapping >       < filter-name > struts2 </ filter-name >       < url-pattern > /* </ url-pattern >   </ filter-mapping >

的话,struts2将可以处理无后缀的action请求,如

/demo/hello.action 和/demo/hello 是同样的效果。

这样有时会出现一些问题,将有一些组件映射为无后缀的请求拦截了,如/demo/dwr 请显示对外公布的js方法列表,在struts2拦截后确显示

 
  
There is no Action mapped for namespace / and action name dwr

这种情况下可以在struts.properties中配置

 
  
struts.action.excludePattern = /dwr.*,/xxx.*
属性值是以逗号分隔的正则表达式。           

分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.youkuaiyun.com/jiangjunshow

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值