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配置文件当中依次配置daoservice以及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对象省略getset以及构造方法

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
原创粉丝点击