struts2+spring+hibernate整合
来源:互联网 发布:淘宝pc端宝贝详情尺寸 编辑:程序博客网 时间:2024/05/02 04:54
1.在web.xml文件当中配置以监听方式加载Spring配置文件
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param>
2.在struts.xml文件当中配置通过Spring管理Struts的创建
<struts> <!-- 创建struts对象的过程交由Spring来完成 --> <constant name="struts.objectFactory" value="spring"></constant> <package name="student" namespace="/" extends="struts-default"> <!-- class的值为Spring配置文件当中bean的name属性值 --> <action name="add" method="add"> <result>/index.jsp</result> </action> </package> <!-- json --> <package name="json" namespace="/" extends="json-default"> <action name="json"> <result type="json" name="success"></result> </action> </package></struts>
3.在Spring配置文件当中依次配置dao、service以及Action对象的bean
<bean id="studao" class="com.s2sh.dao.StudentDao"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="service" class="com.s2sh.service.Service"> <property name="studao" ref="studao"></property> </bean> <bean name="addAction" class="com.s2sh.control.StudentAction"> <property name="serivce" ref="service"></property> </bean> <bean name="jsonTest" class="com.s2sh.control.JsonAction"></bean>
4.以下为代码编写测试
数据bean对象省略get、set以及构造方法
public class Student { private int sid; private String sname; private String description; }
<!-- 将配置文件当中的configuration全部改成mapping --><hibernate-mapping> <!-- class配置当前类的映射 --> <!-- name:对应当前类包名以及类型 table:当前类在数据库当中的映射 table可以不写,默认与类同名 --> <class name="com.s2sh.bean.Student" table="student"> <!-- id标签配置当前表的主键 name:属性名称 column:对应的列明 <column name=""></column> type:对应的类型 --> <id name="sid" column="sid" type="java.lang.Integer"> <!-- 主键生成机制/策略 --> <generator></generator> </id> <!-- type:可以不写,写Java类型,写hibernate类型 --> <property name="sname" column="sname" type="java.lang.String"></property> <property name="description" column="description" type="java.lang.String"></property> </class></hibernate-mapping>
5.下面为dao层代码编写
public class StudentDao extends HibernateDaoSupport implements IStudentDao{ public void addStudent(Student stu) { // TODO Auto-generated method stub this.getHibernateTemplate().save(stu); }}
6.下面为service层代码编写
public class Service { private IStudentDao studao; public IStudentDao getStudao() { return studao; } public void setStudao(IStudentDao studao) { this.studao = studao; } public void saveStudent(Student stu){ studao.addStudent(stu); }}
7.下面为Action代码编写
public class StudentAction extends ActionSupport { private Service serivce; private Student stu; public Student getStu() { return stu; } public void setStu(Student stu) { this.stu = stu; } public Service getSerivce() { return serivce; } public void setSerivce(Service serivce) { this.serivce = serivce; } public String add() throws Exception { serivce.saveStudent(stu); return SUCCESS; }}
8.下面为普通jsp页面测试
<form action="add.action" method="post"> username:<input type="text" name="stu.sname"/><br/> description:<input type="text" name="stu.description"/><br/> <input type="submit" value="提交"> </form>
9.下面为测试json对象的访问方式
<script type="text/javascript" src="jquery/jquery-1.7.1.min.js"></script> <script type="text/javascript">$(function() { $("#getStr").click(function() { $.ajax( { type : "POST", url : "json.action", dataType : "json", success : function(msg) { alert("Data Saved: " + msg.name); } }); }); $("#getObj").click(function() { $.ajax( { type : "POST", url : "json.action", dataType : "json", success : function(msg) { alert("Data Saved: " + msg.stu.sid + " " + msg.stu.sname); } }); }); $("#getList").click( function() { $.ajax( { type : "POST", url : "json.action", dataType : "json", success : function(msg) { alert("Data Saved: " + msg.list.length); $(msg.list).each( function(i, domObj) { alert(i + " " + domObj.sid + " " + domObj.sname + " " + domObj.description) }); } }); });});</script> <input type="button" value="getString" id="getStr"><br /> <input type="button" value="getObject" id="getObj"> <br /> <input type="button" value="getList" id="getList"> <br />
0 0
- struts2+hibernate+spring整合
- struts2 spring hibernate 整合
- Spring struts2 hibernate 整合
- struts2+spring+hibernate整合
- struts2+spring+hibernate整合
- struts2+spring+hibernate整合
- Spring、Struts2、Hibernate整合
- Spring + Hibernate + Struts2整合
- struts2+spring+hibernate整合
- struts2+spring+hibernate整合
- Struts2+Hibernate+Spring整合
- Spring Struts2 Hibernate整合
- 整合struts2+spring+hibernate
- spring struts2 hibernate整合
- 整合Struts2,Spring,Hibernate
- Spring + Struts2 + Hibernate整合
- spring+struts2+hibernate整合
- spring、hibernate、struts2整合
- 机器学习经典算法之-----最小二乘法
- Oracle实现auto_increment
- 又一枚神器:nginx
- 2013.1.5
- 附孜速囊料佣殖喜苑崭
- struts2+spring+hibernate整合
- 【cocos2d-x IOS游戏开发】2014年开发及学习计划:cocos2d-x视频专辑
- 巴黎动真格了!为梅西报价4亿 誓夺巴萨头牌
- Python模块包和搜索路径
- MongoDB介绍及视频讲解
- Linux下安装Tomcat(Ubuntu)
- linux 链接ln的使用 创建和删除符号连接(软、硬链接)
- 13123123
- Struts2 spring hibernate 添加 支持顺序