标签:flex struts+spring+ibatis 整合实例
原创整理不易,转载请注明出处:flex3+struts 1.3+spring+ibatis 2.x整合代码实例
代码下载地址:http://www.zuidaima.com/share/1778657261997056.htm
经过两天工作闲余时间的奋战,终于flex3+struts 1.3+spring +ibatis 2.x 整合成功,下面介绍下详细的步骤和核心代码:
IDE:myeclipse
(当然:前提是FLEX+java整合成功的情况下,关于flex+java整合的文章就比较多,google下就OK):
1.成功整合FLEX+JAVA后,右键单击项目选择 MyEclipse---》add struts(添加STRUTS比较简单这里就不多说了),下面贴代码:
(1)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans /> <global-exceptions /> <action-mappings > <action path="/user" type="com.yinhai.struts.action.UserAction" scope="request" > <forward name="sucess" path="/index.jsp" /> </action> </action-mappings> </struts-config>
在web-info下的struts-config.xml中添加如上代码
(2)UserAction
package com.zuidaima.struts.action; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import model.User; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import dao.ContextHelper; import dao.UserDaoImpl; /** * MyEclipse Struts * Creation date: 08-05-2009 * * XDoclet definition: * @struts.action parameter="method" validate="true" * @struts.action-forward name="fail" path="/fail.jsp" * @struts.action-forward name="sucess" path="/sucess.jsp" */ public class UserAction extends Action { /* * Generated Methods */ /** * Method execute * @param mapping * @param form * @param request * @param response * @return ActionForward * @throws IOException */ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { // ClassPathXmlApplicationContext ctxss = new ClassPathXmlApplicationContext("applicationContext.xml"); // BeanFactory ctx=(BeanFactory)ctxss; // UserDaoImpl um = (UserDaoImpl) ctx.getBean("userDao"); UserDaoImpl um =(UserDaoImpl)ContextHelper.getContext().getBean("userDao"); List users = um.getUsers(); User user=null; String xml="<?xml version=\"1.0\" encoding=\"utf-8\"?><users>"; for(int i=0;i<users.size();i++){ user=(User)users.get(i); xml=xml+"<user>"+ "<usernumber>"+user.getUsernumber()+"</usernumber>" + "<name>"+user.getName()+"</name>"+ "<password>"+user.getPassword()+"</password>"+ "</user>"; } xml=xml+"</users>"; System.out.println(xml); PrintWriter p = null; try { // p=response.getWriter(); response.getWriter().write(xml); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } //p.write(xml); // p.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><users><user><usernumber>1</usernumber><name>caicai</name><password>dddddd</password></user><user><usernumber>2</usernumber><name>yingying</name><password>dddddd</password></user><user><usernumber>3</usernumber><name>ceshi</name><password>dddddd</password></user><user><usernumber>4</usernumber><name>ceshi</name><password>dddddd</password></user>"); // p.close(); return null; }
(3)获取bean的工具类
package dao; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Date: 2008-3-20 Time: 17:28:48 To change this template use File | Settings | * File Templates. */ public class ContextHelper { private static ClassPathXmlApplicationContext _ctx; static { _ctx = new ClassPathXmlApplicationContext("/applicationContext.xml"); } public static ClassPathXmlApplicationContext getContext() { return _ctx; } }
2.添加SRPING
右键单击项目选择 MyEclipse---》add spring
spring的配置文件
<?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=" dataSource " class=" org.apache.commons.dbcp.BasicDataSource " destroy - method=" close "> <property name=" driverClassName " value=" com.mysql.jdbc.Driver " /> <property name=" url " value=" jdbc:mysql://localhost:3306/test " /> <property name=" username " value=" root " /> <property name=" password " value=" root " /> </bean> <bean id=" sqlMapClient " class=" org.springframework.orm.ibatis.SqlMapClientFactoryBean "> <property name=" configLocation " value=" sqlmap-config.xml " /> <property name=" dataSource " ref=" dataSource " /> </bean> <bean id=" userDao " class=" dao.UserDaoImpl "> <property name=" sqlMapClient " ref= " sqlMapClient " /> </bean> </beans>
(3)添加IBATIS
首先将ibatis的JAR包导入,放在LIB下即可
IBTIS配置文件
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> <!-- Configure a built - in transaction manager. If you ‘ re using an app server, you probably want to use its transaction manager and a managed datasource --> <!-- List the SQL Map XML files. They can be loaded from the classpath, as they are here (com.domain.data) --> <sqlMap resource=" dao/User.xml " /> <!-- List more here <sqlMap resource=" com/mydomain/data/Order.xml " /> <sqlMap resource= " com/mydomain/data/Documents.xml " /> --> </sqlMapConfig>
User.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=" User "> <!-- Use type aliases to avoid typing the full classname every time. --> <typeAlias alias=" user " type=" model.User " /> <!-- Select with no parameters using the result map for Account class . --> <select id=" getUsers " resultClass= " user "> select * from user; </select> </sqlMap>
注意:User.xml一定要和相应的DAO的实现类放一个包下
4.FLEX文件
<?xml version=" 1.0 " encoding=" utf-8 " ?> <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml " layout=" absolute "> <mx:Script> <! [CDATA[ import mx.rpc.events.FaultEvent; import mx.controls.Alert; import mx.rpc.events.ResultEvent; import mx.controls.DataGrid; [Bindable] public var gridXml:XML; public function httpHandler(event:ResultEvent): void { Alert.show("hello2"); gridXml=XML(event.result); griddata.dataProvider=gridXml.children();//XMLList } public function initHandler(): void { testHttp.url="http://localhost:8080/LCSYS/user.do"; Alert.show("hello1"); testHttp.send(); } public function httpFail(event:FaultEvent): void { Alert.show("调用失败"); Alert.show(event.message.toString()); } ]]> </mx:Script> <mx:HTTPService id=" testHttp " result=" httpHandler(event) " fault=" httpFail(event) " resultFormat=" e4x " /> <mx:Panel x=" " y=" " width=" " height=" " layout=" absolute " id=" userPannel " title=" userPannel1 "> <mx:DataGrid x=" " y=" " width=" 100% " height=" 100% " id=" griddata "> <mx:columns> <mx:DataGridColumn dataField=" usernumber " headerText=" usernumber " /> <mx:DataGridColumn dataField=" name " headerText=" name " /> <mx:DataGridColumn dataField=" password " headerText=" password " /> </mx:columns> </mx:DataGrid> <mx:ControlBar height=" " y=" "> <mx:Button label=" 查询人员列表 " click= " initHandler() " /> </mx:ControlBar> </mx:Panel> </mx:Application>
5.其他文件:
工具类,解决乱码
package dao; import java.io.UnsupportedEncodingException; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Tool { public static String change(String str){ String str2; try { str2=new String(str.getBytes("iso-8859-1"),"UTF-8"); return str2; } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } }
userDaoImpl
package dao; import java.util.List; import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport; public class UserDaoImpl extends SqlMapClientDaoSupport implements UserDao { public List getUsers() { // TODO Auto-generated method stub return this.getSqlMapClientTemplate().queryForList("getUsers"); } }
时间关系,介绍得比较粗糙也没撒说明,不过只要对框架较熟悉的应该很快能搭建起环境
本文章主要是集成框架介绍
注意:需要IBATIS的JAR吧,dbcp连接池的包,还有相应的驱动包
flex3+struts 1.3+spring+ibatis 2.x整合代码实例,布布扣,bubuko.com
flex3+struts 1.3+spring+ibatis 2.x整合代码实例
标签:flex struts+spring+ibatis 整合实例
原文地址:http://blog.csdn.net/yaerfeng/article/details/25592761