gpt4 book ai didi

spring-mvc - Spring4 MVC无法识别JSPS

转载 作者:行者123 更新时间:2023-12-03 05:17:40 25 4
gpt4 key购买 nike

我正在尝试使用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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com