Little White felt springboot into a jar package directly using the inline Tomcat or jetty container (Java-jar Xxx.jar) Run the project is not conducive to positioning problems, I am still accustomed to look at Tomcat or Nginx log to locate the problem, today Xiao White on the springboot fight to deploy Javaweb project in Tomcat.
New Web Project Helloboot
Development tools: Spring tool Suite (STS)
File->new->spring Starter Project, as shown in:
The project structure is as follows:
Application.properties configuration:
server.port=8082server.context-path=/hellobootspring.devtools.restart.additional- Exclude=non-restart/**spring.resources.static-locations=classpath:/resources/static/,classpath:/ Static/spring.thymeleaf.cache=falselogging.level.org.springframework.security=info
Pox.xml(auto-generated, unmodified):
<?xml version="1.0"encoding="UTF-8"? ><project xmlns="http://maven.apache.org/POM/4.0.0"Xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>demo</artifactId> <ve Rsion>0.0.1-snapshot</version> <packaging>war</packaging> <name>helloboot</name> <descripti On>demo Project forSpring boot</description> <parent> <groupId>org.springframework.boot</groupId> &L T;artifactid>spring-boot-starter-parent</artifactid> <version>1.5. -. Release</version> <relativePath/> <!--lookup Parent fromRepository-</parent> <properties> <project.build.sourceencoding>utf-8</project.build.sourceEncoding> <project.reporting.outputencoding>utf-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupid>o Rg.springframework.boot</groupid> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-devtools</arti factid> <scope>runtime</scope> </dependency> <dependency> & Lt;groupid>org.springframework.boot</groupid> <ARTIFACTID>SPRING-BOOT-STARTER-TOMCAT</ARTIFAC tid> <scope>provided</scope> </dependency> <dependency> < ; GROUPID>ORG.SPRINGFRAmework.boot</groupid> <artifactId>spring-boot-starter-test</artifactId> <scope >test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactid> ;spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build></proj Ect>
Project Packaging
1) Create a new Servletinitializer.java under the same sibling directory as the project's startup class Hellobootapplication.java, and the code is as follows, and its location can refer to the project chart:
Package Com.example.demo;import Org.springframework.boot.builder.springapplicationbuilder;import Org.springframework.boot.web.support.SpringBootServletInitializer; Public class Servletinitializer extends Springbootservletinitializer { @Override protected Springapplicationbuilder Configure (Springapplicationbuilder application) { return Application.sources (hellobootapplication. Class);} }
2) package with Maven: Run as--maven install, as shown in package success, will be generated under target directory Demo-0.0.1-snapshot.war
Project deployment
Enter the WebApps subdirectory of Tomcat to upload and rename to Helloboot.war when uploading is complete:
Cd/root/tomcat/webapps
Rz
MV Demo-0.0.1-snapshot.war Helloboot.war
Modify the Tomcat profile Server.xml file to change 8080 port to 8082, and of course 8080 port is also possible, due to the 8080 port of the service is already occupied.
Enter Tomcat's Bin directory to start Tomcat:
Cd/root/tomcat/bin
SH startup.sh
To view Tomcat logs:
cd/root/tomcat/-F Catalina. out
From the log you can see the startup success:
Project Access
Access to the intranet can be accessed via the Xshell channel:
In the browser address bar, enter: Http://127.0.0.1:8082/helloboot/index, visible Project packaging published successfully.
Garbled processing
Today, small white in the test demo is found through the form submitted data garbled, there may be two reasons, a front-end submission to the background of the data background parsing garbled, the other is normal parsing but in the database when the database encoding caused by garbled. Small white on these two cases to test, the first case through the log to find the front end of the print to the back of the normal data, so small white to see the MySQL database encoding, as follows:
' Character_set_database ' ; +------------------------+--------+| Variable_name | Value |+------------------------+--------+| character_set_database | latin1 |+------------------------+------- -+1inset (0.01 sec)
Collect data try to find that as long as you add a line of code in MySQL my.cnf character-set-server=utf8, restart MySQL
[[email protected] etc]# vi/etc/my.cnf[[email protected] etc]# service mysqld restart
my.cnf:
[Email protected] etc]# cat/etc/my.cnf# for advice The Change settings please see# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html[mysqld]## Remove leading # andSetTo the amount of RAM forThe most important data# cacheinchMysql. Start at -% of Total RAM forDedicated server,Else Ten%. # innodb_buffer_pool_size=128m## Remove Leading # to turn on a very important data integrity option:logging# changes to the binary log between backups.# log_bin## Remove Leading # toSetOptions mainly useful forreporting servers.# The server defaults is faster fortransactions and fast selects.# Adjust sizes asneeded, experiment to find the optimal values.# join_buffer_size=128m# sort_buffer_size=2m# read_rnd_buffer_size=2Mdatadir=/var/lib/Mysqlsocket=/var/lib/mysql/mysql.sock# Disabling Symbolic-links isrecommended to prevent assorted security riskssymbolic-links=0character-Set-server=Utf8log-error=/var/log/Mysqld.logpid-file=/var/run/mysqld/mysqld.pid
After the modified MySQL database is encoded, the form submission data is also normal.
' Character_set_database ' ; +------------------------+-------+| Variable_name | Value |+------------------------+-------+| Character_set_database | UTF8 |+------------------------+-------+1inset (0.06 sec)
Springboot War Package Deployment Tomcat server, and form submission data garbled processing