以前一直用ssh2做东西,最近一阵子忽然想学习一下springMVC。据说设计理念也很不错的。我是个实用主义者,啥也不说,看看书,立马开始动手。
1、web.xml
这个是大家一开始都需要配置的
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
这个是我的初始化类,用来自动建立数据库的,可以不需要
<servlet> <servlet-name>initialServlet</servlet-name> <servlet-class>com.news.servlet.InitialServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet>
ok,web.xml完成了,我们来接着配置spring的一系列配置文件。
2、applicationContext系列配置,我个人的风格喜欢将不同作用的配置分门别类的放到不同的配置文件中
(1)applicationContext.xml,这个文件我仅做了一项配置,用于加载properties文件
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
<property name="ignoreInvalidKeys" value="true"></property>
properties文件
dataSource.driverClass=com.mysql.jdbc.Driver
dataSource.jdbcUrl=jdbc:mysql://localhost:3306/filesystem
dataSource.user=root
dataSource.password=root
(2)和hibernate的整合
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/news"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
<property name="minPoolSize" value="5"/>
<property name="maxPoolSize" value="60"/>
<property name="initialPoolSize" value="3"/>
<property name="maxIdleTime" value="60"/>
<property name="acquireIncrement" value="10"/>
<property name="idleConnectionTestPeriod" value="60"/>
<property name="acquireRetryAttempts" value="30"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLInnoDBDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.jdbc.batch_size">30</prop>
<prop key="hibernate.default_batch_fetch_size">30</prop>
</props>
</property>
<property name="packagesToScan" value="com.news.po"></property>
</bean>
3、事务的代理,因为我个人觉得事务的管理是一个比较独立的东西,是基于AOP的,所以把一些AOP的配置放一块了
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="logInterceptor" class="com.news.advice.LogInterceptor">
</bean>
<bean id="exceptionInterceptor" class="com.news.advice.ExceptionInterceptor"></bean>
4、以下是spring对DAO和serivce的管理,这个比较简单,随便贴个代码
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="baseDAO" class="com.news.dao.impl.BaseDAOImpl" abstract="true">
<property name="hibTemplate" ref="hibernateTemplate"></property>
</bean>
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="userService" class="com.news.service.impl.UserServiceImpl"> <property name="userDAO" ref="userDAO"></property> <property name="roleDAO" ref="roleDAO"></property> </bean> </beans>
5、加入springMVC的配置,最后一步
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" default-autowire="byName">
//springmvc中最常用的url-mapping方式,因为比较习惯通配符的做法,因此依照本人习惯,还是通配符,将匹配顺序置为第一。同时还可以给springMVC指定方法的解析模式,一种是采用指定参数的形式,另一种是根据url直接指定方法(所谓的属性匹配),我不用,写出来参考一下 <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="order" value="1"/> <property name="mappings"> <props> <prop key="index.htm">indexInit</prop> <prop key="*SysAdmin.htm">sysAdminManage</prop> <prop key="login*.htm">login</prop> </props> </property> </bean> <bean id="methodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver"> <property name="paramName" value="method"></property> </bean> <bean id="propetyResolver" class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver"> <property name="mappings"> <props> <prop key="/jcrsSearch.htm">jcrsSearch</prop> <prop key="/jcrsSearch2.htm">jcrsSearch2</prop> </props> </property> </bean>
6、这样的话可以在你的index.jsp中写下
<jsp:forward page="checkSysAdmin.htm"></jsp:forward>
那么springmvc的映射将会将路径映射到你的控制器中,进行请求处理了
暂时就写这么多,如果大家有好的学习经验,可以多多交流啊
633

被折叠的 条评论
为什么被折叠?



