ITeye博客

关于struts2如何去掉默认的后缀(.action)

irfen · 2014-02-10 12:00 · java · struts2 · 编程语言 · Markdown

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

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

 

<constant name="struts.action.extension" value="do"/>

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

 

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

 

<constant name="struts.action.extension" value=","/>

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

 

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

 

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

 

发表自本人博客  http://irfen.me

评论(14)

  1. #14 楼 · irfen · 2014-02-19 16:02

    yccn writes:

    默认就可以没有后缀啊

    默认有时候会自动加上.action

  2. #13 楼 · irfen · 2014-02-19 15:59

    string2020 writes:

    楼主,你这是官方的配置方法,还是你自己试出来的

    在struts2的默认常量配置文件中(default.properties,在struts-core-2.3.x.jar的org.apache.struts2下),有这么一段内容

    Used by the DefaultActionMapper

    You may provide a comma separated list, e.g. struts.action.extension=action,jnlp,do

    The blank extension allows you to match directory listings as well as pure action names

    without interfering with static resources, which can be specified as an empty string

    prior to a comma e.g. struts.action.extension=, or struts.action.extension=x,y,z,,

    struts.action.extension=action,,

  3. #12 楼 · irfen · 2014-02-19 15:39

    alvin198761 writes:

    这样子可能会导致很多问题,比如 Servlet不能正常使用,

    这个我们项目里几乎都交给struts2处理的,原生的servlet没有使用,但是如果真的需要用servlet的话可以通过其他配置不拦截这些请求

  4. #11 楼 · irfen · 2014-02-19 15:36

    string2020 writes:

    楼主,在实际生产中这样用过吗?

    我们这里项目就是这样用的,并没有产生像2楼说的上传下载被拦截的情况

  5. #10 楼 · ray_yui · 2014-02-11 16:34

    本來默認就可以沒有

  6. #9 楼 · string2020 · 2014-02-11 12:44

    楼主,你这是官方的配置方法,还是你自己试出来的

  7. #8 楼 · yccn · 2014-02-11 11:21

    默认就可以没有后缀啊

  8. #7 楼 · sweat89 · 2014-02-11 10:31

    alvin198761 writes:

    这样子可能会导致很多问题,比如 Servlet不能正常使用,

    你们的做法是?

  9. #6 楼 · alvin198761 · 2014-02-11 09:04

    这样子可能会导致很多问题,比如 Servlet不能正常使用,

  10. #5 楼 · sbpcx · 2014-02-10 22:45

    谢谢分享。

  11. #4 楼 · bnboo77 · 2014-02-10 15:57

    mark一下。以后估计会碰到。

  12. #3 楼 · string2020 · 2014-02-10 15:44

    楼主,在实际生产中这样用过吗?

  13. #2 楼 · liufangmeng · 2014-02-10 15:38

    这个不建议酱紫做。 因为上传下载可能会被拦截

  14. #1 楼 · songbgi · 2014-02-10 15:18

    mark一下

评论功能维护中,当前仅展示历史评论。