4

I have my web.xml as

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

applicationContext as

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
>

    <context:component-scan base-package="com.mindedges" />


</beans>

Dispatcher servlet as

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

        <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

        <!--
        Most controllers will use the ControllerClassNameHandlerMapping above, but
        for the index controller we are using ParameterizableViewController, so we must
        define an explicit mapping for it.
        -->
        <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="mappings">
                <props>
                    <prop key="index.htm">indexController</prop>
                </props>
            </property>
        </bean>

        <bean id="viewResolver"
              class="org.springframework.web.servlet.view.InternalResourceViewResolver"
              p:prefix="/WEB-INF/jsp/"
              p:suffix=".jsp" />

        <!--
        The index controller.
        -->
        <bean name="indexController"
              class="org.springframework.web.servlet.mvc.ParameterizableViewController"
              p:viewName="index" />

    </beans>

Controller as in package com.mindedges.testapp

@Controller
@RequestMapping("/sample")
public class SampleController  {

    public String sample(Model model){
        return "sample.jsp";
    }
}

When i hit http://localhost:8084/TestApp/sample .

WARNING: No mapping found for HTTP request with URI [/TestApp/sample] in DispatcherServlet with name

Also when spring starts I don't see handlers for Testapp/sample loaded

I guess my component scan is not working.Why so? any other reason

EDIT: dispatcher-context is

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans "
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"   
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
">

    <context:component-scan base-package="com.mindedges" />

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

    <!--
    Most controllers will use the ControllerClassNameHandlerMapping above, but
    for the index controller we are using ParameterizableViewController, so we must
    define an explicit mapping for it.
    -->
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>
            </props>
        </property>
    </bean>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

    <!--
    The index controller.
    -->
    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="index" />

</beans>
Shaggy
  • 1,444
  • 1
  • 23
  • 34
user93796
  • 18,749
  • 31
  • 94
  • 150

2 Answers2

7

Return the name of the view without the .jsp file extension. The resolution of the view name is handled by the InternalResourceViewResolver.

public String sample(Model model){
    return "sample";
}

This snippet of your configuration:

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp" />

takes the view name sample and applies the appropriate prefix (/WEB-INF/jsp/) and suffix (.jsp) to form the entire view name used for resolution of the view.

Also make sure the sample.jsp is located at /WEB-INF/jsp/sample.jsp.

Also when using the @RequestMapping annotation you must enable these annotations in the configuration for the dispatcher serlvet by including <mvc:annotation-driven/>.

You must also add component scanning within the dispatcher configuration so that the controllers are registered with the dispatcher servlet. Currently, you perform component scanning within the applicationContext.xml configuration file using:

<context:component-scan base-package="com.mindedges" />

However, since this component scanning occurs within the applicationContext file the registered beans (assuming they are your controllers) will not be available to the dispatcher serlvet. You must place this configuration snippet within dispatcher-context.xml.

You many want to consult one of my previous answers regarding the differences between the dispatcher configuration and the context configuration.

There was also some whitespace in your dispatcher-context.xml file and a the mvc namespace was missing. I have corrected these issues and provided them in this GitHub Gist.

Community
  • 1
  • 1
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
  • Moved sample.jsp under jsp folder.Also changed code to return"sample".Still its not working.Same problem.I think componet scan is not working.As when spring loads it shows mapping of urls patter and its handlers in log right? – user93796 Feb 15 '14 at 13:06
  • 1
    Add `` to the dispatcher configuration to use the `@RequestMapping` annotations. – Kevin Bowersox Feb 15 '14 at 13:07
  • You should also add component scanning for controllers to your dispatcher configuration instead of in the context configuration so that controllers are registered with the dispatcher servlet. – Kevin Bowersox Feb 15 '14 at 13:10
  • Off the top of my head I'm not sure if spring shows all of the url patterns its mapping, but it does show all of the registered beans. – Kevin Bowersox Feb 15 '14 at 13:13
  • Is there a reason you load the dispatcher servlet second as opposed to first? – Kevin Bowersox Feb 15 '14 at 13:17
  • I just creat a sample porject using netbeans and 2 was default value there.Am new to spring.Am trying to add .Also i did not get what u mean by "You should also add component scanning for controllers to your dispatcher configuration instead of in the context configuration so that controllers are registered with the dispatcher servlet." can u explain more? – user93796 Feb 15 '14 at 13:19
  • Read the link I placed in the answer, while I work on a more specific example. What is the package containing your controllers? – Kevin Bowersox Feb 15 '14 at 13:20
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/47556/discussion-between-user93796-and-kevin-bowersox) – user93796 Feb 15 '14 at 13:22
  • where should i put context:component-scan and ? inside aplicationContext.xml or dispatcher-servlet.xml ? – user93796 Feb 15 '14 at 13:24
  • Inside dispatcher-servlet.xml – Kevin Bowersox Feb 15 '14 at 13:25
0

Change the name of the request map, have a different name from the jsp it is trying to return