views:

7819

answers:

4

my handler forwards to internalresourceview 'apiForm' ? but then i get error

404 RequestURI=/WEB-INF/pages/apiForm.jsp . i'm sure apiForm.jsp located in /WEB-INF/pages/

13:45:02,034 DEBUG [org.springframework.web.servlet.view.JstlView] - Forwarding to resource [/WEB-INF/pages/apiForm.jsp] in InternalResourceView 'apiForm' 13:45:02,035 DEBUG [org.springframework.web.servlet.DispatcherServlet] - DispatcherServlet with name 'testapp2' determining Last-Modified value for [/WEB-INF/pages/apiForm.jsp] 13:45:02,038 DEBUG [org.springframework.web.servlet.DispatcherServlet] - No handler found in getLastModified 13:45:02,038 DEBUG [org.springframework.web.servlet.DispatcherServlet] - DispatcherServlet with name 'testapp2' processing request for [/WEB-INF/pages/apiForm.jsp] 13:45:02,038 WARN [org.springframework.web.servlet.PageNotFound] - No mapping found for HTTP request with URI [/WEB-INF/pages/apiForm.jsp] in DispatcherServlet with name 'testapp2' 13:45:02,045 DEBUG [org.springframework.web.servlet.DispatcherServlet] - Successfully completed request 13:45:02,048 DEBUG [org.springframework.web.servlet.DispatcherServlet] - Successfully completed request

this is how my dispatcher.xml look like..

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.UrlBasedViewResolver">
      <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>

    <property name="prefix" value="/WEB-INF/pages/"/>
    <property name="suffix" value=".jsp"/>
</bean>
+9  A: 

Looks like DispatcherServlet is trying to process the request for apiForm.jsp, which suggests to me that your web.xml servlet-mapping is directing requests for that space to DispatcherServlet.

You might have something like this?

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

Try calling your controllers with a different extension (.do for example) and update the servlet-mapping to suit

 <servlet-mapping>
  <servlet>dispatcher</servlet>
  <url-pattern>*.do</url-pattern>
</servlet-mapping>
ptomli
yes, u are right. can u explain more, why i cannot set dispatcher as /* ?
cometta
When you set the url-pattern to /* then all requests will be sent to that DispatcherServlet, which includes the request for JSP rendering.Though it's not true, it's sometimes useful to think of the InternalResourceView (and derived like JstlView) as another HTTP request, since that way you'll see why a request for the JSP is getting picked up by the DispatcherServlet.
ptomli
when i set to / , i the request is render fine. only when i set /* . what is the different / and /* ?
cometta
<url-pattern>/</url-pattern> only matches the URL http://host/servlet/<url-pattern>/*</url-pattern> matches everything under http://host/servlet/, such as /index.html, /foo.jpg and, most importantly in this case, /WEB-INF/pages/apiForm.jspthe * is the wildcard, which says "anything"In the earlier suggestion, *.do matches anything that ends in .do, for example, /foo.do, /foo/bar.do. It doesn't match anything ending in jsp, so a request for /WEB-INF/pages/apiFrom.jsp is not matched, and is not routed to the DispatcherServlet
ptomli
thanks for your answer, helped a lot
arturh
A: 

What you need is to have a controller that responds to the url first which then renders your jsp. See this link for a solution.

NA
A: 

This leads to my question. I've been searching for an easy way for my servlet to handle all requests with specific controllers and a default controller. This explains my StackOverflow issues, as my jsp views are being sent back into my controller ad nauseum. This has to be a common enough scenario, and I've seen the question asked as I google around but not an answer.

My hope is that if a ControllerClassNameHandlerMapping is not found (no "FooController" to handle a /foo request) then the "defaultController" would serve /WEB-INF/jsp/default.jsp. Surely there must be a way to make that happen? Here's the config. There is a controller class springapp.web.InventoryController that is discovered. Web.xml is configured to serve all requests to springapp (with /*).

<context:component-scan base-package="springapp"/>

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

<bean id="defaultController" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
  <property name="viewName" value="default" />
</bean>

<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
  <property name="prefix" value="/WEB-INF/jsp/" />
  <property name="suffix" value=".jsp" />
</bean>

thanx

clish
+1  A: 

Yes, I know I'm late to this party but it might help others.

The servlet container chooses the mapping based on the longest path that matches. So you can put this mapping in for your JSPs and it will be chosen over the /* mapping.

<servlet-mapping>
  <servlet-name>jsp</servlet-name>
  <url-pattern>/WEB-INF/pages/*</url-pattern>
 </servlet-mapping>

Actually for Tomcat that's all you'll need since jsp is a servlet that exists out of the box. For other containers you either need to find out the name of the JSP servlet or add a servlet definition like:

<servlet>
  <servlet-name>jsp</servlet-name>
  <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
</servlet>
ericacm