SSI框架整合
开发工具:
MyEclipse8.5
框架版本:
struts2、spring2.5、ibatis
打开MyEclipse8.5新建一个web project如图:
填写完项目名称后选择Java EE 5.0,点finish
然后开始添加spring支持如图:
选择spring2.5,这里的包默认就可以,因为到最后这些包我都会将之删除,因为jar包冲突所以我将不会使用MyEclipse里自带的包,这里我只要后边这个文件 如图:点next
将applicationContext.xml放在项目WebRoot/WEB-INF文件夹下,点finish
然后我将把导入的spring包删除
删除后,导入自己的包
点next
选择你要导入的spring的jar包,放在test/WebRoot/WEB-INF/lib/spring2.5.5目录下,这样就很方便我们查看自己导入的包,点finish
接下来,就该导入struts了,同上
选择struts2.1 和 /*
点finish。然后删除导入的struts包,引入自己的
点finish。
发现项目仍然有错,缺少dbcp包,所以引入commons-dbcp.jar。具体方法(也是以后缺少包怎样引入的方法) 如图:
OK,没错了。
加入ibatis jar包;
配置applicationContext.xml文件添加以下代码
- <!-- 引入参数配置文件 -->
- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <list>
- <value>classpath:sqlMapping.properties</value>
- </list>
- </property>
- </bean>
- <!-- 数据源配制 -->
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName">
- <value>${jdbc.driverClassName}</value>
- </property>
- <property name="url">
- <value>${jdbc.url}</value>
- </property>
- <property name="username">
- <value>${jdbc.username}</value>
- </property>
- <property name="password">
- <value>${jdbc.password}</value>
- </property>
- </bean>
- <!-- 此处应注入ibatis配置文件,而非sqlMap文件,否则会出现“there is no statement.....异常” -->
- <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
- <property name="configLocation" >
- <value>classpath:SqlMapConfig.xml</value>
- </property>
- <property name="dataSource" ref="dataSource"/>
- </bean>
之后创建 sqlMapConfig.xml文件和sqlMapping.properties文件
sqlMapConfig.xml文件代码为:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE sqlMapConfig
- PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
- "http://www.ibatis.com/dtd/sql-map-config-2.dtd">
- <sqlMapConfig>
- <settings
- cacheModelsEnabled="true"
- enhancementEnabled="true"
- lazyLoadingEnabled="true"
- errorTracingEnabled="false"
- maxRequests="32"
- maxSessions="10"
- maxTransactions="5"
- useStatementNamespaces="false"
- />
- <sqlMap resource="com/naxl/oa/model/tb_User.xml" />
- <sqlMap resource="com/naxl/oa/model/tb_Department.xml" />
- </sqlMapConfig>
sqlMapping.properties文件代码为:
- jdbc.driverClassName=com.mysql.jdbc.Driver
- jdbc.url=jdbc:mysql://192.168.1.104:3306/naxloa
- jdbc.username=root
- jdbc.password=admin
接下来就要配置web.xml了,在web.xml添加以下代码
- <!-- 配置Spring -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/applicationContext.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- 如果你的web.xml文件中没有配置struts,则需添加如下代码
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>
- org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
- </filter-class>
- <init-param>
- <param-name>config</param-name>
- <param-value>struts-default.xml,struts-plugin.xml,struts.xml</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
Struts.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>
- <package name="default" extends="json-default">
- <action name="" class="" method="">
- <result name=""> </result>
- <result name=""> </result>
- </action>
- </package>
- </struts>
运行Tomcat检查配置的项目有没有错,如果有缺失jar包请自己添加;
SSI搭建完成接下来就要写代码程序;