Java spring引导中呈现JSP的问题

Java spring引导中呈现JSP的问题,java,spring,jsp,spring-boot,Java,Spring,Jsp,Spring Boot,我有一个springboot应用程序,我想在其中使用jsp 以下是我在pom.xml中的依赖项 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.M2</version> <relativ

我有一个springboot应用程序,我想在其中使用jsp

以下是我在pom.xml中的依赖项

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

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.jsoup</groupId>
        <artifactId>jsoup</artifactId>
        <version>1.8.3</version>
    </dependency>
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.5.0-b01</version>
    </dependency>
    <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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</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>
</dependencies>

尝试将此属性添加到
application.properties
src/main/resources/
):

将jsp放到
/WEB-INF/jsp/

在控制器返回中,例如“index”,您应该在该文件夹中有index.jsp
/WEB-INF/jsp/

确保您的配置如下所示,并且存在@EnableWebMvc

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

    @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();
        }
    }
更新 删除为这些依赖项提供的范围:

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

org.apache.tomcat.embed

你曾经解决过你的问题吗?我假设上面提到的大多数人都是使用IDE来完成项目的。当您将项目作为jar运行时,情况就大不相同了

我们的JSP正在使用中 //project//src/main/resources/META-INF/resources/WEB-INF/jsp/index.jsp 你们所有的pom都和我们的一样。 然而,我发现SpringBoot1.4.3及以上版本停止支持嵌入式JSP,也许他们想强迫我们使用thymeleaf;)
我刚刚恢复到Spring boot 1.4.1,JSP在jar中可以完美地工作。

这就是我如何在使用嵌入式tomcat(打包=>jar)的SpringBoot应用程序中添加JSP支持的原因:

1) 在pom.xml中添加所需的依赖项:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency><!-- Optional, use this only when you need spring security taglibs -->
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-taglibs</artifactId>
</dependency>
3) 配置视图解析程序:

@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

@Value("${spring.mvc.view.prefix}")
private String prefix;

@Value("${spring.mvc.view.suffix}")
private String suffix;

/**
 * Configures view resolver for jsp views.
 */
@Bean
public InternalResourceViewResolver setupViewResolver() {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix(prefix);
    resolver.setSuffix(suffix);
    resolver.setViewClass(JstlView.class);
    return resolver;
}
}
4) 在src/main/webapp/WEB-INF/目录中创建jsp视图


您可以在我的GitHub上找到完整的工作示例:

您是否阅读/遵循了(特别是提到的)很抱歉没有将此发布到我的问题中-我的config@Bean public viewsolver getviewsolver(){internalresourceviewsolver=new internalresourceviewsolver();resolver.setPrefix(“/WEB-INF/jsp/”);resolver.setSuffix(“.jsp”);return resolver;}和我的jsp在/WEB-INF/jsp/folderI中。请看,这个类是用@Configuration注释的?是扫描的吗?尝试向此方法getViewResolver()添加一些跟踪。它用@Configuration标记。方法getViewResolver已输入,但当我尝试记录resolver.getApplicationContext()时,我得到一个错误-ApplicationObjectSupport实例[org.springframework.web.servlet.view]。InternalResourceViewResolver@7c71c889]不在应用程序中运行。谢谢您的建议,我现在将尝试在这个方向上进行研究,不幸的是,如果我尝试@injectapplicationcontext,我没有得到任何错误,方法getviewsolver()被调用,但是jsp页面仍然呈现不正确:(我还探索如何使用SpringBoot(1.5.x)从JAR提供jsp)。我遇到了几个例子,说明将JSP放在src/main/resources/META-INF/resources/中并提供适当的前缀和后缀JSP是可以实现的。我也做了同样的事情,并且能够从IDE而不是从打包的JAR中实现它。正如您已经提到的,springboot可能已经删除了对JAR提供JSP的支持。我正在寻找获取官方声明/公告,但找不到。您是否有来自spring社区的此类链接?
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

    @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();
        }
    }
<dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency><!-- Optional, use this only when you need spring security taglibs -->
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-taglibs</artifactId>
</dependency>
spring:
  mvc:
    view:
      prefix: /WEB-INF/
      suffix: .jsp
@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

@Value("${spring.mvc.view.prefix}")
private String prefix;

@Value("${spring.mvc.view.suffix}")
private String suffix;

/**
 * Configures view resolver for jsp views.
 */
@Bean
public InternalResourceViewResolver setupViewResolver() {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix(prefix);
    resolver.setSuffix(suffix);
    resolver.setViewClass(JstlView.class);
    return resolver;
}
}