JavaWeb_(Struts2框架)Log4j的配置以及解决中文乱码

 

 

  此系列博文基于同一个项目已上传至github  传送门

  JavaWeb_(Struts2框架)Struts创建Action的三种方式  传送门

  JavaWeb_(Struts2框架)struts.xml核心配置、动态方法调用、结果集的处理  传送门

  JavaWeb_(Struts2框架)Log4j的配置以及解决中文乱码  传送门

  JavaWeb_(Struts2框架)参数传递之接收参数与传递参数  传送门

  JavaWeb_(Struts2框架)Ognl小案例查询帖子  传送门

  JavaWeb_(Struts2框架)Action中struts-default下result的各种转发类型  传送门

  JavaWeb_(Struts2框架)拦截器interceptor  传送门

 

 

解决web项目中文乱码问题

  在项目目录WebContent->WEB-INF->web.xml下<filter>中再配置两个节点

      <init-param>
          <param-name>encoding</param-name>
          <param-value>UTF-8</param-value>
      </init-param>

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>StrutsForum_Login</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <filter>
      <filter-name>struts</filter-name>
      <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
        
      <init-param>
          <param-name>encoding</param-name>
          <param-value>UTF-8</param-value>
      </init-param>
      
  
  </filter>
  <filter-mapping>
      <filter-name>struts</filter-name>

      <url-pattern>/*</url-pattern>
  </filter-mapping>
  
</web-app>
web.xml

 

 

Log4j的配置

  项目可以添加log4j-api-2.11.0和log4j-core-2.11.0的jar包

  配置log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
    <Configuration>
        <Appenders>
            <Console name="STDOUT" target="SYSTEM_OUT">
                <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
            </Console>
            <RollingFile name="RollingFile" fileName="logs/strutslog1.log"
                         filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
                <PatternLayout>
                    <Pattern>%d{MM-dd-yyyy} %p %c{1.} [%t] -%M-%L- %m%n</Pattern>
                </PatternLayout>
                <Policies>
                    <TimeBasedTriggeringPolicy />
                    <SizeBasedTriggeringPolicy size="1 KB"/>
                </Policies>
                <DefaultRolloverStrategy fileIndex="max" max="2"/>
            </RollingFile>
        </Appenders>
        <Loggers>
            <Logger name="com.opensymphony.xwork2" level="WAN"/>
            <Logger name="org.apache.struts2" level="WAN"/>
            <Root level="warn">
                <AppenderRef ref="STDOUT"/>
            </Root>
        </Loggers>
     
     
    </Configuration>
log4j2.xml

 

posted @ 2019-09-23 16:21 Cynical丶Gary 阅读(...) 评论(...) 编辑 收藏