首页 > 代码库 > SpringMVC 配置

SpringMVC 配置

目录结构:

 

web.xml

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app>    <context-param>        <param-name>log4jConfigLocation</param-name>        <param-value>/WEB-INF/log4j.properties</param-value>    </context-param>    <listener>        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>    </listener>    <servlet>        <servlet-name>extractengine</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    </servlet>    <servlet-mapping>        <servlet-name>extractengine</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping>    <welcome-file-list>        <welcome-file>./WEB-INF/index.jsp</welcome-file>    </welcome-file-list></web-app>

 

lo4j.properties在WEB-INF目录下

log4j.rootLogger=info,stdout,info,debug,errorlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=[%-5p] [%d{HH:mm:ss}] %c - %m%n log4j.logger.info=infolog4j.appender.info=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.info.layout=org.apache.log4j.PatternLayoutlog4j.appender.info.layout.ConversionPattern=[%-5p] [%d{HH:mm:ss}] %c - %m%nlog4j.appender.info.datePattern=‘.‘yyyy-MM-ddlog4j.appender.info.Threshold = INFO log4j.appender.info.append=truelog4j.appender.info.File=D:/home/work/workdir/apps/log/extractor-info.log log4j.logger.debug=debuglog4j.appender.debug=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.debug.layout=org.apache.log4j.PatternLayoutlog4j.appender.debug.layout.ConversionPattern=[%-5p] [%d{HH:mm:ss}] %c - %m%nlog4j.appender.debug.datePattern=‘.‘yyyy-MM-ddlog4j.appender.debug.Threshold = DEBUG log4j.appender.debug.append=truelog4j.appender.debug.File=D:/home/work/workdir/apps/log/extractor-debug.loglog4j.logger.error=errorlog4j.appender.error=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.error.layout=org.apache.log4j.PatternLayoutlog4j.appender.error.layout.ConversionPattern=[%-5p] [%d{HH:mm:ss}] %c - %m%nlog4j.appender.error.datePattern=‘.‘yyyy-MM-ddlog4j.appender.error.Threshold = ERROR log4j.appender.error.append=truelog4j.appender.error.File=D:/home/work/workdir/apps/log/extractor-error.log

extractengine-servlet.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:p="http://www.springframework.org/schema/p"    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"    xmlns:util="http://www.springframework.org/schema/util"    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                          http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">    <!-- Handler -->    <mvc:annotation-driven />    <context:component-scan base-package="com.baidu.extractengine.controllers" />    <mvc:resources location="/img/" mapping="/img/**"/>     <mvc:resources location="/js/" mapping="/js/**"/>    <mvc:resources location="/css/" mapping="/css/**"/>    <mvc:resources location="/fonts/" mapping="/fonts/**"/>    <!-- HandlerAdaptor -->    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >        <property name="messageConverters">            <util:list id="beanList">                <ref bean="mappingJacksonHttpMessageConverter"/>            </util:list>        </property>    </bean>    <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">        <property name="supportedMediaTypes">            <list>                <value>text/html; charset=UTF-8</value>            </list>        </property>    </bean>    <!-- ViewResolver -->    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix">            <value>/WEB-INF/jsp/</value>        </property>        <property name="suffix">            <value>.jsp</value>        </property>    </bean></beans>