springboot整合jsp遇到的问题

新公司要求用jsp写页面,但springboot好像是不支持jsp的,所以要自己配置,遇到几个问题后来解决了。

springboot整合jsp不能直接使用启动类启动。可以使用以下三种方式进行启动

1、mvn clean spring-boot:run;
2、java -jar XXX.war
3、外部tomcat

 在进行项目开发过程中,可能需要调试,所以我就用了外部tomcat启动项目。

接下来介绍springboot整合jsp的完整过程

这是项目的完整结构:

springboot整合jsp遇到的问题_第1张图片

需要自己手动添加webapp文件下,再在webapp文件夹下新建WEB-INF,再在WEB-INF下新建jsp文件夹和web.xml

记住需要在对webapp文件夹进行设置,不然不生效

springboot整合jsp遇到的问题_第2张图片

1、pom.xml



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.0.1.RELEASE
		 
	
	com.shidengshu
	demo
	0.0.1-SNAPSHOT
	demo
	Demo project for Spring Boot
	
		1.8
	
	
		
			org.springframework.boot
			spring-boot-starter-web
		
		
		
			javax.servlet
			javax.servlet-api
			provided
		
		
		
			javax.servlet
			jstl
		
		
		
			org.springframework.boot
			spring-boot-starter-tomcat
		
		
		
			org.apache.tomcat.embed
			tomcat-embed-jasper
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
			
				
					org.junit.vintage
					junit-vintage-engine
				
			
		
		
			org.junit.jupiter
			junit-jupiter-api
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


2、application.propertyies

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

#关闭默认模板引擎
spring.thymeleaf.cache=false
spring.thymeleaf.enabled=false
server.port = 8089

3、对启动类进行修改

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(DemoApplication.class);
	}
	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}

4、编写controller类

@Controller
public class IndexController {
    @RequestMapping("/index")
    public String index(){
        return "index";
    }
}

 

在使用外部tomcat启动时,报了一个错误

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.example.SampleWebJspApplication]; nested exception is java.lang.IllegalStateException: Failed to introspect annotated methods on class org.springframework.boot.web.support.SpringBootServletInitializer

 at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:187) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]

 at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:321) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]

 at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:243) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]

 at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:273) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]

 at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:98) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]

 at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:681) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]

 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:523) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]

 at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]

 at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:369) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]

 at org.springframework.boot.SpringApplication.run(SpringApplication.java:313) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]

 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1185) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]

 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1174) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]

 at com.example.SampleWebJspApplication.main(SampleWebJspApplication.java:20) [classes/:na]

 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_77]

 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_77]

 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_77]

 at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_77]

 at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) [idea_rt.jar:na]

Caused by: java.lang.IllegalStateException: Failed to introspect annotated methods on class org.springframework.boot.web.support.SpringBootServletInitializer

 at org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:163) ~[spring-core-4.3.2.RELEASE.jar:4.3.2.RELEASE]

 at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:301) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]

 at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:237) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]

 at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:204) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]

 at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:173) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]

 ... 17 common frames omitted

 解决方案:

1、添加以下依赖



org.apache.tomcat.embed

tomcat-embed-jasper

2、删除其他依赖中的provided

完美解决了

启动后访问http://localhost:8089/index

springboot整合jsp遇到的问题_第3张图片

接下来就是要好好学习前端了,之前都是只专注于后端,现在要变成全栈开发工程师了,哈哈哈,接下来好好学习layui,毕竟项目中有需要

你可能感兴趣的:(springboot整合jsp遇到的问题)