Spring2.5+Struts1.3.8+Jpa(Hibernate实现)整合之七

转载请注明:来自http://blog.csdn.net/M_ChangGong/ 作者:张燕广

该篇主要编写FormBeanaction

src下创建包com.zyg.ssj.web.formbean,在该包下创建FormBean,名称为StudentForm,其代码如下:

package com.zyg.ssj.web.formbean; import org.apache.struts.action.ActionForm; public class StudentForm extends ActionForm { private Integer stuId; private String stuName; public Integer getStuId() { return stuId; } public void setStuId(Integer stuId) { this.stuId = stuId; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } }

src下创建包com.zyg.ssj.web.action,在该包下创建StudentActionStudentManageAction

StudentAction代码如下:

package com.zyg.ssj.web.action; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; 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 com.zyg.ssj.bean.Student; import com.zyg.ssj.service.StudentService; public class StudentAction extends Action { @Resource private StudentService studentService; @Override public ActionForward execute(ActionMapping mapping, ActionForm formBean, HttpServletRequest request, HttpServletResponse response) throws Exception { request.setAttribute("students", studentService.getStudents()); for(Student stu : studentService.getStudents()){ System.out.println(stu.getStuName()); } return mapping.findForward("list"); } }

 

StudentManageAction代码如下:

package com.zyg.ssj.web.action; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; 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 com.zyg.ssj.bean.Student; import com.zyg.ssj.service.StudentService; import com.zyg.ssj.web.formbean.StudentForm; public class StudentManageAction extends Action { @Resource private StudentService studentService; public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { StudentForm formBean = (StudentForm)form; if(formBean.getStuName()!=null && !formBean.getStuName().equals("")){ studentService.save(new Student(formBean.getStuName())); request.setAttribute("message", "学生【"+formBean.getStuName()+"】信息添加成功!"); }else{ request.setAttribute("message", "学生信息不能为空,添加失败!"); } return mapping.findForward("message"); } }

至此,FromBeanaction编写完毕。下一篇编写jsp展示页面。

 

你可能感兴趣的:(spring,exception,Hibernate,struts,jpa,Integer)