spring-mvc - Spring4 MVC无法识别JSPS

标签 spring-mvc gradle jetty spring-4

我正在尝试使用gradle和Spring4制作MVC项目。

@Bean
public UrlBasedViewResolver viewResolver() {
  InternalResourceViewResolver resolver = new InternalResourceViewResolver();
  resolver.setPrefix("/WEB-INF/jsp/");
  resolver.setSuffix(".jsp");
  return resolver;
}

...
@RequestMapping("/home")
public String welcome() {
  return "index";
}

但是当我使用gradle jettyRun运行时,我得到了...
http://localhost:8080/personal-war/home
HTTP ERROR 404

Problem accessing /personal-war/WEB-INF/jsp/index.jsp. Reason:
  NOT_FOUND

更新Web.xml
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
  <display-name>Spring MVC Application</display-name>
  <servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
      </init-param>
      <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>com.proj.spring.config</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
      <servlet-name>mvc-dispatcher</servlet-name>
      <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

我加了这条线
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
}

但是然后只有html渲染了服务器端的东西不起作用

最佳答案

首先,您需要知道Servlet容器(我也假设Jetty)具有用于呈现JSP的Servlet。通常将此Servlet扩展名映射到*.jsp

Servlet Specification gives the order of url-pattern matching

  1. The container will try to find an exact match of the path of the request to the path of the servlet. A successful match selects the servlet.
  2. The container will recursively try to match the longest path-prefix. This is done by stepping down the path tree a directory at a time, using the ’/’ character as a path separator. The longest match determines the servlet selected.
  3. If the last segment in the URL path contains an extension (e.g. .jsp), the servlet container will try to match a servlet that handles requests for the extension. An extension is defined as the part of the last segment after the last ’.’ character.
  4. If neither of the previous three rules result in a servlet match, the container will > attempt to serve content appropriate for the resource requested. If a "default" servlet is defined for the application, it will be used. Many containers provide an implicit default servlet for serving content.


就您而言,当您继续
/WEB-INF/jsp/index.jsp

Servlet容器会将路径匹配到映射到的DispatcherServlet
/*

这发生在容器可以匹配映射到*.jsp的JSP servlet之前。

因此,它将使用您的DispatcherServlet对请求进行service(..)。但是您的DispatcherServlet没有针对的处理程序
/WEB-INF/jsp/index.jsp

简单的解决方案是将DispatcherServlet映射到
/

如果找不到匹配项,则将其作为后备Servlet

关于spring-mvc - Spring4 MVC无法识别JSPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22213696/

相关文章:

javascript - Apache Tomcat 错误 : 404 page not found while the other jsp runs properly

java - 传统项目转spring boot,不改applicationContext.xml、dispatcher-servlet.xml & tomcat的context.xml

android-studio - Android Studio Gradle尝试从Maven URL下载不存在的sha1

gradle - gradle自定义任务类使其无法编译

java - ClassNotFoundException FreeMarkerConfigurationFactory

java - Heroku 无法访问 jetty-runner jar 文件

java - org.springframework.beans.factory.CannotLoadBeanClassException : Cannot find class

带有 OpenId 提供程序的 Java Spring Security

java - 使用 gradle 修改 Android 库的包

tomcat - 如何修改 Jersey 响应头