URL Pattern for servlet mapping in web.xml

匿名 (未验证) 提交于 2019-12-03 02:20:02

问题:

I need a workaround with this URL mapping in web.xml to create URLs with a letter, followed by a "_" followed by any combination of alphanumeric characters.

I want to map a servlet to something like this:

/something_* 

Instead of:

/something/* 

Using different "somethings" for different JSP's. Example:

/search_Something-I-searched-for 

I tried using:

  <servlet-mapping>     <servlet-name>MyServlet</servlet-name>     <url-pattern>/something_*</url-pattern>   </servlet-mapping> 

But this doesn't seem to work. This answer tells me I can't do this within web.xml, so maybe there's some workaround.

I don't know if this information is important, but I'm using JBoss and Struts2 in my project.

回答1:

Map a servlet to the containing directory. Inside that servlet, take apart the URL path and forward to the appropriate named servlet.



回答2:

Why not try Spring MVC Framework. Spring can offer that url mapping you want.

@RequestMapping(value="/something_{name}", method=RequestMethod.GET) public String demo(@PathVariable(value="name") String name, ModelMap map) {  String something = name;  // Do manipulation  return "something"; // Forward to something.jsp } 

Watch this Spring MVC Framework Tutorial



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!