[英]spring mvc website on root (“/”)
我想將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" />
我有這個控制器:
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”時,我得到如下輸出:
Fri Aug 03 00:22:12 IDT 2012 /
Fri Aug 03 00:22:13 IDT 2012 /favicon.ico
..看? 即使明確排除了/favicon.ico
也會調用service()
!
現在我想@Controller
對XML有一些“優先級”,但是,我如何進行排除工作呢?
一個簡單的要求 - 將網站放在“/”上。
PS 這個答案回答了一個非常相似的問題。
另一個注意事項:這個問題不是關於tomcat的上下文。
我想澄清一下,不是重寫<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/>
這里的問題是,使用<mvc:resources
注冊的基礎HandlerMapping與使用<mvc:annotation-driven/>
注冊的基礎相比具有非常低的優先級。 如果你的要求只是讓某些東西響應“/”,那么更好的方法可能就是擁有一個不同的@RequestMapping而不是/**
而是將它作為/home
並在這些行中定義一些東西:
<mvc:view-controller path="/" view-name="home" />
如果這不起作用,唯一的另一個選擇是降低<mvc:resources
的底層handlerMapping的優先級,這可以通過顯式定義HandlerMapping來完成 - 有點復雜但可以完成。
更新這里是一個可能的配置:
先嘗試一下:
<mvc:resources mapping="/favicon.ico" location="/favicon.ico" order="0"/>
<mvc:resources mapping="/robots.txt" location="/robots.txt" order="0"/>
如果僅此一項不起作用, <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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.