Java web.xml 中 servlet 映射的 URL 模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/491773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:18:52  来源:igfitidea点击:

URL Pattern for servlet mapping in web.xml

javajspservletsjakarta-eeweb.xml

提问by Fernando Briano

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.

我需要在 web.xml 中使用此 URL 映射的解决方法来创建带有字母的 URL,后跟“_”,后跟字母数字字符的任意组合。

I want to map a servlet to something like this:

我想将一个 servlet 映射到这样的东西:

/something_*

Instead of:

代替:

/something/*

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

为不同的 JSP 使用不同的“东西”。例子:

/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 answertells me I can't do this within web.xml, so maybe there's some workaround.

但这似乎不起作用。 这个答案告诉我我不能在 web.xml 中做到这一点,所以也许有一些解决方法。

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

我不知道这些信息是否重要,但我在我的项目中使用了 JBoss 和 Struts2。

采纳答案by Tom Hawtin - tackline

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

将 servlet 映射到包含目录。在该 servlet 中,拆分 URL 路径并转发到适当的命名 servlet

回答by Jaymelson Galang

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

为什么不试试 Spring MVC 框架。Spring 可以提供您想要的 url 映射。

@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

观看此Spring MVC 框架教程