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 resolve as .jsp files in the WEB-INF folder. How to indicate that I want the same servlet to handle .htm and * .xml URLs?

+3
source share
3 answers

Adding multiple url template tags to the same display works for me with 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>

As for your controllers to allow them for objects of the form (.jsp) you want, you can do this using controllers that extend the controller class and follow a specific naming convention, or you can use annotation controlled controllers. The following is an example of an annotation controlled 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
    }
}
+7

, .

<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>
+3

, <servlet-mapping> "web.xml".

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

Note that this is a feature of the Java EE Servlet specification. A related request submission occurs before Spring scans the requests.

+1
source

Source: https://habr.com/ru/post/1791403/


All Articles