11

I'm trying to get a first Spring 3 MVC setup running.

My app is running on tomcat, with in the server context of "grapevine"

For the purposes of testing, I'm trying to get requests from http://localhost:8080/grapevine/test to render the contents of WEB-INF/jsp/noSuchInvitation.jsp

When I try this, I'm getting a 404, and the logs suggest that my jsp isn't present:

WARN  org.springframework.web.servlet.PageNotFound  - No mapping found for HTTP request with URI [/grapevine/WEB-INF/jsp/noSuchInvitation.jsp] in DispatcherServlet with name 'grapevine'

I must have mis-configured this somewhere, but I can't see what I've done wrong.

Here's all the relevant snippets.

Web.xml:

<servlet>
    <servlet-name>grapevine</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>grapevine</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

From my context:

<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
</bean>

Controller:

@Controller
public class ParticipantInvitationController {

@RequestMapping("/test")
public ModelAndView test()
{
    return new ModelAndView("noSuchInvitation");
}

Log:

DEBUG org.springframework.web.servlet.DispatcherServlet  - Rendering view [org.springframework.web.servlet.view.JstlView: name 'noSuchInvitation'; URL [/WEB-INF/jsp/noSuchInvitation.jsp]] in DispatcherServlet with name 'grapevine'
DEBUG org.springframework.web.servlet.view.JstlView  - Forwarding to resource [/WEB-INF/jsp/noSuchInvitation.jsp] in InternalResourceView 'noSuchInvitation'
DEBUG org.springframework.web.servlet.DispatcherServlet  - DispatcherServlet with name 'grapevine' processing GET request for [/grapevine/WEB-INF/jsp/noSuchInvitation.jsp]
WARN  org.springframework.web.servlet.PageNotFound  - No mapping found for HTTP request with URI [/grapevine/WEB-INF/jsp/noSuchInvitation.jsp] in DispatcherServlet with name 'grapevine'
DEBUG org.springframework.security.web.context.HttpSessionSecurityContextRepository  - SecurityContext contents are anonymous - context will not be stored in HttpSession. 
DEBUG org.springframework.web.servlet.DispatcherServlet  - Successfully completed request
Harshit patel
  • 170
  • 11
Marty Pitt
  • 28,822
  • 36
  • 122
  • 195

5 Answers5

27

This is because the <url-pattern> in your web.xml is too "wide". A value of /* means that the servlet is configured to receive all requests, and that includes the request from the servlet to the JSP. The error message you're seeing is from DispatcherServlet, which is receiving its own forwarded request.

You should pick a more specific <url-pattern>, e.g. <url-pattern>/xyz/*</url-pattern>, so that your URL then becomes http://localhost:8080/grapevine/xyz/test, and then it should work fine.

skaffman
  • 398,947
  • 96
  • 818
  • 769
  • 1
    What is the difference between /* and just /? I have a Spring webapp configured almost exactly the same way as Marty's, but mine works with the dispatcher servlet mapped to /. – Ben J Oct 07 '10 at 10:42
  • 3
    @Ben: `/` will only match the root URL, whereas `/*` will match all URLs – skaffman Oct 07 '10 at 11:14
  • 2
    That fixed the problem, thanks. Hovewer, I have one question. Given a page example.com, the url-pattern / will match only http://example.com. How is it possible that the request to http://example.com/test is redirected to the spring dispatcher servlet? – Wojciech Górski Oct 12 '12 at 11:45
  • @WojciechGórski At startup, your controllers are scanned and necessary paths are set like servlets. You can check your logs for `o.s.w.s.m.m.a.RequestMappingHandlerMapping` and see the paths which are set. – SelimOber Jan 08 '14 at 13:08
  • SelimOber, I know that it works. I was just curious *why* it works (the url-pattern should apply only to the root URL, not to the "sub-pages") – Wojciech Górski Jan 08 '14 at 21:06
  • @WojciechGórski it's my understanding that with `/` only mapped urls are forwarded to Dispatcher servlet, which might be for example: `/foo/bar/baz`. On the other hand with `/*` every request goes to Dispatcher, it serves what it can, and sends not found otherwise. – SelimOber Jan 15 '14 at 08:54
2

Just replace /* to / as your url-pattern. It will work...

Alfergon
  • 5,463
  • 6
  • 36
  • 56
Jomon
  • 21
  • 1
1

Solution 1: You can register your servlet at *.html and *.json (or xml, gif, png...):

<servlet>
    <servlet-name>RestServlet</servlet-name>
    <servlet-class>
       org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
</servlet>

<servlet-mapping>
  <servlet-name>RestServlet</servlet-name>
  <url-pattern>/</url-pattern>
  <url-pattern>*.html</url-pattern>
  <url-pattern>*.json</url-pattern>
 </servlet-mapping>

Solution 2: If you want to keep your servlet mapped at /*, add the following to your spring.xml file:

<mvc:default-servlet-handler/> 

And this to your web.xml file:

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

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

The rationale is explained here: spring, web.xml. This will register an explicit handler for JSP pages with greater precedence than /*.

Community
  • 1
  • 1
Nacho Coloma
  • 7,070
  • 2
  • 40
  • 43
0

BEWARE: This could be a misleading error message. It just happened to me.

Even thought the error message unexpectedly contains the /ContextName/... at the beginning of the path, it could still be a misspelling in either the InternalResourceViewResolver prefix:

<property name="prefix" value="/WEB-INF-typo-Here/jsp/"/>

or the file path itself.

Now that I fixed my misspelling, it works fine. I don't know why the context shows in the error message, and it really caused me to ignore my silly typo and attempting to try the wonderful other contributions to this question. Don't let it happen to you!

BTW, I am using Spring 4.0.0 release.

David Lotts
  • 550
  • 5
  • 10
0

for me I solved the problem by using .jsp templates and not just .html.

appsthatmatter
  • 6,347
  • 3
  • 36
  • 40