java Spring-MVC:一个 servlet 映射是否可以有两个 url 模式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4965755/
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
Spring-MVC: Is it possible to have two url-patterns for one servlet-mapping?
提问by ktm5124
I have both .htm and .xml URLs that I want to be resolved as .jsp files in my WEB-INF folder. How do I specify that I want the same servlet to handle both *.htm and *.xml URLs?
我有 .htm 和 .xml URL,我想在我的 WEB-INF 文件夹中将它们解析为 .jsp 文件。我如何指定我想要同一个 servlet 来处理 *.htm 和 *.xml URL?
回答by Ryan Thomas
Adding multiple url-pattern tags in the same mapping works for me using Spring 3.0
使用 Spring 3.0 在同一个映射中添加多个 url-pattern 标签对我有用
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/<url-pattern>
<url-pattern>*.htm</url-pattern>
<url-pattern>*.html</url-pattern>
<url-pattern>*.xml</url-pattern>
</servlet-mapping>
In regards to making your controllers resolve them to the view objects (.jsp) that you desire you can do so using controllers that extend a controller class and follow a specific naming convention or you can use annotation driven controllers. Below is an example of annotation driven controller.
关于让您的控制器将它们解析为您希望的视图对象 (.jsp),您可以使用扩展控制器类并遵循特定命名约定的控制器来实现,或者您可以使用注释驱动的控制器。下面是一个注解驱动控制器的例子。
@Controller
public class Controller {
@RequestMapping(value={"/","/index","/index.htm","index.html"})
public ModelAndView indexHtml() {
// RETURN VIEW (JSP) FOR HTM FILE
}
@RequestMapping(value="/index.xml")
public ModelAndView indexXML() {
// RETURN VIEW (JSP) FOR XML FILE
}
}
回答by asgs
Yes, you can very well do that.
是的,你可以很好地做到这一点。
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.xml</url-pattern>
</servlet-mapping>
回答by Stephen C
I assume you are talking about the <servlet-mapping>
element in your "web.xml" file.
我假设您正在谈论<servlet-mapping>
“web.xml”文件中的元素。
The answer is that you can (sort of) by using two <servlet-mapping>
elements with different patterns for the same <servlet>
element.
答案是您可以(某种程度上)<servlet-mapping>
为同一<servlet>
元素使用具有不同模式的两个元素。
Note that is a feature of the Java EE Servlet specification. The associated request dispatching happens before Spring gets a look at the requests.
请注意,这是 Java EE Servlet 规范的一个特性。相关的请求分派发生在 Spring 查看请求之前。