0

Hi I am working on Spring MVC 4.0. When I try to access login page by URL http://localhost:8080/bookstore-web-0.0.1-SNAPSHOT/bookstore/authentication/login, it shows 404 error. And logs shows below warning

WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/bookstore-web-0.0.1-SNAPSHOT/bookstore/authentication/login] in DispatcherServlet with name 'appServlet']]

Here bookstore-web-0.0.1-SNAPSHOT is my .war file. I did following mapping in my web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml, /WEB-INF/spring/appServlet/security-config.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>
        org.springframework.web.filter.DelegatingFilterProxy
    </filter-class>
</filter>   

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/bookstore/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>    
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>

<!-- Map all /resources requests to the Resource Servlet for handling -->
<!-- <servlet-mapping>
    <servlet-name>Resources Servlet</servlet-name>
    <url-pattern>/resources/*</url-pattern>
</servlet-mapping> -->

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/bookstore/*</url-pattern>
</servlet-mapping>

Controller Is:

package com.abhendra.bookstore;
@Controller
public class AuthenticationController {

private static final Logger logger = LoggerFactory.getLogger(AuthenticationController.class);

/**
 * Simply selects the home view to render by returning its name.
 */
@RequestMapping(value = "/authentication/login", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
    logger.info("Welcome home! The client locale is {}.", locale);


    System.out.println("In Controller!!!!!!!!!!!!!!!!!!!!!!");
    return "login";
}

}

I have also done component-scan entry in my root-context.xml file. like:

<context:component-scan base-package="com.abhendra.bookstore, com.abhendra.core" />
<context:annotation-config />

I am not getting where am I doing mistake.

Thanks In advance.

Abhendra Singh
  • 1,959
  • 4
  • 26
  • 46

1 Answers1

1

It's not enough to simply scan for @Controller annotated classes, you need to specify

<mvc:annotation-driven>

so that Spring creates the beans that will actually map your @COntroller beans' handler methods to URL paths.

Do this in the context loaded by the DispatcherServlet, ie. servlet-context.xml.


Note that if you've specified

<context:component-scan .../>

you don't need

<context:annotation-config />
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • Thanks for your reply. But no luck with this... After adding ``, I am facing the same problem. – Abhendra Singh Jan 23 '14 at 03:38
  • @AbhendraSingh Map your controller to `/bookstore/authentication/login` – Sotirios Delimanolis Jan 23 '14 at 05:32
  • I used [this](http://stackoverflow.com/questions/11692837/url-mapping-issue-spring-web-mvc) as reference. I think I don't need to prepend by `/bookstore` in Controller due to `alwaysUseFullPath` (default false). Am I right? – Abhendra Singh Jan 23 '14 at 10:11
  • @AbhendraSingh No, I think that's if your handler method was mapped to `/bookstore/authentication/login` and your servlet was mapped to `/bookstore/*`, then if you set it to true, you would have to make a request to `/bookstore/bookstore/authentication/login`. Leaving it to false, you would only have to send `/bookstore/authentication/login`. – Sotirios Delimanolis Jan 23 '14 at 13:47
  • I found my solution. Adding `` and ` – Abhendra Singh Jan 27 '14 at 18:09