springboot+thymeleaf打war包在外部tomcat运行

工程目录

 

pom文件注意点

<packaging>war</packaging>
<dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-tomcat</artifactId>
          <scope>provided</scope>
        </dependency>
<build>
        <finalName>prs</finalName>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
          </plugin>
        </plugins>
    </build>

配置文件application.properties

server.port=8013
server.session.timeout=3000
server.context-path=/prs

#thymelea模板配置
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false
#spring.resources.chain.strategy.content.enabled=true
#spring.resources.chain.strategy.content.paths=/**

# 上传文件大小配置
spring.http.multipart.maxFileSize=10MB
spring.http.multipart.maxRequestSize=10MB

#spring.mvc.async.request-timeout=600000
#spring.http.multipart.max-request-size=200MB

#spring.aop.auto=true
#spring.aop.proxy-target-class=false

#server.tomcat.uri-encoding=UTF-8
#server.tomcat.max-threads=100
logging.config=classpath:logback.xml

#mybatis.configLocation=classpath:mybatis/mybatis-config.xml
#mybatis.mapperLocations=classpath:mybatis/mapper/*.xml

#spring.datasource.driverClassName = com.mysql.jdbc.Driver  
#com.microsoft.sqlserver.jdbc.SQLServerDriver
#spring.datasource.url = jdbc:mysql://127.0.0.1:3306/test
#jdbc:sqlserver://104.15.202.101:1105;DatabaseName=tt
#spring.datasource.username = root
#spring.datasource.password = 123456

#spring.datasource.initialSize=2
#spring.datasource.minIdle=0
#spring.datasource.maxActive=5
#spring.datasource.maxWait=60000
#spring.datasource.validationQuery=select 1
#spring.datasource.timeBetweenEvictionRunsMillis=60000
#spring.datasource.testWhileIdle=true
#spring.datasource.testOnBorrow=true
#spring.datasource.testOnReturn=false
#spring.datasource.poolPreparedStatements=true
#spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
server.context-path=/prs这里的路径名最好和上面pom里的finalName最好是一致的,因为当我们把打包好的war包放到tomcat的webapp里时,访问路径前缀就是这个finalName

启动类

@ComponentScan(basePackages= {"com.wymessi"})//扫描组件
@SpringBootApplication
@EnableAspectJAutoProxy
@EnableTransactionManagement(proxyTargetClass = true)
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class})
public class SpringbootApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootApplication.class, args);
    }
    
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        // 注意这里要指向原先用main方法执行的Application启动类
        return builder.sources(SpringbootApplication.class);
    }

//    @Bean
//    public HttpMessageConverters fastJsonHttpMessageConverters() {
//        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//        FastJsonConfig fastJsonConfig = new FastJsonConfig();
//        // fastJsonConfig.setSerializerFeatures(SerializerFeature.BrowserCompatible);
//        // fastJsonConfig.setSerializerFeatures(SerializerFeature.BrowserSecure);
//        fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect);
//        // fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue);
//        SerializeConfig config = new SerializeConfig();
//        config.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));
//        fastJsonConfig.setSerializeConfig(config);
//        fastConverter.setFastJsonConfig(fastJsonConfig);
//        HttpMessageConverter<?> converter = fastConverter;
//        return new HttpMessageConverters(converter);
//    }

}

和打jar包的区别是继承了SpringBootServletInitializer这个类,然后重写了configure方法。

运行程序

eclipse里运行

右击项目名,run as - spring boot app。

浏览器里输入http://127.0.0.1:8013/prs

打包成war包在tomcat里运行

右击项目名,run as - maven - build,看到success就代表打包成功了。

把打包好的prs放到tomcat的webapp里,然后启动tomcat。

这里要注意,tomcat的访问端口要设置成和配置文件里的一样,不然用配置文件里的port是访问不到的。

打包的名称要和server.context-path=/prs的名称一致,不然也是访问不到的,这个上面已经说过了。

浏览器里输入http://127.0.0.1:8013/prs

posted @ 2018-09-04 23:02  夏威夷8080  阅读(889)  评论(0编辑  收藏  举报