Gaurav
Gaurav

Reputation: 31

Spring MVC : Dispatcher mapping to the wrong view

The problem is: I am returning customer as my view. According to my viewresolver is should be mapped to WEB-INF/pages/customer.html. Instead it is going through dispatcher servlet and not able to find the customer html. The error which it gives is: "WARNING: No mapping found for HTTP request with URI [/SpringMVC/WEB-INF/pages/customer.html] in DispatcherServlet with name 'mvc-dispatcher'"

This is my controller

@Controller
public class CustomerController implements BeanFactoryAware {

    private Customers customers;


    /*public String getCustomer(@RequestParam String name) {

        //ApplicationContext context = new FileSystemXmlApplicationContext("/WEB-INF/springapp-servlet.xml");
        //Customers customers = get
        System.out.println("In Controller");
        return "customer";
    }*/

    @RequestMapping(value="/form")  
    public String getCustomer(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {


            System.out.println("In Customer Controller");
            return "customer";
    }

    @Override
    public void setBeanFactory(BeanFactory context) throws BeansException {
        // TODO Auto-generated method stub
        customers = (Customers)context.getBean("customers");

        //System.out.println(customers);

    }

}

This is my web.xml

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Spring Web MVC Application</display-name>

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/mvc-dispatcher-servlet.xml,/WEB-INF/beans.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

This is my dispatcher.xml


<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/pages/</value>
    </property>
    <property name="suffix">
        <value>.html</value>
    </property>
</bean>

Upvotes: 0

Views: 2476

Answers (3)

Biju Kunjummen
Biju Kunjummen

Reputation: 49915

This will not work, as ultimately the call will go to the RequestDispatcher something like this:

RequestDispatcher dispatcher = httpRequest.getRequestDispatcher("/WEB-INF/pages/test.html");
dispatcher.forward(httpRequest, httpRequest);

At this point container will expect DispatcherServlet to again handle the request(because of the /* path). If it is a jsp page, container has a mapping for *.jsp and knows how to handle it.

The fix for you will be to place your resources somewhere relative to your Web resources(if maven structure then under src/main/webapp/resources/, configure handler for this content:

<mvc:resources location="/resources/" mapping="/resources/**" />

and now from your controller you can return:

return "forward:/resources/mypage.html";

Also, I see that you are doing a lookup on the "customers" bean, you need not do that and instead expect Spring to inject it in:

@Controller
public class CustomerController{

    @Autowired private Customers customers;

Upvotes: 0

vishnu viswanath
vishnu viswanath

Reputation: 3854

Your servlet will intercept all the calls on url patterns matching '/*' but its not able to find the specified mapping in any of you spring controllers.

I have faced similar trouble once and how i overcame this is by following a particular pattern for all your spring calls. e.g.,

<url-pattern>*.htm</url-pattern>

Hope this helps you.

Upvotes: 0

Reimeus
Reimeus

Reputation: 159784

Try changing your servlet mapping from:

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

to

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

Upvotes: 0

Related Questions