SpringBoot应用程序中未呈现JSP页面

时间:2019-05-28 04:24:50

标签: spring-boot jsp

我看到了很多相同的问题,但是我的问题并没有用所有这些解决方案解决。我的JSP页面未呈现。

我添加了所有必需的jar,例如thymeleaf,tomcat,jstl,以运行应用程序,但仍然出现错误

下面是我的控制器

@RequestMapping("/rest/user")
public class BookResource {

@GetMapping("/recommend")
    public List<String> recommend() {
        return bookrepo.BookDetail(bookname);

    }
@RequestMapping(value = "/recommendation", method = RequestMethod.GET)
    public String recommend(Model model) {
        model.addAttribute("recommendations", bookrepo.BookDetail(bookname));
        model.addAttribute("name", "Harsh");
        return "recommendation";

    }
}

点击网址后,我收到以下错误消息:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Tue May 28 09:50:33 IST 2019
There was an unexpected error (type=Internal Server Error, status=500).
Error resolving template "recommendation", template might not exist or might not be accessible by any of the configured Template Resolvers

我创建的JSP页面未呈现,我将我的jsp页面放在资源/模板中,也放在WEB-INF / jsp /中。 并且属性文件也已更新

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

Pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.springBootApp</groupId>
    <artifactId>FirstSpringBootApp</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.3.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.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>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>3.3.6</version>
        </dependency>

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap-datepicker</artifactId>
            <version>1.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>1.9.1</version>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- Redis Dependencies -->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- GCP Dependencies -->

        <!-- <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-gcp-starter</artifactId>
            <version>1.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-gcp-starter-sql-mysql</artifactId>
            <version>1.0.0.RELEASE</version>
        </dependency> -->

    </dependencies>

    <build>
        <plugins>
             <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin> 

            <!-- GCP plugin -->
            <!-- <plugin>
                <groupId>com.google.cloud.tools</groupId>
                <artifactId>appengine-maven-plugin</artifactId>
                <version>1.3.2</version>
            </plugin> -->
        </plugins>
    </build>

</project>

结构。

Project Structure

3 个答案:

答案 0 :(得分:1)

Spring Boot does not support JSPs when using jar packaging。如果要使用JSP,则必须使用war包装。您可以通过将以下内容添加到pom.xml中来做到这一点:

<packaging>war</packaging>

通常将其放置在带有<version>标签的行之后。

进行了此更改后,您应该通过右键单击FirstSpringBootApp,然后选择Maven-> Update Project…,告诉Eclipse更新Maven项目。

使用战争包装将意味着src/main/webapp被Eclipse识别,然后应在src/main/webapp/WEB-INF/jsp中找到您的JSP文件。作为进一步参考,Spring Boot为您可能感兴趣的JSP提供了sample project

答案 1 :(得分:0)

对于jsp来说,根据spring boot的说法,Limitations是存在的。

如果未在pom.xml中声明包装,则默认情况下,spring boot会将其视为jar。

要克服这些限制,我们需要在应用程序中进行配置,以通过将jsp放在src / main / resources / META-INF / resources / WEB-INF / jsp文件夹下来呈现jsp。

示例代码:Click Here

参考文献:

https://dzone.com/articles/spring-boot-with-jsps-in-executable-jars-1

https://github.com/hengyunabc/spring-boot-fat-jar-jsp-sample

答案 2 :(得分:-1)

从堆栈跟踪中可以看到springboot无法解析模板“推荐” ,因此它与保存jsp文件的位置有关。

将jsp模板保留在/WEB-INF/jsp/下,因为这是application.properties中提供的内容。另外,请确保模板的扩展名本身就是.jsp,因为您也在属性文件中也指定了扩展名。

确保在pom中添加了以下依赖项:

<!-- JSTL -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>
<!-- To compile JSP files -->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>

由于您在此处使用jsp作为模板引擎,因此可以删除百里香依赖性。

如果无法正常工作,那么您可以尝试手动配置jsp视图解析器,例如:

@Configuration
public class MvcConfiguration implements WebMvcConfigurer
{
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/view/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        registry.viewResolver(resolver);
    }
}