首页 > 代码库 > 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.xsd10 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd11 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 <bean16 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="http://www.mamicode.com/提交">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.log10 log4j.appender.D.Append = true11 log4j.appender.D.Threshold = DEBUG 12 log4j.appender.D.layout = org.apache.log4j.PatternLayout13 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 @Controller2 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。