简体   繁体   中英

Spring mvc web.xml error

I'm trying to write a web application using Spring MVC. I have a configuration in the web.xml that maps some URLs that are in my code:

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

}

But when I put Spring MVC mapping in my web.xml , it don't load the page, but just shows a 404 error.

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

If I remove this, it don't map the URLs, so I cant access app/tags .

What is the right way to configure the web.xml ?

Here is my complete 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>

And this is the 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>

Looking at your logger declaration, I assume that your AjaxHandler class is in app.web package. However, you set your app-servlet.xml to scan only in apptag.web . That's probably why Spring found no controller.

Solution is either add or change to <context:component-scan base-package="app.web" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM