HardBirch

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

时间:09-05-13 栏目:iOS移动应用开发技术 作者:张飞不张,文采横飞 评论:0 点击: 1,293 次

该篇主要编写FormBeanaction

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

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代码如下:

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代码如下:

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展示页面。

声明: 本文由( 张飞不张,文采横飞 )原创编译,转载请保留链接: Spring2.5+Struts1.3.8+Jpa(Hibernate实现)整合之七

Spring2.5+Struts1.3.8+Jpa(Hibernate实现)整合之七:等您坐沙发呢!

发表评论


QQ群互动

Linux系统与内核学习群:194051772

WP建站技术学习交流群:194062106

魔豆之路QR

魔豆的Linux内核之路

魔豆的Linux内核之路

优秀工程师当看优秀书籍

优秀程序员,要看优秀书!

赞助商广告

友荐云推荐