struts2+spring+ibatis框架搭建心得

  我们知道struts1与spring整合是靠org.springframework.web.struts.DelegatingActionProxy来实现的,以下通过具体一个用户登录实现来说明struts2整合spring的相关内容.

    一、准备工作

1.配置开发环境: MyEclipse6.0+Eclipse3.3+JDK6.0+Tomcat6.0+Struts 2.0+spring2.5.6+ibatis2.3.4。

2.新建web项目,导入相应的jar包,如以下所示:
     a.由于现在IDE开发工具还没有对struts2.0有很好的支持,所有我们需要手功配置,首先将我们刚下下来的struts2.0的lib里面的asm-3.1.jar、asm-commons-3.1.jar、asm-tree-3.1.jar、commons-fileupload-1.2.2.jar、commons-io-2.0.1.jar、commons-lang-2.5.jar、freemarker-2.3.16.jar、javassist-3.11.0.GA.jar、ognl-3.0.1.jar、struts2-core-2.2.3.jar、xwork-core-2.2.3.jar、commons-logging-1.0.4.jar、、commons-pool-1.4.jar、log4j-1.2.15.jar、spring-2.5.6.jar、spring-webmvc-struts-2.5.6.jar、struts2-spring-plugin-2.0.9.jar、xercesImpl-2.8.1.jar、xml-apis-1.3.03.jar、commons-dbcp-1.2.2.jar、ibatis-2.3.4.726.jar、ojdbc6.jar(很多jar其实没必要,有待深究)   添加的WEB-INF/lib下面

3.配置(WEB-INF\)web.xml,如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Struts Blank</display-name>
<!-- struts -->
    <filter>
        <filter-name>struts_demo001</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts_demo001</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
<!-- spring -->
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>   
<!-- spring配置文件 -->
    <context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value> /WEB-INF/test/conf/test.applicationContext.xml
		</param-value>
	</context-param>

</web-app>

 4.struts文件(src\)配置

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
	<constant name="struts.devMode" value="true"></constant>
	<constant name="struts.custom.i18n.resources" value="test"></constant>
    <include file="struts-default.xml"></include>

    <package name="test" extends="struts-default" namespace="/" >

       <action name="loginAction" class="test.action.LoginAction" >
           <result name="init">/WEB-INF/test/jsp/login.jsp</result>
       </action>
    </package>
    
</struts>

4.test.applicationContext.xml(/WEB-INF/test/conf/)

<?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">
    	
    	
    <bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
			<!-- 定义一个PropertyPlaceholderConfigurer的实例,并将其位置属性设置为我们的属性文件,来代替占位符${xxx} -->
				<value>WEB-INF/database.properties</value>
			</list>
		</property>
	</bean>
	
		<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="${test.driverClassName}" />
		<property name="url" value="${test.url}" />
		<property name="username" value="${test.username}" />
		<property name="password" value="${test.password}" />
	</bean>
	
	<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="configLocation" value="/WEB-INF/SqlMapConfig.xml" />
		<property name="dataSource" ref="dataSource" />
	</bean>
    <bean id="loginDao" class="test.daoImpl.LoginDaoImpl">
		<property name="sqlMap" ref="sqlMapClient"></property>
	</bean>
    
    <bean name="loginService" class="test.serviceImpl.LoginServiceImpl" >
    <property name="loginDao" ref="loginDao"></property>
    </bean>
    
    <bean name="resultAction" class="test.action.ResultAction">
        <property name="loginService">
            <ref bean="loginService"/>
        </property>
    </bean>        

</beans>

 5.database.properties(存放访问数据库的驱动等信息)

test.driverClassName=oracle.jdbc.driver.OracleDriver
test.url=jdbc:oracle:thin:@192.168.1.12:1521:test
test.username=name
test.password=pwd
 

 

6.SqlMapConfig.xml(WEB-INF/)

<?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 useStatementNamespaces="true" />

       <sqlMap resource="test/daoImpl/test.xml" />

</sqlMapConfig>

 7.test.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 namespace="test">

       <typeAlias alias="userInfoVo" type="test.vo.UserInfoVo" />

       <select id="queryByLoginName" resultClass="java.lang.String"

              parameterClass="string">

             select servnumber from IB_BANK_HIS where taskid = #value#

       </select>

</sqlMap>
 

8.建立action文件

package test.action;
import java.sql.SQLException;
import java.util.Map;

import javax.management.loading.PrivateClassLoader;

import org.apache.struts2.util.IteratorGenerator.Converter;
import org.omg.PortableInterceptor.SUCCESSFUL;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

import test.service.LoginService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.net.httpserver.Authenticator.Success;

public class LoginAction extends ActionSupport {

   private LoginService loginService;
   
    public String query() throws Exception 
    {

        String pwd = loginService.querybyLoginName("name");
        System.out.print(pwd);
       
       return "init";

    }
    
    public String init()
    {
        return "init";
    }
   /**
    * @return 返回 loginService
    */
   public LoginService getLoginService()
   {
       return loginService;
   }

   /**
    * @param 对loginService进行赋值
    */
   public void setLoginService(LoginService loginService)
   {
       this.loginService = loginService;
   }

}

 9.LoginServiceImpl文件

 

package test.serviceImpl;
import java.sql.SQLException;
import test.dao.LoginDao;
import test.service.LoginService;

public class LoginServiceImpl implements LoginService
{
    private LoginDao loginDao; 
  
    
   
    public String querybyLoginName(String name) throws SQLException
    {
       return loginDao.querybyLoginName(name);
    }
    public String  queryForResult()
    {
        
       return "service return:loveYY";   
    }
    
    /**
     * @return 返回 loginDao
     */
    public LoginDao getLoginDao()
    {
        return loginDao;
    }
    /**
     * @param 对loginDao进行赋值
     */
    public void setLoginDao(LoginDao loginDao)
    {
        this.loginDao = loginDao;
    }
}

 10.LoginDaoImpl文件

package test.daoImpl;

import java.sql.SQLException;


import com.ibatis.sqlmap.client.SqlMapClient;

import test.dao.LoginDao;

public class LoginDaoImpl  implements LoginDao
{
    SqlMapClient sqlMap ;
    
    public String querybyLoginName(String name) throws SQLException
    {
        String pwd=null;
        try{
             pwd = (String)sqlMap.queryForObject("test.queryByLoginName",(String)"1");
        }
        catch (Exception  e){
            e.printStackTrace();
        }
        return pwd;
    }
    /**
     * @return 返回 sqlMap
     */
    public SqlMapClient getSqlMap()
    {
        return sqlMap;
    }
    /**
     * @param 对sqlMap进行赋值
     */
    public void setSqlMap(SqlMapClient sqlMap)
    {
        this.sqlMap = sqlMap;
    }
   
}
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值