How to configure Spring MVC DispatcherServlet to avoid extension URLs?

I have an MVC Spring project (4.1.6.RELEASE) with a controller that maps to /home , but my problem is that it is also called for paths like /home.html or /home.do

My configuration:

web.xml:

  <servlet> <servlet-name>main</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>main</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> 

main servlet.xml:

  <mvc:annotation-driven /> <mvc:resources mapping="/resources/**" location="/resources/" /> <!-- ... --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> 

HomeController.java:

 @Controller @RequestMapping({"/", "/home"}) public class HomeController { @RequestMapping(method = RequestMethod.GET) public String doGet(Model model) { // ... return "home"; } } 

As suggested in similar questions:

I tried to add the following configurations:

  <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> <property name="useDefaultSuffixPattern" value="false" /> </bean> 

and

  <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"> <property name="useSuffixPatternMatch" value="false" /> <property name="useRegisteredSuffixPatternMatch" value="false" /> </bean> 

but without success.

When I debug a DispatcherServlet , I see that the RequestMappingHandlerMapping and DefaultAnnotationHandlerMapping did not set the above comments to the false properties.

enter image description here

It seems like a simple configuration should do this, but I am missing something that I cannot recognize.

How to configure DispatcherServlet to avoid file extensions in displayed paths?

Thanks in advance.

+6
source share
1 answer

According to Spring, the doc config should be under mvc:annotation-driven , for example.

  <mvc:annotation-driven> <mvc:path-matching suffix-pattern="false" /> </mvc:annotation-driven> 

as described in docs

Whether to use a suffix pattern (".*") When matching patterns with Queries. If the method associated with "/users" enabled, also matches "/users.*" . The default value is true .

+3
source

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


All Articles