开发者

Spring-MVC: Is it possible to have two url-patterns for one servlet-mapping?

I have both .htm and .xml URLs that I want to be resolved as .jsp fi开发者_运维百科les in my WEB-INF folder. How do I specify that I want the same servlet to handle both *.htm and *.xml URLs?


Adding multiple url-pattern tags in the same mapping works for me using Spring 3.0

<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.

@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
    }
}


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>


I assume you are talking about the <servlet-mapping> element in your "web.xml" file.

The answer is that you can (sort of) by using two <servlet-mapping> elements with different patterns for the same <servlet> element.

Note that is a feature of the Java EE Servlet specification. The associated request dispatching happens before Spring gets a look at the requests.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