Announcement Announcement Module
Collapse
No announcement yet.
View Resolver problem Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • View Resolver problem

    Hello all
    I am trying to build a simple spring application with minimal configuratioms . With this example view resolver is not working properly. Following are my configuration files

    web.xml

    PHP Code:

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

      <
    servlet-mapping>
        <
    servlet-name>MVCDispatcher</servlet-name>
        <
    url-pattern>/app/*</url-pattern>
     </servlet-mapping> 
    config.xml

    PHP Code:
    <bean id="componentController1" class="com.mvc.component.controller.ComponentController1">
            <
    property name="componentBuilderResolver" ref="jcaComponentBuilderResolver" />
         </
    bean>
            
        
        <
    bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
           <
    property name="mappings">
            <
    props>
               <
    prop key="/*.do">componentController1</prop>

             </
    props>
           </
    property>
        </
    bean>
            
      <
    bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
          <
    property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
          <
    property name="cache" value="false" />
          <
    property name="prefix" value="/WEB-INF/jsp/"/>
          <
    property name="suffix" value=".jsp"/>
      </
    bean
    The only code in the handleRequest of my controller class is

    PHP Code:
    @RequestMapping(method = { RequestMethod.GETRequestMethod.POST })
        public 
    ModelAndView handleRequest(HttpServletRequest requestHttpServletResponse responsethrows Exception {
            
            
    ModelAndView result = new ModelAndView("welcome.jsp");
              
    System.out.println("Controller called 2");
            return 
    result;
        } 
    And the URL which I am firing is

    PHP Code:
    http://localhost:8080/Mvc/app/doIt.do 
    As per above configurations , when the controller is hit, welcome.jsp should get rendered as it is declared as the next view in the controller class. Though in my case welcome.jsp is COMPLETELY ignored by the spring container and instead of that following URL gets generated.

    PHP Code:
    HTTP Status 404 - /Mvc/WEB-INF/jsp/doIt.jsp

    type Status report

    message 
    /Mvc/WEB-INF/jsp/doIt.jsp

    description The requested resource 
    (/Mvc/WEB-INF/jsp/doIt.jspis not available.
    Apache Tomcat/6.0.29 
    For what on earth does the spring container tries to look for doIt.jsp INSTED OF welcome.jsp. Why my URL does not get form as

    (/Mvc/WEB-INF/jsp/welcome.jsp)

    Any link, white paper will greatly be appricaiated.
    Thanks in advance

    SAmir

  • #2
    Your Controller is returning the following view:

    Code:
    ModelAndView result = new ModelAndView("welcome.jsp");
    Your ViewResolver has the following properties:
    Code:
    <property name="prefix" value="/WEB-INF/jsp/"/>
          <property name="suffix" value=".jsp"/>
    So that means welcome.jsp will be resolve to /WEB-INF/jsp/welcome.jsp.jsp. That will give you double ".jsp" extension. Your Controller should instead return:

    Code:
    ModelAndView result = new ModelAndView("welcome");
    You have declared a servlet-mapping MVCDispatcher in the web.xml. Did you declare a MVCDispatcher-servlet.xml as well? If you did, what's inside it?

    You also declared a controller:

    Code:
    <prop key="/*.do">componentController1</prop>
    What's inside this controller?

    Your firing the URL: http://localhost:8080/Mvc/app/doIt.do. It is map in your controller?

    It would be better if you could provide as a mock sample so we can see what's happening. I actually prefer using Spring 3.0 MVC's annotations to build my Spring App.

    Check this guide I wrote on how I did mine: http://krams915.blogspot.com/2010/12...tegration.html

    Just ignore the Spring Security part (though eventually you'll need one for your Spring app )

    Comment


    • #3
      Hello skram
      Thank you so much for the reply. Yes..my URL is mapped to the controller . And I tried there by removing .jsp extention in ModelAndView. Though it is still not working with me. I am still getting the same exception.
      As per your last post here are full configuration files that I am using

      config.xml
      PHP Code:
      <?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:context="http://www.springframework.org/schema/context"
             xsi:schemaLocation="http://www.springframework.org/schema/beans 
                 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                 http://www.springframework.org/schema/context
                 http://www.springframework.org/schema/context/spring-context-3.0.xsd">
        
        
          
          <bean id="componentController1" class="com.mvc.component.controller.ComponentController1">
              <property name="componentBuilderResolver" ref="jcaComponentBuilderResolver" />
           </bean>
              
          
          <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
             <property name="mappings">
              <props>
                 <prop key="/*.do">componentController1</prop>

               </props>
             </property>
          </bean>
              
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
            <property name="cache" value="false" />
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
              
            
          
      </beans>
      web.xml

      PHP Code:
      <?xml version="1.0" encoding="UTF-8"?>
      <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
        <display-name>Mvc</display-name>
       
       <servlet>
          <servlet-name>MVCDispatcher</servlet-name>
          <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class>
          <load-on-startup>1</load-on-startup>
      </servlet>




        <servlet-mapping>
          <servlet-name>MVCDispatcher</servlet-name>
          <url-pattern>/app/*</url-pattern>
       </servlet-mapping>

          

      </web-app>
      MVCDispatcher-servlet.xml

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


          
          <import resource="config.xml" />

      </beans>
      I have used the annotation @controller for declearing my class as a controller.

      My controller class is as follows

      PHP Code:

      @Controller
      public class ComponentController1 {
          


          @
      RequestMapping(method = { RequestMethod.GETRequestMethod.POST })
          public 
      ModelAndView handleRequest(HttpServletRequest requestHttpServletResponse responsethrows Exception {
              
              
      ModelAndView result = new ModelAndView("welcome");
          
              
      System.out.println("Controller called 2");

              return 
      result;
          }

      And the URL I fired is

      PHP Code:
      http://localhost:8080/Mvc/app/doIt.do 
      It gets mapped CORRECTLY with my controller. Though I am getting the same error as before

      PHP Code:
      HTTP Status 404 - /Mvc/WEB-INF/jsp/doIt.jsp

      type Status report

      message 
      /Mvc/WEB-INF/jsp/doIt.jsp

      description The requested resource 
      (/Mvc/WEB-INF/jsp/doIt.jspis not available.
      Apache Tomcat/6.0.29 
      welcome.jsp is present in WEB-INF/jsp folder.

      Thanks
      Samir
      Last edited by samir.ware; Dec 27th, 2010, 07:16 AM.

      Comment


      • #4
        Check you imports

        Check your imports, make sure that ModelAndView is of type:

        org.springframework.web.servlet.ModelAndView

        Comment


        • #5
          Originally posted by mistigi View Post
          Check your imports, make sure that ModelAndView is of type:

          org.springframework.web.servlet.ModelAndView
          That reminds me of a post where he imported org.springframework.web.portlet.ModelAndView instead

          Comment

          Working...
          X