Pon*_*oni 7 java mapping servlets spring-mvc
我想将spring mvc controller映射到root(/**)路径(而不是像"/ something"这样的子文件夹),同时使用mvc:resources(打开另一个方法)进行异常.
这应该是该框架的基础知识,但显然是一个非常复杂的问题.
我app-servlet.xml有这些明显的映射异常:
<mvc:resources mapping="/favicon.ico" location="/favicon.ico" />
<mvc:resources mapping="/robots.txt" location="/robots.txt" />
Run Code Online (Sandbox Code Playgroud)
我有这个控制器:
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";
}
}
Run Code Online (Sandbox Code Playgroud)
现在,当我点击"/"或"/ test"或"/ test/page"时,我得到如下输出:
Fri Aug 03 00:22:12 IDT 2012 /
Fri Aug 03 00:22:13 IDT 2012 /favicon.ico
Run Code Online (Sandbox Code Playgroud)
..看?service()正在被要求,/favicon.ico即使明确被排除在外!
现在我想@Controller对XML 有一些"优先权" ,但是,我如何进行排除工作呢?
一个简单的要求 - 将网站放在"/"上.
PS 这个答案回答了一个非常相似的问题.
另一个注意事项:这个问题不是关于tomcat的上下文.
小智 8
我想澄清一下,不是重写,而是<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/>
Run Code Online (Sandbox Code Playgroud)
这里的问题是注册的底层HandlerMapping与注册的<mvc:resources相比具有非常低的优先级<mvc:annotation-driven/>.如果你的要求是简单地让某些东西响应"/",那么更好的方法可能就是拥有一个不同的@RequestMapping,/**而不是将其作为/home并按照以下方式定义:
<mvc:view-controller path="/" view-name="home" />
如果这不起作用,唯一的另一个选择是降低底层handlerMapping的优先级<mvc:resources,这可以通过显式定义HandlerMapping来完成 - 有点复杂但可以完成.
更新 这里是一个可能的配置:
先尝试一下:
<mvc:resources mapping="/favicon.ico" location="/favicon.ico" order="0"/>
<mvc:resources mapping="/robots.txt" location="/robots.txt" order="0"/>
Run Code Online (Sandbox Code Playgroud)
如果仅此一项不起作用,请更改<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.MappingJacksonHttpMessageConverter"></bean>
</list>
</property>
</bean>
<bean name="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
<property name="order" value="2"></property>
</bean>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10339 次 |
| 最近记录: |