SpringBoot使用jsp

①,項目要打成war包java

②,配置InternalResourceViewResolverweb

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer{
	
	@Override
	public void configureViewResolvers(ViewResolverRegistry registry) {
//配置視圖映射:默認爲"/WEB-INF/", ".jsp"
		registry.jsp();
	}

}

③,配置類繼承SpringBootServletInitializer 並重寫其configure,將SpringBoot的啓動類傳入spring

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 SpringbootJspApplication extends SpringBootServletInitializer{

	@Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        //告訴程序SpringBoot的啓動類是哪一個
        return application.sources(SpringbootJspApplication.class);
    }
	
	public static void main(String[] args) {
		SpringApplication.run(SpringbootJspApplication.class, args);
	}
}
相關文章
相關標籤/搜索