要做毕业设计了 想了下实习的时候再用ssh框架 然后也有一点接触ssi框架 决定学习下ssi框架 然后做个毕业设计这样子 通过简单的搭建框架 也对ssi各个部分有了更加清晰的认识
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_3_0.xsd" version="3.0">
<display-name></display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/applicationContext-*.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.apache.struts2.dispatcher.ng.listener.StrutsListener</listener-class>
</listener>
<filter>
<filter-name>characterEncoding</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
applicationContext-web.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- 配置数据源,连接池采用的是c3p0,具体各参数代表意义参看c3p0自带的doc,非常详细。 -->
<!-- 引入参数配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/WEB-INF/ibatis/jdbc.properties</value>
</list>
</property>
</bean>
<!-- 数据源配制 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${jdbc.driverClass}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
<property name="minPoolSize" value="${jdbc.minPoolSize}" />
<property name="maxPoolSize" value="${jdbc.maxPoolSize}" />
<property name="maxIdleTime" value="${jdbc.maxIdleTime}" />
<property name="acquireIncrement" value="${jdbc.acquireIncrement}" />
<property name="maxStatements" value="${jdbc.maxStatements}" />
<property name="initialPoolSize" value="${jdbc.initialPoolSize}" />
<property name="idleConnectionTestPeriod" value="${jdbc.idleConnectionTestPeriod}" />
<property name="acquireRetryAttempts" value="${jdbc.acquireRetryAttempts}" />
</bean>
<!-- 配置iBatis的sqlMapClient,这里当然是交给了spring去处理,其中,将SqlMapConfig文件放到了WEB-INF的iBatis目录下,也是便于管理 -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>/WEB-INF/ibatis/SqlMapConfig.xml</value>
</property>
<!-- 这里使用的数据源就是上面配置的数据源 -->
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<!-- bean 注册 -->
<bean id="StudentService" class="com.xlb.test.service.StudentServiceImpl">
<property name="studentDAO">
<ref bean="StudentDAO" />
</property>
</bean>
<bean id="StudentDAO" class="com.xlb.test.dao.StudentDAOImpl">
<property name="sqlMapClient">
<ref bean="sqlMapClient" />
</property>
</bean>
<bean id="StudentAction" class="com.xlb.test.action.StudentAction">
<property name="studentService">
<ref bean="StudentService"/>
</property>
</bean>
</beans>
struct.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!-- 这是一个重要的地方,很多人在使用<s:include>子页面后,发现子页面乱码,怎么改都不行,原因就在次,struts2的默认编码为UTF-8,乱码的同志请看看你的jsp页面上的编码是不是和这个不一致呢。只要把这里和jsp编码改一致就行了 -->
<constant name="struts.i18n.encoding" value="UTF-8" />
<!-- 告诉struts2,我要用spring装配工厂,其实默认就是这个了-_-!!! -->
<constant name="struts.objectFactory" value="spring" />
<!-- struts2的扩展名,比如struts1的时候,用的.do,struts2默认为.action,可以改成其它的,比如.dxd -->
<constant name="struts.action.extension" value="action" />
<!-- 资源文件 -->
<constant name="struts.custom.i18n.resources"
value="messageResource">
</constant>
<package name="default" namespace="/" extends="struts-default,json-default">
<!-- 登录 -->
<action name="login" class="StudentAction" method="loginUser">
<result name="show">/jsp/list.jsp</result>
<result name="success" type="redirectAction">
<param name="actionName">login!show</param>
</result>
</action>
</package>
</struts>
jdbc.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/bysj
jdbc.user=root
jdbc.password=123456
jdbc.minPoolSize=5
jdbc.maxPoolSize=20
jdbc.maxIdleTime=1800
jdbc.acquireIncrement=5
jdbc.maxStatements=50
jdbc.initialPoolSize=10
jdbc.idleConnectionTestPeriod=1800
jdbc.acquireRetryAttempts=30
SqlMapConfig.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings cacheModelsEnabled="false" enhancementEnabled="true"
lazyLoadingEnabled="true" errorTracingEnabled="true" maxRequests="32"
maxSessions="10" maxTransactions="5" useStatementNamespaces="true" />
<!-- 用户信息表 -->
<!--<sqlMap resource="com/xlb/test/sqlmap/CtsSqlMap.xml"/><span style="font-family: Arial, Helvetica, sans-serif;">--></span>
</sqlMapConfig>
文档结构
基本上这样就配置完毕了 需要注意的是jar我是直接下载了一个ssi的集合 可以避免许多问题 然后如果编码问题问题 tomcat那也要设置一下 tomcat也要加入mysql的连接包
附上jar包链接
http://download.csdn.net/detail/u012357455/9261423
最后简单讲一下自己的心得,可能有理解错的地方 ,也希望大家能够指正,共同进步,首先当我们在浏览器输入url时 后台会首先加载web.xml 之后由于在web.xml中配置了struct和spring 所以会进行两者的加载 struct的配置相对简单,也不用主动注入spring和ibatis
<constant name="struts.objectFactory" value="spring" />
然后spring相对复杂一点 首先spring是已bean为基本单位,帮我们进行了许多的管理,spring中会注入ibatis 将相应的配置读取并连接数据库,之后可以进行sqlmapconfig的配置 将相应的地址给他,我的理解是sqlmap是数据库到entity的一个中间过程,算是一个映射吧,在sqlmap中会进行sql语句的配置(可以用工具生成),最后是最重要的部分,spring对实体对象进行全方位的注入管理 包括dao service 和action 其中action感觉不是必须的 不过注入的话可以在struct的直接使用bean的id而不是class的地址,感觉关系上更加清晰
这是我的简单理解啦 有不足的希望大家提出来
最后再感谢一下那些写了很多相应博客的人 给了我很多帮助 由于看得比较多杂 就不一一答谢了