java 根 ("/") 上的 spring mvc 网站
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11786115/
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 website on root ("/")
提问by Poni
I want to map spring mvc controller to root (/**
) path (and not sub-folder such as "/something") while making exceptions with mvc:resources
(open to another method).
我想将 spring mvc 控制器映射到根 ( /**
) 路径(而不是诸如“/something”之类的子文件夹),同时使用mvc:resources
(打开另一种方法)进行例外处理。
This should be the ABC of that framework but apparently is a very complicated stuff to ask of it.
这应该是该框架的基础知识,但显然这是一个非常复杂的问题。
My app-servlet.xml
has these obvious mapping exceptions:
我app-servlet.xml
有这些明显的映射异常:
<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 hit "/" or "/test" or "/test/page" I get output like:
现在,当我点击“/”或“/test”或“/test/page”时,我得到如下输出:
Fri Aug 03 00:22:12 IDT 2012 /
Fri Aug 03 00:22:13 IDT 2012 /favicon.ico
.. see? service()
is being called for /favicon.ico
even when it's explicitly excluded!
.. 看?即使明确排除在外service()
,/favicon.ico
也会被调用!
Now I guess there's some "priority" to the @Controller
over the XML, still, how do I make the exclusion work?
现在我想@Controller
XML有一些“优先级” ,但是,我如何使排除工作?
A simple requirement - have the website on "/".
一个简单的要求 - 将网站放在“/”上。
P.S This answeranswers to a very similar question.
PS这个答案回答了一个非常相似的问题。
Another note: This question is not about tomcat context.
另一个注意事项:这个问题与 tomcat 上下文无关。
采纳答案by Biju Kunjummen
The issue here is that the underlying HandlerMappingregistered with <mvc:resources
has a very low priority compared to the one registered with <mvc:annotation-driven/>
. If you requirement is to simply have something respond to "/" a better way probably will be to have a different @RequestMapping than /**
instead say have it as /home
and define something along these lines:
这里的问题是,与注册的底层HandlerMapping<mvc:resources
相比,注册的优先级非常低<mvc:annotation-driven/>
。如果您的要求只是让某些东西响应“/”,那么更好的方法可能是使用不同的 @RequestMapping/**
而不是说将其作为/home
并按照以下方式定义某些内容:
<mvc:view-controller path="/" view-name="home" />
<mvc:view-controller path="/" view-name="home" />
If this will not work, the only other option will be to lower the priority of underlying handlerMapping of <mvc:resources
, which can be done by explicitly defining the HandlerMapping - a little complicated but can be done.
如果这不起作用,唯一的其他选择是降低底层 handlerMapping 的优先级<mvc:resources
,这可以通过显式定义 HandlerMapping 来完成 - 有点复杂但可以完成。
UpdatedHere is a possible configuration:
更新这是一个可能的配置:
Try with just this first:
先试试这个:
<mvc:resources mapping="/favicon.ico" location="/favicon.ico" order="0"/>
<mvc:resources mapping="/robots.txt" location="/robots.txt" order="0"/>
If this alone does not work, change <mvc:annotation-driven/>
to something along these lines for Spring 3.1.x:
如果仅此一项不起作用,请<mvc:annotation-driven/>
针对 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.MappingHymansonHttpMessageConverter"></bean>
</list>
</property>
</bean>
<bean name="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
<property name="order" value="2"></property>
</bean>
回答by Mr_Mig
I'd like to clarify that instead of rewriting the <mvc:annotation-driven/>
one can change the declaration order of the handler directives:
我想澄清的是,<mvc:annotation-driven/>
可以更改处理程序指令的声明顺序而不是重写:
<mvc:resources mapping="/favicon.ico" location="/favicon.ico" order="0"/>
<mvc:resources mapping="/robots.txt" location="/robots.txt" order="0"/>
<mvc:annotation-driven/>