这几天一直想着搭建一个简单的SSI框架,下面就把步骤一步一步写下来,相互学习交流!
1.新建Web Project工程
2.增加struts2的jar包
3.然后配置web.xml中的struts过滤器,启动应该是不报错误。
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<!-- 让struts定义的核心filter拦截所有请求 -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
4.增加Spring的jar包
5.然后配置webl.xml中的Spring配置,然后在src目录下建一个applicationContext.xml,启动应该不会报错误
<!-- 用于配置读取applicationContext.xml -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
6.基本的配置完毕后,下面我们就要做数据源饿配置,我用的是oracle数据库,c3p0连接池
(1).在src目录下建立一个c3p0连接池文件:c3p0.properties
datasource.type=oracle
datasource.driverClassName=oracle.jdbc.driver.OracleDriver
datasource.url=jdbc:oracle:thin:@localhost:1521:orcl
datasource.username=test
datasource.password=test
c3p0.acquireIncrement=3
c3p0.initialPoolSize=3
c3p0.idleConnectionTestPeriod=900
c3p0.minPoolSize=2
c3p0.maxPoolSize=50
c3p0.maxStatements=100
c3p0.numHelperThreads=10
c3p0.maxIdleTime=600
c3p0.testConnectionOnCheckout=true
c3p0.preferredTestQuery=select sysdate from dual
c3p0.dialect=org.hibernate.dialect.Oracle9Dialect
(2).在applicationContext.xml文件中配置数据源
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:c3p0.properties</value>
</list>
</property>
</bean>
<!--定义数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close" dependency-check="none">
<property name="driverClass">
<value>${datasource.driverClassName}</value>
</property>
<property name="jdbcUrl">
<value>${datasource.url}</value>
</property>
<property name="user">
<value>${datasource.username}</value>
</property>
<property name="password">
<value>${datasource.password}</value>
</property>
<property name="acquireIncrement">
<value>${c3p0.acquireIncrement}</value>
</property>
<property name="initialPoolSize">
<value>${c3p0.initialPoolSize}</value>
</property>
<property name="minPoolSize">
<value>${c3p0.minPoolSize}</value>
</property>
<property name="maxPoolSize">
<value>${c3p0.maxPoolSize}</value>
</property>
<property name="maxIdleTime">
<value>${c3p0.maxIdleTime}</value>
</property>
<property name="idleConnectionTestPeriod">
<value>${c3p0.idleConnectionTestPeriod}</value>
</property>
<property name="maxStatements">
<value>${c3p0.maxStatements}</value>
</property>
<property name="numHelperThreads">
<value>${c3p0.numHelperThreads}</value>
</property>
<property name="testConnectionOnCheckout">
<value>${c3p0.testConnectionOnCheckout}</value>
</property>
<property name="preferredTestQuery">
<value>${c3p0.preferredTestQuery}</value>
</property>
</bean>
(3).此时启动可能要报错,现在需要引入两个jar包,然后再次启动则没有错误
7.下面配置struts文件
<constant name="struts.objectFactory" value="spring"></constant>
<!-- 所有的action都应该放在对应的package下 -->
<package name="riyun" extends="struts-default">
<action name="login" class="loginAction">
<!-- 定义逻辑视图和物理资源之间的映射 -->
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
8.然后建一个实体类和登录action
9.下面就开始加入mybatis包用来持久化类了
(1).