您的位置:首页 > 编程语言 > Java开发

Spring +Struts2+ibatis 整合细节

2014-11-06 10:06 197 查看
本人也是JavaWeb初学者,代码还存在的不足之处还请明确指出

那好 废话不多说 开始正题

这次实例运用Spring +Struts2+ibatis 基于B/S结构的《用户管理系统》

1.首先导入JAR 开发包

这个不用一一列举了 直接跳过

2.配置WebContent/WEB-INF目录下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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SpringAndStruts2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
   <!-- spring listener -->
    <listener>
        <!-- 这个就是今后用到的WebApplicationUtilContent -->
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<span style="color:#FF0000;">  <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/spring-*.xml</param-value>
</context-param></span>
 <!-- 这个就是struts2的核心过滤器 -->
 <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>/*</url-pattern>
</filter-mapping>
</web-app>
从<param-value>classpath*:spring/spring-a*.xml</param-value>

可以看出 和官方的不同 其实我没有按照官方的方法存放文件,只是按照个人的习惯配置

若有不适请自行修改

<span style="color:#CC0000;">   <context-param>
<param-name>contextConfigLocation</param-name>
<!-- 把spring的配置文件放到了/WEB-INF/下的springframework包里,命名规则是以applicationContent-开头的xml文件,初始化时会自动搜索所有符合规则的配置文件 -->
<param-value>
/WEB-INF/springframework/applicationContext-*.xml
</param-value>
</context-param></span>
有同学可能要问 什么要这样配置

呵呵 不多吐槽

3.整合Spring +Struts2

首先是struts2的配置

目录结构图

struts2的配置 分为来公布

1.在web.xml配置struts2的核心类库

前面已经给出文档 自行查看

2.在src根目录创建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>

<constant name="struts.objectFactory" value="spring" />

<package name="user" extends="struts-default" namespace="/user">
<action name="user" class="userAction">
<result name="success">/userManager/userList.jsp</result>
<result name="error">/index.jsp</result>
<result name="save">/userManager/addUser.jsp</result>
<result name="update">/userManager/updateUser.jsp</result>
</action>
</package>

</struts>
熟悉struts2的同学可能明白

<action ……/>里面缺少 method=""
没错<action method=""/> 的配置  分为 三种<span style="color:#FF0000;"> 静态  动态  通配符</span>

那好  先讲讲 静态调用

静态调用  和struts1 区别不是很大 在struts.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.objectFactory" value="spring" />

    <package name="login" extends="struts-default" namespace="/user" >
        <action name="user" class="com.zy.action.UerAction" method="login">
            <result name="success">/userManager/userList.jsp</result>
            <result name="error">/index.jsp</result>
        </action>
        <action name="save" class="com.zy.action.UerAction" method="save">
            <result name="success">/userManager/userList.jsp</result>
            <result name="error">/index.jsp</result>
            <result name="save">/userManager/addUser.jsp</result>
        </action>
……………
    </package>

</struts>

然后是讲讲通配符的使用
先看代码
ml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<pre name="code" class="html">    <constant name="struts.objectFactory" value="spring" />
<package name="user" extends="struts-default" namespace="/user">

<action name="*Action" class="com.zy.action.UserAction" method="{1}">

<result name="success">/userManager/userList.jsp</result>

<result name="error">/error.jsp</result>

<result name="save">/userMannager/addUser.jsp</result>

<result name="update">/userManager/updateUser.jsp</result>

</action>

</package>

</struts>

可以看出红字的代码和上面有很大不同之处

没错用了*通配符

那好然后看下

login.jap文件

<body>

<h2 align="center">Java我來了</h2>

<s:form action="loginAction" method="post">

<s:textfield label="userName" name="userName" />

<s:password label="userPass" name="userPass" />

<s:submit label="登录"></s:submit>

</s:form>

可以很清楚的看出

<span style="color:#FF0000;">name="*Action"   与</span><span style="color:#FF0000;"> action="loginAction"</span><span style="color:#FF0000;">
的联系
没错在这次请求中 * 号代表</span><span style="color:#FF0000;"> action="loginAction"</span><span style="color:#FF0000;"> 中的login
而在struts.xml文件中的</span><span style="color:#FF0000;"> method="{1}"</span><span style="color:#FF0000;">就代表   第一个通配符也就是login
 </span><span style="color:#FF0000;"></span><pre name="code" class="html"><pre name="code" class="html"><span style="color:#FF0000;"></span>



这样就解决了 重复写多个action的麻烦 实例
<pre name="code" class="html"><span style="color:#FF0000;"><form action="updateAction"</span><span style="color:#FF0000;"> >   </span><span style="color:#FF0000;">struts.xml文件中的</span><span style="color:#FF0000;"> method="{1}"</span><span style="color:#FF0000;"> = </span><span style="color:#FF0000;">method="lupdate"</span><span style="color:#FF0000;"> </span><span style="color:#FF0000;">
相应的  就调用 </span>UerAction<span style="color:#FF0000;">.java类中的 update()方法

</span><pre name="code" class="html"><pre name="code" class="html">但是不推荐一开始就是用   因为如果不小心些许哦了一个字母   就很难找到错误的 因为都是隐藏 的




最后是动态调用方法看上面的代码会发现调用的类总是同一个 action 并且还是会有很多代码冗余不废话 先看代码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>
<constant name="struts.objectFactory" value="spring" /> <package name="user" extends="struts-default" namespace="/user"zz> <action name="user" class="userAction"> <result name="success">/userManager/userList.jsp</result> <result
name="error">/index.jsp</result> <result name="save">/userManager/addUser.jsp</result> <result name="update">/userManager/updateUser.jsp</result> </action> </package></struts>
<pre name="code" class="html"><span style="color:#CC0000;"><action name="user" class="userAction"></span>


细心地同学可能会发现 class="userAction" 没有了绝对路径的类指向 发而变成 了一个字符串并且action 只有一个没错 这就是Spring与struts2整合的结果那好说说Spring的配置问题Spring的配置 需要在web.xml注册监听器 以及 Spring-context.xml 的配置我直接配置在了src根目录的Spring 之下


首先看看源码

Spring-action.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.5.xsd "
    default-autowire="byName"
    >
<span style="color:#CC0000;">    <!-- SpringAction配置,scope设置Action作用域为单例模式还是多态模式 ,默认为单例模式-->
    <bean id="userAction" class="com.zy.action.UserAction"  scope="prototype"></bean></span>

</beans>
没错 看了代码 之前的疑问 迎刃而解

在struts.xml配置文件里 class 的指向 就是<bean id="userAction"> 这样通过Spring提供的映射原理 自动的加载
<pre name="code" class="html"><span style="color:#CC0000;">class="com.zy.action.UserAction" 类  并赋给</span><span style="color:#CC0000;">userAction</span><span style="color:#CC0000;">  userAction就代表 UserAction这个类

</span>


我们再看 请求页面index.jsp<s:form action="user/user!login" method="post"><s:textfield name="user.userName" label="用户名"/><s:password name="user.userPass" label="密码"/><s:submit value="登录"></s:submit></s:form>红字标示的部分有所不同,我来解释下user/是命名空间
usr 是action 的名字 !login 是调用 UserAction类中 的login() 方法简单明了
注意 在html 标签中要带后缀user/user!login.axtion4.Spring与ibatis的整合与上面一样 先看代码Spring-Config.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"> <!-- 引用JDBC的配置文件-->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>jdbc.properties</value> </property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
> <property name="driverClassName"> <value>${driver}</value> </property> <property name="url"> <value>${url}</value> </property> <property name="username"> <value>${username}</value>
</property> <property name="password"> <value>${userpass}</value> </property> </bean> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <!-- 此处应注入ibatis配置文件,而非sqlMap文件,否则会出现“there
is no statement.....异常” --> <property name="configLocation" value="ibatis/SqlMapConfig.xml"></property> <property name="dataSource" ref="dataSource" /> </bean> <bean id="UserDaoImpl" class="com.zy.daoimpl.UserDaoImpl"> <property
name="dataSource"> <ref bean="dataSource" /> </property> <property name="sqlMapClient"> <ref bean="sqlMapClient" /> </property> </bean></beans>从上面代码可以开出 ibatis与数据库的链接功能 被Spring所取代 具体的配置看上面代码ibatis 配置文件<?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> <sqlMap resource="ibatisextconfig/UserSqlMap.xml" /></sqlMapConfig>既然数据库的链接功能在整合后被Spring所取代
那么ibatis的配置也显得 没用了那么 ibatis 到底在干嘛呢可以看出 ibatis配置文件有一个链接
<sqlMap resource="ibatisextconfig/UserSqlMap.xml" />
找到链接的资源

看下面代码

UserSqlMap.xml


<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"

"http://ibatis.apache.org/dtd/sql-map-2.dtd" >

<sqlMap>

<typeAlias alias="user" type="com.zy.entity.User" />

<select id="findUser" resultClass="user">

select * from user

where 1=1

<isNotEmpty prepend="and" property="userName">

userName like

'%$userName$%'

</isNotEmpty>

<isNotEmpty prepend="and" property="userID">

userID=#userID#

</isNotEmpty>

</select>

<select id="login" resultClass="user">

select * from user

where userName=#userName# and userPass=#userPass#

</select>

<update id="update">

update user set

userName=#userName#,

userPass=#userPass#

where userID=#userID#

</update>

<insert id="save">

insert into

user(userID,userName,userPass)values(#userID#,#userName#,#userPass#);

</insert>

<delete id="delete">

delete from user where userID=#userID#

</delete>

</sqlMap>

了解ibatis的同学可能就会明白了

ibatis 只做SQL 查询数据的SQL语句工作

其他不多说 看代码自己理解是关键

实例源代码


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: