root上的spring mvc网站("/") [英] spring mvc website on root ("/")
问题描述
我想将spring mvc控制器映射到根(/**
)路径(而不是子文件夹,例如"/something"),同时使用mvc:resources
设置例外(打开另一种方法).
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).
这应该是该框架的基础知识,但显然是一个非常复杂的问题.
This should be the ABC of that framework but apparently is a very complicated stuff to ask of it.
我的app-servlet.xml
具有以下明显的映射异常:
My app-servlet.xml
has these obvious mapping exceptions:
<mvc:resources mapping="/favicon.ico" location="/favicon.ico" />
<mvc:resources mapping="/robots.txt" location="/robots.txt" />
我有这个控制器:
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";
}
}
现在,当我按"/"或"/test"或"/test/page"时,我得到如下输出:
Now when I hit "/" or "/test" or "/test/page" I get output like:
Fri Aug 03 00:22:12 IDT 2012 /
Fri Aug 03 00:22:13 IDT 2012 /favicon.ico
..看到了吗?即使明确排除了service()
,也要为其调用service()
!
.. see? service()
is being called for /favicon.ico
even when it's explicitly excluded!
现在我猜想@Controller
在XML上还有一些优先级",但是,如何使排除工作起作用?
Now I guess there's some "priority" to the @Controller
over the XML, still, how do I make the exclusion work?
一个简单的要求-将网站放在"/"上.
A simple requirement - have the website on "/".
P.S 此答案回答了一个非常相似的问题.
P.S This answer answers to a very similar question.
另一个说明:这个问题与tomcat上下文无关.
Another note: This question is not about tomcat context.
推荐答案
此处的问题是基础/**的@RequestMapping而不是将其表示为/home
并按以下方式定义内容:
The issue here is that the underlying HandlerMapping registered 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:
<mvc:view-controller path="/" view-name="home" />
如果这不起作用,则唯一的其他选择是降低<mvc:resources
的基础handlerMapping的优先级,这可以通过显式定义HandlerMapping来完成-有点复杂,但是可以做到.
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.
已更新 这是可能的配置:
首先尝试一下:
<mvc:resources mapping="/favicon.ico" location="/favicon.ico" order="0"/>
<mvc:resources mapping="/robots.txt" location="/robots.txt" order="0"/>
如果仅此一项不起作用,请针对Spring 3.1.x将<mvc:annotation-driven/>
更改为以下内容:
If this alone 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>
这篇关于root上的spring mvc网站("/")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!