Spring mvc web.xml错误

时间:2011-11-11 17:15:50

标签: java servlets spring-mvc web.xml

我正在尝试使用Spring MVC编写Web应用程序。我在web.xml中有一个配置,用于映射我的代码中的一些网址:

@Controller
@RequestMapping(value = "app")
public class AjaxHandler {
    /**
     * Instance of Logger
     */
    private static final Logger logger = Logger
        .getLogger(app.web.AjaxHandler.class);

    @RequestMapping(value = "/tags", method = RequestMethod.GET)
    public @ResponseBody
    String tagsRecommender(String token) {
        return "Some tag";
    }

}

但是当我在我的web.xml中放置Spring MVC映射时,它不会加载页面,只是显示404错误。

<servlet-mapping>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

如果我将其删除,则不会映射网址,因此我无法访问app/tags

配置web.xml的正确方法是什么?

这是我的完整web.xml

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

<context-param>  
    <param-name>log4jConfigLocation</param-name>  
    <param-value>/WEB-INF/log4j.xml</param-value>  
</context-param>  
<listener>  
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
</listener> 

<filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>
        org.springframework.web.filter.CharacterEncodingFilter
    </filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Handles all requests into the application -->
<servlet>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/app-servlet.xml
        </param-value>
    </init-param>       
    <load-on-startup>1</load-on-startup>        
</servlet>

<servlet-mapping>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

这是app-servlet

<!-- Scans the classpath of this application for @Components to deploy as 
    beans -->
<context:component-scan base-package="apptag.web" />

<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />

<!-- Forwards requests to the "/" resource to the "welcome" view -->
<mvc:view-controller path="/" view-name="index" />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving 
    up static resources in the ${webappRoot}/resources/ directory -->
<mvc:resources mapping="/resources/**" location="/resources/" />

<!-- Saves a locale change using a cookie -->
<bean id="localeResolver"
    class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />

<!-- Application Message Bundle -->
<bean id="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="/WEB-INF/messages/messages" />
    <property name="cacheSeconds" value="0" />
</bean>

<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views 
    directory -->
<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WebContent/WEB-INF/views/" />
    <property name="suffix" value=".html" />
</bean>

1 个答案:

答案 0 :(得分:1)

查看您的记录器声明,我假设您的AjaxHandler类位于app.web包中。但是,您将app-servlet.xml设置为仅在apptag.web中扫描。这可能就是为什么Spring找不到控制器的原因。

解决方案是添加或更改为<context:component-scan base-package="app.web" />