Spring mvc website as root ("/")

I want to map the spring mvc controller to the root ( /** ) path (and not a subfolder such as "/ something"), making exceptions with mvc:resources (open to another method).

It should be the ABC of this structure, but it seems to be very complex material to ask about it.

My app-servlet.xml has these obvious exceptions:

 <mvc:resources mapping="/favicon.ico" location="/favicon.ico" /> <mvc:resources mapping="/robots.txt" location="/robots.txt" /> 

And I have this controller:

 import java.util.Date; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping("/**") public class MainController { @RequestMapping(method = RequestMethod.GET) public String service(final HttpServletRequest request) { final String servlet_path = request.getServletPath(); System.out.println(String.format("%s %s", new Date().toString(), servlet_path)); return "test"; } } 

Now when I press "/" or "/ test" or "/ test / page", I get the output, for example:

 Fri Aug 03 00:22:12 IDT 2012 / Fri Aug 03 00:22:13 IDT 2012 /favicon.ico 

.. cm.? service() is called for /favicon.ico , even if it is explicitly excluded!

Now I assume that there is some β€œpriority” for @Controller in XML, however, how do I get the exception working?

A simple requirement is on the "/" website.

PS This answer answers a very similar question.

One more note: this question is not about the tomcat context.

+6
source share
2 answers

The problem is that the base HandlerMapping registered in <mvc:resources has a very low priority compared to that registered in <mvc:annotation-driven/> . If you just need to answer "/" something, the best way is probably to have a different @RequestMapping than /** , instead say that it has the value /home and define something on these lines:

<mvc:view-controller path="/" view-name="home" />

If this does not work, the only other option is to lower the priority of the base handlerMapping <mvc:resources handler, which can be done by explicitly defining HandlerMapping - a little complicated, but can be done.

Update Here is a possible configuration:

Try it first:

 <mvc:resources mapping="/favicon.ico" location="/favicon.ico" order="0"/> <mvc:resources mapping="/robots.txt" location="/robots.txt" order="0"/> 

If this does not work, change <mvc:annotation-driven/> to something along these lines for Spring 3.1.x:

 <bean name="handlerAdapter" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="webBindingInitializer"> <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer"> <property name="conversionService" ref="conversionService"></property> <property name="validator"> <bean class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"> <property name="providerClass" value="org.hibernate.validator.HibernateValidator"></property> </bean> </property> </bean> </property> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"></bean> <bean class="org.springframework.http.converter.StringHttpMessageConverter"></bean> <bean class="org.springframework.http.converter.ResourceHttpMessageConverter"></bean> <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"></bean> <bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter"></bean> <bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"></bean> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean> </list> </property> </bean> <bean name="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"> <property name="order" value="2"></property> </bean> 
+7
source

I would like to clarify that instead of rewriting <mvc:annotation-driven/> you can change the declaration order of the handler directives:

 <mvc:resources mapping="/favicon.ico" location="/favicon.ico" order="0"/> <mvc:resources mapping="/robots.txt" location="/robots.txt" order="0"/> <mvc:annotation-driven/> 
+8
source

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


All Articles