花了一天的时间,完成了基本框架的搭建,可以实现简单功能,并进行了简单的设计。
1。在web.xml中
<servlet>
<servlet-name>post</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>post</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 设置过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<!-- 加载spring的配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/post-servlet.xml
/WEB-INF/config/applicationContext-ibatis.xml;
/WEB-INF/config/applicationContext-dao.xml;
/WEB-INF/config/applicationContext-controller.xml
</param-value>
</context-param>
2。jdbc数据库连接,以后会切到数据源上
<!-- 导入配置文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="/WEB-INF/config/data_source.properties"/>
</bean>
<!-- 设置jdbc连接 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.name}"/>
<property name="password" value="${jdbc.pass}"/>
</bean>
<!-- 使用spring提供的sqlMapClient -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="/WEB-INF/config/SqlMapConfig.xml"></property>
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>
3。jdbc连接配置,快速切换数据库连接,挺频繁的一个操作,data_source.properties
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:HONGMING
name=post_kf
pass=post_kf
4.dao层的配置,只是一个测试代码
<?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.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
default-autowire="byName">
<!-- 采用继承SqlMapClientDaoSupport的方式对spring和ibatis进行整合,加入了default-autowire="byName"的方式,减少配置文件 -->
<bean id="teacherDao" class="cn.com.pansky.dao.TeacherDAOImpl"></bean>
</beans>
5。controller层的配置
<!-- 为请求定制跳转,以.do结尾的请求都可以经过此spring的请求链上做2次跳转请求 -->
<bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/test.do=teacherController
</value>
</property>
</bean>
<!-- 配置跳转 -->
<bean id="teacherController" class="cn.com.pansky.web.TeacherController">
<property name="methodNameResolver">
<ref bean="methodNameResolver"/>
</property>
<property name="teacherDao">
<ref bean="teacherDao"/>
</property>
</bean>
6。spring的servlet所需的配置文件
<!-- spring的methodNameResolver的实现。配合controller使用,用来判定参数名称。 -->
<bean id="methodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName" value="method"/>
<property name="defaultMethodName" value="list"/>
</bean>
<!-- 定制跳转视图 -->
<bean id="resolverView" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
</bean>