【问题标题】:Spring boot jsp is not renderingSpring Boot jsp 不渲染
【发布时间】:2018-02-05 13:53:47
【问题描述】:

Jsp 显示为默认文本image

我的 index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>HelloWorld</h1>
</body>
</html>

我的索引控制器

@Controller
public class IndexController {

    @RequestMapping("/")
    public String index(Model model) {
        return "index";
    }

}

网页配置

@Configuration
@EnableWebMvc
@ComponentScan("ru.hyndo.web")
public class WebConfig extends WebMvcConfigurerAdapter {
    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }


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

网络应用初始化器

public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[] { RootConfig.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] { WebConfig.class };
    }

}

根配置

@Configuration
@ComponentScan(basePackages = { "ru.hyndo.web" }, excludeFilters = {
        @ComponentScan.Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class) })
public class RootConfig {
}

应用

@SpringBootApplication
public class DemoApplication {

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

pom.xml

<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>demo</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.10.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>javax.servlet.jsp-api</artifactId>
        <version>2.3.1</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>javax.servlet.jsp.jstl</groupId>
        <artifactId>javax.servlet.jsp.jstl-api</artifactId>
        <version>1.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

我尝试了一切,但没有任何效果。如果您能提供一些工作示例,请提供。

另外,如果您需要我在想法中的项目结构 - http://prntscr.com/iaemoc

我被困在这里,感觉我永远都做不到 :( 在没有帮助的情况下,我查看了大量关于 * 的教程和尽可能多的问题。

非常感谢任何建议!

【问题讨论】:

  • 对于 JSP 的初学者,您需要 war 而不是 jar 并阅读 the documentation
  • 您在启动期间在日志或控制台中看到了什么?您是否看到任何错误?
  • 我尝试将 jar 更改为 war 但我仍然遇到同样的问题
  • 日志中没有任何错误
  • 您的项目结构中的 WEB-INF.views 是什么?它应该是 WEB-INF/views

标签: java spring maven jsp spring-boot


【解决方案1】:

war 项目中,JSP 页面由 src/main/webapp/WEB-INF/ 提供。

Jar 项目中,JSP 页面 不能 只需从 webapp 位置或 src/main/resources/ 提供服务。这是因为boot ref docs 中所述的限制。

位置src/main/webapp/WEB-INF 可能会以分解形式工作,但应避免使用。来自引导参考文档:

如果您的应用程序将被打包为 jar,请不要使用 src/main/webapp 目录。虽然这个目录是一个通用标准,但它只适用于 war 打包,如果你生成一个 jar,它会被大多数构建工具默默地忽略。

幸运的是,Jar 项目还有另一种选择:Servlet 3.0 规范允许在 src/main/resources/META-INF/resources/ 中拥有动态页面(请查看示例 here)。

SOURCE

【讨论】:

  • 它是 web-inf/views 但想法是渲染 WEB-INF.views。我试过你的代码,但还是不行