Spring Boot – 未找到 JSP 视图
Posted
技术标签:
【中文标题】Spring Boot – 未找到 JSP 视图【英文标题】:Spring Boot – JSP Views Not Found 【发布时间】:2017-10-04 08:13:12 【问题描述】:我正在寻找如何使用 spring boot 运行 jsp。试了好几次还是没找到。
然后我做了这个配置:
-我对pom.xml的依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.17.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.0.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-server -->
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.17</version>
</dependency>
<!--the SL4J dependencies. -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.19</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.5</version>
</dependency>
<!-- the Log4J 2 dependencies. -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<!-- Spring security -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>4.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>4.0.3.RELEASE</version>
</dependency>
<!-- Spring Security JSP Taglib -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<!-- To compile JSP -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- jstl for jsp page -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>$jstl.version</version>
</dependency>
</dependencies>
-类 MvcConfig:
@Configuration
@EnableWebMvc
@ComponentScan
public class MvcConfig extends WebMvcConfigurerAdapter
@Override
public void configureViewResolvers(ViewResolverRegistry registry)
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/Views/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
registry.viewResolver(resolver);
@Override
public void addViewControllers(ViewControllerRegistry registry)
registry.addViewController("/login").setViewName("login");
-class WebSecurityConfig :
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled=true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter
protected void configure(HttpSecurity http) throws Exception
http.authorizeRequests()
.antMatchers("/Users/**").access("hasRole('ADMIN')")
.anyRequest().authenticated()
.and()
.formLogin().loginPage("/login").permitAll()
.defaultSuccessUrl("/Acceuil")
.and()
.logout().logoutSuccessUrl("/login?logout")
.and()
.exceptionHandling().accessDeniedPage("/403")
.and()
.csrf();
-类应用程序:
@SpringBootApplication
@ComponentScan
@EnableAutoConfiguration
public class AppsApplication extends SpringBootServletInitializer
protected SpringApplicationBuilder configure(SpringApplicationBuilder application)
return application.sources(AuditConfigurationApplication.class);
public static void main(String[] args)
SpringApplication.run(AuditConfigurationApplication.class, args);
-application.properties:
spring.mvc.view.prefix= /WEB-INF/Views/
spring.mvc.view.suffix= .jsp
当我访问一个网址时:“http://localhost:8099/App/login”
我有这个消息:
o.s.web.servlet.PageNotFound: 没有为 HTTP 请求找到映射 DispatcherServlet 中带有名称的 URI [/App/WEB-INF/jsp/login.jsp] 'dispatcherServlet'
jsp页面位于:src/main/webapp/WEB-INF/jsp/login.jsp
编辑1:
dispatcher.xml:
<beans xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.SSC"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>JSP</display-name>
<welcome-file-list>
<welcome-file>login.html</welcome-file>
<welcome-file>login.htm</welcome-file>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<!-- Spring Boot Servlets -->
<servlet>
<servlet-name>SpringBootWebXmlServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringBootWebXmlServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>
jsp页面的url:src/main/webapp/WEB-INF/jsp/login.jsp
文件xml的url:src/main/webapp/WEB-INF/web.xml,dispatcher.xml
我有这个问题:
Circular view path [login]: would dispatch back to the current handler URL [/App/login] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
感谢您帮助我在服务器中运行 login.jsp 页面。
【问题讨论】:
baeldung.com/spring-dispatcherservlet - Baeldung 上的一篇文章详细介绍了设置 JSP 感谢@Adam Gerard 的链接,我应用了链接 baeldung 的“3.3. ViewResolver Interface”部分。我有这个消息:osweb.servlet.PageNotFound: No mapping found for HTTP request with URI [/App/WEB-INF/jsp/login.jsp] in DispatcherServlet with name 'dispatcherServlet' jsp 页面可以在:src /main/webapp/WEB-INF/jsp/login.jsp 您是否阅读过引导文档的 JSP 限制部分? docs.spring.io/spring-boot/docs/current/reference/htmlsingle/… 需要定义一个返回jsp页面名称的控制器吗? 嘿@Michel - 我认为下一个问题可能与实际的 dispatcherServlet 映射有关 - 至少,当我没有正确配置 dispatcherServlet 时,我遇到了这个问题:@987654324 @ 【参考方案1】:我没有看到您的完整 pom.xml,但对我有用(根据 cmets 中提到的Spring JSP Limitations)是将包装从 JAR 更改为 WAR。
<packaging>war</packaging>
【讨论】:
以上是关于Spring Boot – 未找到 JSP 视图的主要内容,如果未能解决你的问题,请参考以下文章
JSP 文件未在 Spring Boot Web 应用程序中呈现
struts2 使用 Spring Boot - JSP 未呈现