首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络基础 >

Struts2.1.6 Spring2.5.6 Ibaits2.3.4 自个儿搭一个框架吧

2012-10-26 
Struts2.1.6 Spring2.5.6 Ibaits2.3.4 自己搭一个框架吧突然想试试不同版本下搭的框架有什么不同,于是在网

Struts2.1.6 Spring2.5.6 Ibaits2.3.4 自己搭一个框架吧

突然想试试不同版本下搭的框架有什么不同,于是在网上找了些示例,自己也搭一个。
当然这个框架只是我个人练习使用的,没有用到实际开发中,里面是否含有什么致命的问题,或者欠缺什么考虑,我却是不知道了。如果日后有发现,我会在后面注释的。
本来struts2想要用2.1.6,不过MyEclipse8.0里面自带的是2.1.8的jar包。

首先列出来使用到的jar包,拷贝到WEB-INF/lib/目录下
commons-dbcp.jar
commons-fileupload.jar
commons-loggin.jar
commons-pool.jar
log4j.jar

freemarker-2.3.15.jar
ognl-2.7.3.jar
struts2-core-2.1.8.jar
struts2-spring-plugin-2.1.8.jar
xwork-core-2.1.6.jar

ibatis-2.3.4.726.jar

mysql-connector-java-3.1.12-bin.jar

spring-beans.jar
spring-context.jar
spring-core.jar
spring-jdbc.jar
spring-orm.jar
spring-tx.jar
spring-web.jar
当然spring的jar包也可以使用spring.jar这种集合版


下面简要的贴一下主要配置文件的配置参数,后面把我搭的示例也一并贴上来


第一步,修改web.xml文件,添加MVC框架支持
想要在web项目中使用MVC框架,首先肯定要在web.xml文件中声明过滤器和监听程序
虽然struts2在MyEclipse中似乎可以自定义后缀,比如*.do,其实不然,struts2只支持默认的*.action,工具似乎还是不太保险。为了保险,最好不要用MyEclipse自带的添加框架支持的功能,还是手动把jar包拷贝到lib目录下,然后手动建立各种配置文件。

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5"     xmlns="http://java.sun.com/xml/ns/javaee"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">    <welcome-file-list>        <welcome-file>index.jsp</welcome-file>    </welcome-file-list>    <!-- 配置Struts2 -->    <filter>        <filter-name>struts2</filter-name>        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>    </filter>    <filter-mapping>        <filter-name>struts2</filter-name>    <url-pattern>/*</url-pattern>    </filter-mapping>    <!-- 配置Spring -->    <context-param>       <param-name>contextConfigLocation</param-name>       <param-value>           WEB-INF/classes/applicationContext*.xml    </param-value>       </context-param>       <listener>           <listener-class>               org.springframework.web.context.ContextLoaderListener           </listener-class>       </listener></web-app>




第二步,建立struts2的映射配置文件
在src的根目录下面建立一个struts.xml文件,功能相当于struts1中的config文件。
用户提交请求以后,FilterDispatcher会判断用户请求是不是action请求,如果是,就到WEB-INF/classes/目录下查找名为strtus.xml文件,解析后调用相应的action逻辑控制器。

<?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>    <constant name="struts.devMode" value="true" />    <constant name="struts.enable.DynamicMethodInvocation" value="false" />    <constant name="struts.custom.i18n.resources" value="messageResource" />    <package name="default" extends="struts-default">        <action name="login" method="viewBook">            <result name="success">/book/viewBook.jsp</result>        </action>        <action name="addBook" method="addBook">            <result name="success" type="redirectAction">viewBook</result>        </action>        <action name="updateBook" method="updateBook">            <result name="success">/book/updateBook.jsp</result>        </action>        <action name="saveBook" method="saveBook">            <result name="success" type="redirectAction">viewBook</result>        </action>        <action name="deleteBook" method="deleteBook">            <result name="success" type="redirectAction">viewBook</result>        </action>    </package></struts>




