spring boot war包在tomcat下运行

java技术文章

2018-10-24

47

0

大家好,上一篇在spring boot集成struts2中说了怎么样集成struts2,但是在后面的开发中遇到一个问题,就是action无法跳转到jsp页面,但是jsp页面路径和配置都没有问题。经过两天问题查找,最后发现是spring boot启动方式存在问题,导致struts2-convention-plugin jar包中类DefaultResultMapBuilder中的servletContext.getResourcePaths方法获取不到JSP的路径导致。最后我的项目是换成了tomcat方式启动,才解决这个问题。

现在就说说spring boot 怎么样以war包形式启动。

一、引入maven包

其他组件包,大家根据自己的需要自行添加

<dependencies>
		<!-- Compile -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<!-- To build an executable war use one of the profiles below -->
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!--这里starter web就不要排除掉spring-boot-starter-tomcat了-->
		<!-- Provided -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>provided</scope>
		</dependency>
		<!-- Test -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

二、tomcat启动web项目配置

spring boot 类Application继承SpringBootServletInitializer

然后在Application类中重写当前方法,如下:

@Override   
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(WebApplication.class);
    }

这个就是tomcat启动的入口,相当于main方法中运行run一样。

可以这么理解:

以jar方式运行的话,通过main方法来启动。

以war方式运行的话,通过重写configure方法来实现。

下面是我的Application类:

/**
 * Project Name:mco.webbase
 * File Name:WebApplication.java
 * Package Name:com.mco
 * Date:2018年8月27日下午9:58:03
 * Copyright (c) 2018, 649445087@qq.com All Rights Reserved.
 *
*/

package com.mco;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.autoconfigure.security.servlet.SecurityFilterAutoConfiguration;
import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

/**
 * ClassName:WebApplication <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason:	 TODO ADD REASON. <br/>
 * Date:     2018年8月27日 下午9:58:03 <br/>
 * @author   LUOFUJIA
 * @version  
 * @jdk version JDK 1.8
 * @see 	 
 */
@SpringBootApplication
@EnableAutoConfiguration(exclude= {FreeMarkerAutoConfiguration.class,RestTemplateAutoConfiguration.class,SecurityAutoConfiguration.class,SecurityFilterAutoConfiguration.class})
@ComponentScan(basePackages= {"com.mco"})
@EnableJpaRepositories(basePackages = {"com.mco"})
@EntityScan(basePackages = {"com.mco"})
public class WebApplication  extends SpringBootServletInitializer  {
    
    private final static Logger logger = LoggerFactory.getLogger(WebApplication.class);
    
    public WebApplication(){
        
    }
    
	//这里以jar方式运行
    public static void main(String args[]) {
        logger.info("WebBaseApplication start..........................................");
        new WebApplication().run(args);
        logger.info("WebBaseApplication end..........................................");
    }

    public void run(String args[]) {
        SpringApplication application = new SpringApplication(WebApplication.class);
        application.run(args);
    }
    
	//这里以war方式运行
    @Override   
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(WebApplication.class);
    }
    
    @SuppressWarnings({"rawtypes", "unchecked" })
    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        StrutsPrepareAndExecuteFilter struts = new StrutsPrepareAndExecuteFilter();
        registrationBean.setFilter(struts);
        registrationBean.setOrder(1);
        return registrationBean;
    }
    

    
}

三、eclipse中配置tomcat运行spring boot

步骤:servers--》new---》server--》选择tomcat版本--》选择项目war包--》完成配置。

四、完成

欢迎访问:www.hongfu951.com博客,查看更多文章

发表评论

全部评论:0条

鸿福951

努力打造一个好用的webui

热评文章

推荐文章