springboot 开发一个jar 包运行的web项目,但是访问jsp的时候却出现了下载jsp的情况。
第一步 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--添加对jsp的编译支持--->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>compile</scope> <!--这个是关键信息-->
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jsp-api</artifactId>
<scope>compile</scope><!--这个是关键信息-->
</dependency>
第二步
@Bean
@Qualifier("viewResolver")
@ConditionalOnMissingBean(InternalResourceViewResolver.class)
public InternalResourceViewResolver defaultViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/views/");
resolver.setSuffix(".jsp");
return resolver;
}
第三步
package au.com.xxx.callinfo.capture;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@SpringBootApplication(scanBasePackages = {"au.com.xxx.callinfo.capture"})
@EnableTransactionManagement
public class CallInfoCaptureAppWebApplication {
@Bean
@Qualifier("viewResolver")
@ConditionalOnMissingBean(InternalResourceViewResolver.class)
public InternalResourceViewResolver defaultViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/views/");
resolver.setSuffix(".jsp");
return resolver;
}
public static void main(String[] args) {
try {
SpringApplication.run(CallInfoCaptureAppWebApplication.class, args);
}catch(Throwable e) {
e.printStackTrace();
}
}
}
第四步

在Spring Boot项目中,尝试运行一个基于jar的Web应用时,遇到访问JSP页面导致文件被下载的异常。文章介绍了从依赖检查到配置调整的四个步骤来解决这个问题。

863

被折叠的 条评论
为什么被折叠?



