一、目录结构
二、 具体步骤
2.1 applicationContext.xml 配置
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config /></beans>
2.2 mvc.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <mvc:annotation-driven/> <!-- 自动扫描包 --> <context:component-scan base-package="pr.cgl.controller" /> <!-- mvc返回页面的配置 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <!-- 模板路径为WEB-INF/pages/ --> <property name="prefix" value="/WEB-INF/"/> <!-- 视图模板后缀为.JSP --> <property name="suffix" value=".jsp"/> </bean> </beans>
2.3 web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>webAppRootKey</param-name> <param-value>SpringWebT1.root</param-value> </context-param> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:applicationContext.xml </param-value> </context-param> <!-- 启动spring 加载 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <!-- 加载springmvc --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 以.htm结尾的都被mvc拦截 --> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/SpringWebT1/*</url-pattern> </servlet-mapping> </web-app>
2.4 添加日志
2.4.1 配置log4j.properties
### Log4j Properties ### # Options are <DEBUG, INFO, WARN, ERROR or FATAL> # Use two appenders, one to log to console, another to log to a filelog4j.rootLogger=ALL,debug,info,error,console#log4j.rootLogger=debug,error,console#log4j.rootLogger=error,consolelog4j.threshold=ALL log4j.category.com.jlt.mis=debug,AdapterFilelog4j.appender.AdapterFile=org.apache.log4j.RollingFileAppenderlog4j.appender.AdapterFile.MaxFileSize=46MBlog4j.appender.AdapterFile.MaxBackupIndex=3log4j.appender.AdapterFile.File=${SpringWebT1.root}/logs/JLTShop.loglog4j.appender.AdapterFile.append=truelog4j.appender.AdapterFile.layout=org.apache.log4j.PatternLayoutlog4j.appender.AdapterFile.layout.ConversionPattern=%t %d{DATE} %c{3} %p - %m%n # WRITE LOG TO A CONSOLElog4j.appender.console=org.apache.log4j.ConsoleAppenderlog4j.appender.console.Target=System.outlog4j.appender.console.layout=org.apache.log4j.PatternLayout#log4j.appender.console.layout.ConversionPattern=[SpringWebT1] %d{MM/dd/yyyy HH:mm:ss,SSS} - %C{1}.%M (%F:%L) >> %p: %m%nlog4j.appender.console.layout.ConversionPattern=%d{MM HH:mm:ss,SSS} - %M (%F:%L) >> %p: %m%nlog4j.appender.console.threshold=DEBUG # WRITE LOG TO A FILE, ROLL THE FILE EVERY DAYlog4j.appender.info=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.info.layout=org.apache.log4j.PatternLayoutlog4j.appender.info.layout.ConversionPattern=%d{MM/dd/yyyy HH:mm:ss,SSS}-%C{1}.%M (%F:%L) >> %p: %m%nlog4j.appender.info.DatePattern=‘.‘yyyy-MM-ddlog4j.appender.info.threshold=INFOlog4j.appender.info.append=truelog4j.appender.info.File=${SpringWebT1.root}/logs/infoLogFile.log log4j.appender.debug=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.debug.layout=org.apache.log4j.PatternLayoutlog4j.appender.debug.layout.ConversionPattern=%d{MM/dd/yyyy HH:mm:ss,SSS} - %C{1}.%M (%F:%L) >> %p: %m%nlog4j.appender.debug.DatePattern=‘.‘yyyy-MM-ddlog4j.appender.debug.threshold=DEBUGlog4j.appender.debug.append=truelog4j.appender.debug.File=${SpringWebT1.root}/logs/debugLogFile.log log4j.appender.error=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.error.layout=org.apache.log4j.PatternLayoutlog4j.appender.error.layout.ConversionPattern=%d{MM/dd/yyyy HH:mm:ss,SSS} - %C{1}.%M (%F:%L) >> %p: %m%nlog4j.appender.error.DatePattern=‘.‘yyyy-MM-ddlog4j.appender.error.threshold=ERRORlog4j.appender.error.append=truelog4j.appender.error.File=${SpringWebT1.root}/logs/errorLogFile.log2.4.2 配置web.xml 添加以下代码段
<context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener>
2.5 controller
package pr.cgl.controller; import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping; /** * Created by CGL on 2015/9/10. */@Controller@RequestMapping("/main")public class TestController { @RequestMapping("/index.html") public String index(Model m){ m.addAttribute("name", "clg"); return "index"; }}2.6 测试 地址栏输入http://localhost:8080/SpringWebT1/main/index.html,得到Hello, clg
时间: 2024-10-04 23:06:18