【问题标题】:Spring mvc web.xml errorSpring mvc web.xml错误
【发布时间】:2011-12-27 05:00:30
【问题描述】:

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

@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";
    }

}

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

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

如果我删除它,它不会映射 URL,所以我无法访问 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>

【问题讨论】:

  • 启动日志有错误吗?

标签: java servlets spring-mvc web.xml


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2018-06-04
    • 1970-01-01
    • 1970-01-01
    • 2014-05-05
    • 2013-10-17
    • 2016-07-03
    • 2015-11-10
    • 1970-01-01
    • 2012-09-30
    相关资源
    最近更新 更多