在Struts+Spring+hibernate中,将FormBean与EntityBean进行整合(续)

来源:互联网 发布:网络推广平台哪个便宜 编辑:程序博客网 时间:2024/05/19 09:41

具体步骤如下:

 

1、新建项目FormBeanEntityBean

2、向项目中添加Struts和Hibernate工具包

3、创建数据库test

   建立表student

 

 

 

 

 

 

 

4、修改student.java

public class Student  implements java.io.Serializable

public class Student extends ActionForm

 

//添加属性以及对应的getset方法

    private FormFile file;

    public FormFile getFile() {

        return file;

    }

    public void setFile(FormFile file) {

        this.file = file;

    }

 

5、修改student.hbm.xml

去掉catalog="test"

 

6、修改hibernate.cfg.xml

jdbc:microsoft:sqlserver://localhost:1433

修改为:

jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test;

 

7、修改struts-config.xml

<form-beans />

 

    <form-beans>

        <form-bean name="student" type="com.Jcuckoo.struts.FormBean.Student" />

    </form-beans>

 

8、创建输入界面:index.jsp

<%@ page language="java" pageEncoding="gbk"%>

<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"

    prefix="bean"%>

<%@ taglib uri="http://jakarta.apache.org/struts/tags-html"

    prefix="html"%>

<html>

    <head> </head>

    <body>

       <html:form method="post" action="saveStudent"

           enctype="multipart/form-data">

  用户名:<html:text property="stuName"></html:text> <br>

  密码:<html:password property="stuPass"></html:password>  <br>

  照片:<html:file property="file"></html:file>   <br>

           <html:submit></html:submit>

       </html:form>

    </body>

</html>

 

9、创建处理的Aciton(SaveStudent.java)

    public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {

       Student student = (Student) form;

       String path=student.getFile().getFileName();

       student.setImagePath(path);

       Session session=HibernateSessionFactory.getSession();

       Transaction tx=session.beginTransaction();

       session.save(student);

       tx.commit();

       return null;

    }

10、说明:在这里并没有实现文件上传,仅仅测试了将StrutsFormbeanHibernateEntityBean实体,整合成一个Bean,从而实现代码重用性。

原创粉丝点击