1.打包为war包 页面使用jsp文件
pom.xml
pom文件需要指定打包方式为war,build标签里面指定<finalName>标签为最终生成的war包的名字
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency> <!--处理jsp的依赖-->
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/webapp</directory>
<targetPath>META-INF/resources</targetPath> <!--将directory中的文件编译到这个目录下面去-->
<includes>
<include>**/*.*</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
controller 文件
@Controller public class JspController { @RequestMapping("/f1") public String f1(Model model){ model.addAttribute("data","这里是springboot打包为war包"); return "index"; } }
application文件
server.port=9001 server.servlet.context-path=/mywar spring.mvc.view.prefix=/ ###前缀 spring.mvc.view.suffix=.jsp ###后缀
主启动类
@SpringBootApplication //集成这个类才能使用独立的tomcat服务器,重写这个方法 public class SpringbootWarApplication extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(SpringbootWarApplication.class, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(SpringbootWarApplication.class); } }
index.jsp文件 在src/main/webapp目录下,webapp目录需要新建,新建后需要处理,FILE--->PROJECT STRUCTURE,选择web,选择关联的目录
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> 获取一下数据吧 ${data} </body> </html>
maven package之后,就可以获取到生成的war包了,后期可以把war包放到tomcat服务器的webapps目录下发布了,有可能出现tomcat的版本和war包,不一致的情况,需要调查。
打包为jar包
pom.xml文件
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<!--打包后的文件名-->
<finalName>myboot</finalName>
<!--加入resource插件-->
<resources>
<resource>
<!--把src/main/webapp目录下的文件编译到META-INF/resources 这个目录下-->
<directory>src/main/webapp</directory>
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/*.*</include>
</includes>
<filtering>true</filtering>
</resource>
<!--把src/main/resouces目录下的所有文件编译到classpath目录中-->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
<filtering>true</filtering>
</resource>
<!--如果mybatis的mapper.xml文件在src/main/java下,把这些xml文件同步编译过去-->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--打包插件,如果jar包中包含jsp文件时,必须指定版本号为 1.4.2.RELEASE,其他都不好用-->
<version>1.4.2.RELEASE</version>
</plugin>
</plugins>
</build>
controller文件
@Controller public class MyController { @RequestMapping("/ff1") public ModelAndView f1(ModelAndView modelAndView){ modelAndView.addObject("data","springboot打包为jar包"); modelAndView.setViewName("index"); return modelAndView; } }
主启动类
@SpringBootApplication public class SpringbootJarApplication { public static void main(String[] args) { SpringApplication.run(SpringbootJarApplication.class, args); } }
application配置
server.port=9002 server.servlet.context-path=/myjar spring.mvc.view.prefix=/ spring.mvc.view.suffix=.jsp
index.jsp 在webapp目录下,webapp目录需要配置,和war包中的一样
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> 获取数据 ${data} </body> </html>
打包后就可以 使用 java -jar myboot.jar 启动服务
log4j 是 log for java ,配置文件是 log4j.properties
log4j2 是 比 log for java 的第二版 ,配置文件是 log4j.xml
logback 是最新的 配置文件是 在类路径下 logback-spring.xml
logback-spring.xml 示例
<?xml version="1.0" encoding="UTF-8"?> <configuration scan="true" scanPeriod="60 seconds" debug="false"> <!-- <contextName>logback</contextName>--> <!-- 日志脱敏--> <!-- <conversionRule conversionWord="msg" converterClass="com.ccbhousing.zo.qd.support.log.CustomMessageConverter">--> <!-- </conversionRule>--> <!-- 控制台输出 --> <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern> </encoder> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>DEBUG</level> <!-- 大于该级别的日志都输出 --> <!-- TRACE < DEBUG < INFO < WARN < ERROR --> </filter> </appender> <!-- 时间滚动输出 level为 INFO 日志 --> <appender name="file—info" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>INFO</level> <!--下面这两行是只有INFO级别的日志才写入到文件中--> <onMatch>ACCEPT</onMatch> <onMismatch>DENY </onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <FileNamePattern>./logs/service-qd/info.%d{yyyy-MM-dd}.log</FileNamePattern> <MaxHistory>30</MaxHistory> </rollingPolicy> <encoder> <pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern> </encoder> </appender> <!-- 时间滚动输出 level为 ERROR 日志 --> <appender name="file—error" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>DEBUG</level> <!-- 大于该级别的日志才输出 --> <!-- <onMatch>ACCEPT</onMatch>--> <!-- <onMismatch>DENY </onMismatch>--> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <FileNamePattern>./logs/service-qd/error.%d{yyyy-MM-dd}.log</FileNamePattern> <MaxHistory>30</MaxHistory> </rollingPolicy> <encoder> <pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern> </encoder> </appender> <appender name="remote—invoke" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>INFO</level> <!-- <onMatch>ACCEPT</onMatch>--> <!-- <onMismatch>DENY </onMismatch>--> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <FileNamePattern>./logs/service-qd/remote.%d{yyyy-MM-dd}.log</FileNamePattern> <MaxHistory>30</MaxHistory> </rollingPolicy> <encoder> <pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern> </encoder> </appender> <root level="info,ERROR"> <!--这里必须引用上面的appender,才生效,root里面配置了level,则以root这里的日志级别为准,root没有配置level,则默认是debug,则以各自appender的日志级别为准 --> <appender-ref ref="stdout" /> <appender-ref ref="file—error" /> <appender-ref ref="file—info"/> </root> <!-- 这是mybatis,sql语句的输出--> <logger name="com.example.springbootmybatis1.mapper" level="DEBUG"> </logger> <!-- <logger name="com.alibaba.nacos.client" level="OFF"></logger>--> <!-- <logger name="org.flowable" level="ERROR"></logger>--> <!-- <logger name="com.ccbhousing.core.cloud.version.CCBHousingRequestMappingHandlerMapping" level="ERROR"></logger>--> <!-- <logger name="org.springframework" level="WARN"></logger>--> <!-- <logger name="com.ccbhousing.core.launch.StartEventListener" level="INFO"></logger>--> <!-- <logger name="com.ccbhousing.core.log.aspect.RequestLogAspect" level="INFO"></logger>--> <!-- <logger name="com.ccbhousing.core.secure.interceptor.PermissionInterceptor" level="ERROR"></logger>--> <!-- <logger name="com.ccb.property.channel.support.http.BaseHttpClient" level="INFO">--> <!-- <appender-ref ref="remote—invoke" />--> <!-- </logger>--> </configuration>
我们在代码中想使用 log写入日志,必须在pom文件中加入 lombok的依赖,在要使用log的类上加上 @Slf4j 注解后,在类中 使用 log.info() , log.debug() 就行了