说明一:
文件中有<constant />的参数定义部分,这部分也可以使用标准properties文件来定义
同样在src目录下建立一个struts.properties文件,里面使用X=Y的标准键值方式来定义

struts.devMode=truestruts.enable.DynamicMethodInvocation=falsestruts.custom.i18n.resources=messageResource




说明二:
可以将所有的action的映射写到这一个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>    <include file="action/sbook.xml"></include>    <include file="action/test.xml"></include></struts>



?
第三步,建立spring的上下文配置文件
这里要建立的xml文件目录在web.xml文件中已经声明过了,可以修改,不像struts.xml是固定的src目录。不过我的个人习惯,还有MyEclipse里面也推荐src目录。
建立applicationContext.xml文件,在里面定义数据源、iBATIS文件声明、service和Dao层的bean定义。
为了结构清晰和使用方便,我把service和dao层的bean定义部分分离出来,形成两个新的文件,文件名以applicationContext开头,这样在web.xml文件中声明的时候,只要使用applicationContext*就可以了。

applicationContext.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" xmlns:p="http://www.springframework.org/schema/p"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">    <!-- JDBC DataSource -->    <bean id="dataSource" destroy-method="close">        <property name="driverClassName" value="com.mysql.jdbc.Driver" />        <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8" />        <property name="username" value="root" />        <property name="password" value="admin" />    </bean>    <!-- Transaction manager for a single JDBC DataSource -->    <bean id="transactionManager" ref="dataSource" />    </bean>    <!-- SqlMap setup for iBATIS Database Layer -->    <bean id="sqlMapClient" value="WEB-INF/classes/sqlMapConfig.xml" />        <property name="dataSource" ref="dataSource" />    </bean></beans>


说明:上面文件中的configLocation一项中,iBATIS配置文件的位置必须使用WEB-INF/classes/的方式来声明,否则会提示找不到文件


applicationContext_service.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" xmlns:p="http://www.springframework.org/schema/p"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">    <bean id="bookService" ref="bookDao" />    </bean>    <bean id="userService" ref="userDao" />    </bean></beans>



applicationContext_dao.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" xmlns:p="http://www.springframework.org/schema/p"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">    <bean id="bookDao" ref="sqlMapClient" />    </bean>    <bean id="userDao" ref="sqlMapClient" />    </bean></beans>


注意:在dao层的bean定义中,一定要把sqlMapClient注入进去。同时dao层的实现类必须继承SqlMapClientDaoSupport这个父类,其中有setSqlMapClient()方法来实现依赖注入。


第四步,建立iBATIS的配置文件和sql映射文件
配置文件的位置,定义在applicationContext.xml文件中。
iBATIS的属性配置文件与struts和spring的配置文件存放在同一个目录src下,sql映射文件存放在具体的实体Bean的目录下,方便管理。
sqlMapConfig.xml定义iBATIS的各种属性,并且统一调度实体Bean的映射文件。
实体Bean的映射文件具体内容的格式说明,这里就不说了。


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="true"        enhancementEnabled="true"        lazyLoadingEnabled="true"        errorTracingEnabled="true"        maxRequests="32"        maxSessions="10"        maxTransactions="5"        useStatementNamespaces="false"    />    <sqlMap resource="vo/book.xml" />    <sqlMap resource="vo/user.xml" /></sqlMapConfig>


注意:在DOCTYPE类型的定义中,关键字必须是sqlMapConfig,这个是由sql-map-config-2.dtd文件规定的。
????? 另外,sqlMap的文件引用资源路径也需要注意,不能使用WEB-INF的方式引用。


以上四个步骤,就是建立一个Struts+Spring+Ibaits的项目结构的简单说明。
下面的附件是我按照这个方式搭建的一个简单的事例,结构绝对谈不上严谨,供参考而已。
里面含有两个小功能。
一个是用户登录验证的部分,使用了xwork的action-validation验证方式。同时代码里也注释了一段在action中验证的方式。
一个是简单的增删改查功能,一个大的项目就是由这样的小功能组合起来的。

?

1 楼 zhao-xn 2011-12-27   学习一下。

热点排行