0

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>
1
  • 1
    Are there any errors in the startup log? Commented Nov 11, 2011 at 17:22

1 Answer 1

1

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" />

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.