java Spring MVC - jsp 不呈现

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1576223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 17:09:07  来源:igfitidea点击:

Spring MVC - jsp not rendering

javaspring-mvc

提问by Johnco

I'm just starting with Spring MVC trying to create a new project, and came accross an issue for which no manual or tutorial seems to help...

我刚开始尝试使用 Spring MVC 尝试创建一个新项目,但遇到了一个似乎没有手册或教程可以帮助解决的问题...

I have set up a simple application with no logic, just trying to get Spring configured properly. The controller just returns the name of a view to be displayed, but the view resolver is not rendering the jsp, and returning a 404 error....

我已经建立了一个没有逻辑的简单应用程序,只是试图正确配置 Spring。控制器只返回要显示的视图的名称,但视图解析器不渲染jsp,并返回404 错误....

Any help is greatly appreciated.

任何帮助是极大的赞赏。

My web.xml is:

我的 web.xml 是:

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <servlet>
    <servlet-name>openstats</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>openstats</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

  <display-name>OpenStats API Server</display-name>
</web-app>

An my openstats-servlet.xml

一个我的 openstats-servlet.xml

<?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-2.5.xsd">

    <context:component-scan base-package="org.openstats.api.controller"/>

    <!-- Enable to request mappings PER METHOD -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

    <!-- Enable annotated POJO @Controller -->
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>

    <!-- Define the view resolver to use jsp files within the jsp folder -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>
        <property name="prefix"><value>/jsp/</value></property>
        <property name="suffix"><value>.jsp</value></property>
    </bean>
</beans>

The controller itself has no logic whatsoever, it's simply:

控制器本身没有任何逻辑,它只是:

@Controller
public class ProductController {

    @RequestMapping(value = "/products.do", method = RequestMethod.GET)
    public ModelAndView listProducts(HttpServletRequest request) {

        ModelAndView model = new ModelAndView("index");
        return model;
    }
}

The controller is reached, the issue is when attempting to render...

到达控制器,问题是在尝试渲染时...

I set up log4j in debug, and this is part of what I get:

我在调试中设置了 log4j,这是我得到的一部分:

02:08:19,702 DEBUG DispatcherServlet:1094 - Testing handler adapter [org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter@397b6074] 02:08:19,803 DEBUG HandlerMethodInvoker:134 - Invoking request handler method: public org.springframework.web.servlet.ModelAndView org.openstats.api.controller.ProductController.listProducts(javax.servlet.http.HttpServletRequest) 02:08:19,833 DEBUG DefaultListableBeanFactory:1367 - Invoking afterPropertiesSet() on bean with name 'index' 02:08:19,876 DEBUG InternalResourceViewResolver:81 - Cached view [index] 02:08:19,877 DEBUG DispatcherServlet:1181 - Rendering view [org.springframework.web.servlet.view.JstlView: name 'index'; URL [/jsp/index.jsp]] in DispatcherServlet with name 'openstats' 02:08:19,877 DEBUG JstlView:240 - Rendering view with name 'index' with model {} and static attributes {} 02:08:19,923 DEBUG JstlView:234 - Forwarding to resource [/jsp/index.jsp] in InternalResourceView 'index' 02:08:19,926 DEBUG DispatcherServlet:955 - DispatcherServlet with name 'openstats' determining Last-Modified value for [/api-server/jsp/index.jsp] 02:08:19,927 DEBUG DispatcherServlet:1054 - Testing handler map [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping@440c4cee] in DispatcherServlet with name 'openstats' 02:08:19,928 DEBUG DefaultAnnotationHandlerMapping:179 - No handler mapping found for [/jsp/index.jsp] 02:08:19,929 DEBUG DispatcherServlet:962 - No handler found in getLastModified 02:08:19,937 DEBUG DispatcherServlet:781 - DispatcherServlet with name 'openstats' processing request for [/api-server/jsp/index.jsp] 02:08:19,938 DEBUG DispatcherServlet:843 - Bound request context to thread: GET /api-server/products.do HTTP/1.1

02:08:19,702 DEBUG DispatcherServlet:1094 - 测试处理程序适配器 [org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter@397b6074] 02:08:19,803 DEBUG HandlerMethodInvoker:134 - 调用 org.springframework 方法:public.springframework web.servlet.ModelAndView org.openstats.api.controller.ProductController.listProducts(javax.servlet.http.HttpServletRequest) 02:08:19,833 DEBUG DefaultListableBeanFactory:1367 - 在名为“index”的 bean 上调用 afterPropertiesSet() 02:08: 19,876 DEBUG InternalResourceViewResolver:81 - 缓存视图 [index] 02:08:19,877 DEBUG DispatcherServlet:1181 - 渲染视图 [org.springframework.web.servlet.view.JstlView: name 'index'; URL [/jsp/index.jsp]] 在 DispatcherServlet 中,名称为 'openstats' 02:08:19,877 DEBUG JstlView:

My jsp folder is right within "webapp" and the index.jsp file exists.

我的 jsp 文件夹就在“webapp”中,并且 index.jsp 文件存在。

Thanks in advance.

提前致谢。

回答by Bas

I do have the same problem with Spring 3.x. Any progress so far?

我对 Spring 3.x 也有同样的问题。到目前为止有什么进展吗?

EDIT:I figured it out myself :-) I used the following servletmapping:

编辑:我自己想出来了:-)我使用了以下servletmapping:

  <servlet-mapping>
    <servlet-name>spring-frontcontroller</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>

Editing the url-pattern to e.g. *.do fixes the problem of not rendering the JSP. But this leaves the question how this is possible with your url-pattern.

将 url-pattern 编辑为例如 *.do 修复了不呈现 JSP 的问题。但这留下了一个问题,即您的 url-pattern 是如何实现的。

回答by Doug

Does your web.xml define index.jsp in the welcome-file-list, if so it may be getting overridden. Try changing the jsp name to products.jsp.

您的 web.xml 是否在欢迎文件列表中定义了 index.jsp,如果是,它可能会被覆盖。尝试将 jsp 名称更改为 products.jsp。

e.g.

例如

@Controller
public class ProductController {

    @RequestMapping(value = "/products.do", method = RequestMethod.GET)
    public String handleRequest() {
          return "products";
    }
}

回答by Jeremy

doesn't your .jsp and your .do conflict? since a file cannot end with both .jsp and .do .. therefore it will never resolve... so you should get rid of .jsp or change your url pattern to /*

你的 .jsp 和你的 .do 没有冲突吗?因为文件不能同时以 .jsp 和 .do 结尾.. 因此它永远不会解析......所以你应该摆脱 .jsp 或将你的 url 模式更改为 /*

回答by Renato Teixeira Lima

Changing

改变

import org.springframework.web.servlet.ModelAndView;

by

经过

import org.springframework.web.portlet.ModelAndView;

Works in my case.

在我的情况下有效。