年终总结spring mvc 代码篇结合之前写的

这个例子比较简单 实现的是spring mvc+jdbctemplate.下面有空会加入hibernate的实现。后续会加入quartz持久化的实现。lucene的简单用法等。

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SSS</display-name>
  <context-param>
      <param-name>webAppRootKey</param-name>
      <param-value>webapp.root</param-value>
  </context-param>
   <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>
		/WEB-INF/applicationContext.xml  
		/WEB-INF/SSS-dao.xml
		/WEB-INF/SSS-service.xml  
      </param-value>
  </context-param>
   <context-param>
	  <param-name>log4jConfigLocation</param-name>
	  <param-value>/WEB-INF/classes/log4j.properties</param-value>
 	</context-param>
  <listener>
      <description>springContextListener</description>
      <listener-class >org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
    <listener>
      <description>springContextListener</description>
      <listener-class >org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>
  <servlet>
      <description>spring filter servlet</description>
      <servlet-name>SSS</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>
              /WEB-INF/SSS-servlet.xml
              /WEB-INF/SSS-controller.xml
          </param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
  </servlet>
  <!-- 在web应用关闭的时候,清除JavaBeans Introspector的监听器;
  可以保证在web 应用关闭的时候释放与掉这个web 应用相关的class loader 
  和由它管理的类 -->
  <listener>
     <listener-class>
         org.springframework.web.util.IntrospectorCleanupListener
     </listener-class>
 </listener>
 
  <servlet-mapping>
       <servlet-name>SSS</servlet-name>
       <url-pattern>*.do</url-pattern>
  </servlet-mapping>
   <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

</web-app>


下面是webxml中提到的所有配置文件xml都是在WEB-INF文件夹下

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:p="http://www.springframework.org/schema/p"  xmlns:tx="http://www.springframework.org/schema/tx"  xmlns:aop="http://www.springframework.org/schema/aop" 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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
  <!--  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
       <property name="driverClassName" value=""/>
       <property name="url" value=""/>
       <property name="username" value=""/>
       <property name="password" value=""/>
   </bean>
    -->
   <context:property-placeholder location="classpath:*.properties" />
   <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
       <property name="driverClass" value="${c3p0.driverClass}"/>
       <property name="jdbcUrl" value="${c3p0.jdbcUrl}"/>
       <property name="user" value="${c3p0.user}"/>
       <property name="password" value="${c3p0.password}"/>
       <property name="minPoolSize" value="${c3p0.maxPoolSize}"/>
       <property name="initialPoolSize" value="${c3p0.initialPoolSize}"/>
       <property name="maxPoolSize" value="${c3p0.maxPoolSize}"/>
       <property name="maxIdleTime" value="${c3p0.maxIdleTime}"/>
       <property name="acquireIncrement" value="${c3p0.acquireIncrement}"/>
   </bean>
   <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
       <property name="dataSource" ref="dataSource"/>
       <property name="fetchSize" value="30"/>
   </bean>
   
   <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       <property name="dataSource" ref="dataSource"></property>
   </bean>
   
    <tx:annotation-driven transaction-manager="transactionManager"  /> 
     <!-- <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="converters">
            <list>
                <bean class="com.sss.util.binder.DateFormatConvert"></bean>
            </list>
        </property>
    </bean>
    <bean id="webBindingInitializer" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
        <property name="conversionService" ref="conversionService"></property>
    </bean>-->
  
</beans>


SSS-controller.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: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 
    ">
    <bean id="methodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver ">
     <property name="paramName" value="method"></property>   
    </bean>
   <bean name="/test/testController.do" class="com.sss.controller.TestController">
     <property name="methodNameResolver">
         <ref bean="methodNameResolver"/>
     </property>   
     <property name="dao" ref="testDao"/>
   </bean>
   <bean name="/jsp/userInfoController.do" class="com.sss.controller.UserInfoController">
       <property name="methodNameResolver">
           <ref bean="methodNameResolver"/>
       </property>
       <property name="userInfoService" ref="userInfoService"></property>
   </bean>
   
</beans>

SSS-dao.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:tx="http://www.springframework.org/schema/tx"  xmlns:aop="http://www.springframework.org/schema/aop" 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/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
    http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
   <bean id="testDao" class="com.sss.dao.TestDAO" scope="prototype">
       <property name="jdbcTemplate" ref="jdbcTemplate"/>
   </bean>
   <bean id="userInfoDao" class="com.sss.dao.impl.UserInfoDAOImpl" scope="prototype">
       <property name="jdbcTemplate" ref="jdbcTemplate"/>
   </bean>
  
</beans>

SSS-service.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:tx="http://www.springframework.org/schema/tx"  xmlns:aop="http://www.springframework.org/schema/aop" 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/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
    http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
   <bean id="userInfoService" class="com.sss.service.impl.UserInfoServiceImpl">
       <property name="userInfoDao" ref="userInfoDao"></property>
   </bean>
  
</beans>

SSS-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: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 
    ">
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value=""></property>
        <property name="suffix" value=""></property>
    </bean>
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
        <property name="interceptors">
            <list>
    <bean class="com.sss.intorceptor.MyIntorceptor"></bean>               
            </list>
        </property>
    </bean>
   
 <!--spring mvc 3 支持convert接口的强类型转换,但是2.x不支持,只支持PropertyEditor的实现
 <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="webBindingInitializer" ref="webBindingInitializer"></property>
    </bean> -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  <!--设置上传文件的最大尺寸为10MB-->
    <property name="maxUploadSize">
      <value>10485760</value>
    </property>
   </bean>
   
</beans>

代码直接见附件源码下载地址

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值