spring boot jsp文件无法查看

时间:2015-11-17 07:50:44

标签: spring jsp spring-boot

我使用的是春季启动应用程序。我为它设置了MvcConfig类,并将tomcat-embed-jasper和jstl依赖项添加到pom.xml。但是,我无法在' WEB-INF'中查看我的jsp文件。文件夹,我将得到404错误(Whitelabel错误页面)。我已经设置了Application.properties。这是我的application.properties:

#
## View resolver
#
spring.view.prefix: /WEB-INF/jsp/
spring.view.suffix: .jsp

这是我的MvcConfig课程:

package com.goodvideotutorials.spring.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("home");
    }

}

这是我的home.jsp:

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Up and running with Spring Framework quickly</title>
</head>
<body>
    <h2>Hello, world!</h2>
</body>
</html>

它被插入src&gt;主要&gt; webapp&gt; WEB-INF&gt; jsp&gt;针对home.jsp

我已将这些依赖项添加到pom.xml:

<dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency> 

这是我的Application.java类:

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application extends SpringBootServletInitializer {
       public static void main(String[] args)  {
           ApplicationContext ctx = SpringApplication.run(Application.class, args);
    }
}

和我的控制器类:

@Controller
public class RootController {

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

}

如果我在控制器中对代码进行了注释并且不使用MvcConfig,它无论如何都行不通。如果我评论该代码并使用MvcConfig类,它也不起作用。这是url:localhost:8080

我刚试了很多东西,但它显示了&#34; Whitelabel错误页面&#34;而不是JSP。我还在JEE环境中安装了Tomcat服务器。这会导致问题吗?

3 个答案:

答案 0 :(得分:0)

Spring boot会自动帮助您:

添加文件src/main/resources/application.properties

spring.view.prefix: /WEB-INF/jsp/ 
spring.view.suffix: .jsp

依赖:

<dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
</dependency>

或者您应该为视图解析器添加配置,例如:

@Configuration
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter{
    @Bean
    public ViewResolver getViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");
        return resolver;
    }

    @Override
    public void configureDefaultServletHandling(
            DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }    
}

更改您的应用程序,如:

public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

答案 1 :(得分:0)

按照以下步骤

1 - tomcat-embed-jasper依赖

$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = 'ci_sessions'; // Or sys_get_temp_dir()
$config['sess_match_ip'] = FALSE; // Or True
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE; // Or True

2 - 添加以下配置是application.properties

<dependency>
      <groupId>org.apache.tomcat.embed</groupId>
      <artifactId>tomcat-embed-jasper</artifactId>
      <scope>provided</scope>
</dependency>

3 - 您要显示的任何JSP页面,以便在控制器中添加以下代码

spring.mvc.view.prefix: /
spring.mvc.view.suffix: .jsp

基于上面的代码,它将在webapps / welcome.jsp中查找welcome.jsp文件

那仍然有一些疑问,然后在链接下面查看

Spring Boot and JSP Integration

答案 2 :(得分:0)

我的2美分。

对我来说,我在服务器日志中收到以下警告:

WARN  : ResourceHttpRequestHandler : Path with "WEB-INF" or "META-INF": [WEB-INF/jsp/welcome.jsp]

并显示了白标错误页面,要修复它,我修改了我的JSP位置

来自

src/main/webapp/WEB-INF/jsp/welcome.jsp

src/main/webapp/templates/jsp/welcome.jsp

为什么对我有用?

Spring文档明确指出ResourceHttpRequestHandler将拒绝包含WEB-INFMETA-INF的所有URL。

ResourceHttpRequestHandler doc link

  

来自DOCS:标识无效的资源路径。默认情况下拒绝:   包含“ WEB-INF”或“ META-INF”的路径

因此,一旦我用模板替换了WEB-INF文件夹名称,它便开始工作。

注意: 我还为内部视图解析器配置了必需的前缀和后缀。 我正在使用spring-boot-starter-parent-2.1.2.RELEASE