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方法。html

運行程序

eclipse裏運行

右擊項目名,run as - spring boot app。mysql

瀏覽器裏輸入http://127.0.0.1:8013/prsweb

打包成war包在tomcat裏運行

右擊項目名,run as - maven - build,看到success就表明打包成功了。spring

把打包好的prs放到tomcat的webapp裏,而後啓動tomcat。sql

這裏要注意,tomcat的訪問端口要設置成和配置文件裏的同樣,否則用配置文件裏的port是訪問不到的。apache

打包的名稱要和server.context-path=/prs的名稱一致,否則也是訪問不到的,這個上面已經說過了。瀏覽器

瀏覽器裏輸入http://127.0.0.1:8013/prstomcat

相關文章
相關標籤/搜索