在不使用扩展名的情况下,在DispatcherServlet中找不到映射

时间:2012-02-02 14:05:20

标签: java spring spring-mvc

我收到消息“请求的资源()不可用。”当我使用DispatcherServlet转到我已映射的URL时。它之前的映射设置使用.do扩展名,但是当我没有扩展名映射时它不起作用。

控制器:

@Controller
public class WodServiceController {

    @Autowired
    private WodService wodService;

    @RequestMapping("/json/wod")
    @ResponseBody
    public Word getWord(HttpServletRequest request, HttpServletResponse response) {
        return wodService.getWordOfTheDay();
    }
}

调度-servlet.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
 http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd
 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<mvc:annotation-driven />
<context:component-scan base-package="foobar.controller" />

<!-- View Resolvers -->

<bean id="defaultViewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    p:viewClass="org.springframework.web.servlet.view.JstlView"
    p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />

</beans>

的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Services</display-name>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:commonBeans.xml
    </param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/json/*</url-pattern>
</servlet-mapping>

</web-app>

如果我将映射设置为* .do并将RequestMapping设置为wod.do它可以正常工作。

编辑:

在日志中,当我转到该URL时,它会显示消息

  

在名为“dispatcher”的DispatcherServlet中找不到带有URI [/ foobar / json / wod]的HTTP请求的映射

编辑2:我将alwaysUseFullPath添加到处理程序:

<bean id="annotationHandlerMapping"
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="alwaysUseFullPath" value="true" />  
</bean>

1 个答案:

答案 0 :(得分:1)

这是由于我的apache重定向设置方式。