Basic Spring MVC config: PageNotFound using InternalResourceViewResolver

后端 未结 5 1774
礼貌的吻别
礼貌的吻别 2020-12-14 23:14

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,

相关标签:
5条回答
  • 2020-12-14 23:36

    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.

    0 讨论(0)
  • 2020-12-14 23:53

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

    0 讨论(0)
  • 2020-12-15 00:01

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

    0 讨论(0)
  • 2020-12-15 00:02

    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 /*.

    0 讨论(0)
  • 2020-12-15 00:03

    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.

    0 讨论(0)
提交回复
热议问题