Spring+SpringMVC+Log4J

Spring+SpringMVC+Log4J

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
    </context-param>

    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/props/log4j.properties</param-value>
    </context-param>

    <context-param>
        <param-name>log4jRefreshInterval</param-name>
        <param-value>6000</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

springmvc-servlet.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 4     xmlns:p="http://www.springframework.org/schema/p"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xmlns:mvc="http://www.springframework.org/schema/mvc" 
 7     xmlns:util="http://www.springframework.org/schema/util"
 8     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 9         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
10         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
11         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
12         
13         <context:component-scan base-package="com.controller"/>
14         
15         <bean
16         class="org.springframework.web.servlet.view.InternalResourceViewResolver"
17         p:order="100" p:viewClass="org.springframework.web.servlet.view.JstlView"
18         p:prefix="/WEB-INF/views/" p:suffix=".jsp" />
19         </beans>

main.jsp:

 1 <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 2 <html>
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 5 <title>Insert title here</title>
 6 </head>
 7 <body>
 8 欢迎${user.username}登录...
 9 </body>
10 </html>

login.jsp:

 1 <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 2 <html>
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 5 <title>Insert title here</title>
 6 </head>
 7 <body>
 8 <form action="login" method="post">
 9 姓名:<input type="text" name="username"><br>
10 密码:<input type="text" name="password"><br>
11 <input type="submit" value="提交">
12 </form>
13 </body>
14 </html>

log4j.properties:

 1 log4j.rootLogger = INFO ,  stdout ,  D 
 2 
 3 log4j.appender.stdout = org.apache.log4j.ConsoleAppender
 4 log4j.appender.stdout.Target = System.out
 5 log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
 6 log4j.appender.stdout.layout.ConversionPattern =  %d{yyyy-MM-dd HH:mm:ss} %5p %c{1}:%L - %m%n
 7  
 8 log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
 9 log4j.appender.D.File = ${webapp.root}\\WEB-INF\\log.log
10 log4j.appender.D.Append = true
11 log4j.appender.D.Threshold = DEBUG 
12 log4j.appender.D.layout = org.apache.log4j.PatternLayout
13 log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

User.java:

 1 public class User {
 2 
 3     private String username;
 4     private String password;
 5     public String getUsername() {
 6         return username;
 7     }
 8     public void setUsername(String username) {
 9         this.username = username;
10     }
11     public String getPassword() {
12         return password;
13     }
14     public void setPassword(String password) {
15         this.password = password;
16     }
17     
18 }

IndexController.java:

1 @Controller
2 public class IndexController {
3 
4     @RequestMapping("/index")
5     public String index(){
6         return "login";
7     }
8 }

LoginController.java:

 1 @Controller
 2 public class LoginController {
 3 
 4     @RequestMapping("login")
 5     public String login(@ModelAttribute("user")User user){
 6         Logger logger=Logger.getLogger(LoginController.class);
 7         logger.debug("this is a debug message");
 8         logger.info("this is a info message");
 9         logger.warn("this is a warn message");
10         logger.error("this is a error message");
11         logger.fatal("this is a fatal message");
12         
13         if(user.getUsername().equals("scott")&& user.getPassword().equals("tiger")){
14             return "main";
15         }else{
16             return "login";
17         }
18     }
19 }

Spring+SpringMVC+Log4J,布布扣,bubuko.com

Spring+SpringMVC+Log4J

上一篇:微信公共号自定义菜单。


下一篇:微信公共号H5页面跳转小程序。