HardBirch

Spring2.5+Struts2+Jpa(Hibernate实现)整合之三

时间:09-05-15 栏目:iOS移动应用开发技术 作者:张飞不张,文采横飞 评论:4 点击: 2,284 次

改篇主要编写整合struts2beanaction

src下创建包com.zyg.ssj.bean,在该包下创建beanStudent,其代码如下:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Student {
private Integer stuId;
private String stuName;

public Student() {}

public Student(String stuName) {
this.stuName = stuName;
}

@Id @GeneratedValue
public Integer getStuId() {
return stuId;
}
public void setStuId(Integer stuId) {
this.stuId = stuId;
}

@Column(length=20,nullable=false)
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
}

src下创建包com.zyg.ssj.web.action,在该包下创建actionStudentAction,其代码如下:

 

import java.util.List;

import javax.annotation.Resource;
import com.zyg.ssj.bean.Student;
import com.zyg.ssj.service.StudentService;

public class StudentAction {
@Resource
private StudentService studentService;
private List<Student> students;
private Student student;
private String message;

/**
* 获取学生列表
* @return
*/
public String list(){
this.students = studentService.getStudents();
return "list";
}

/**
* 返回新建学生信息界面
* @return
*/
public String add(){
student = new Student();
return "add";
}

/**
* 保存新建的学生信息
* @return
*/
public String save(){
if(null==student.getStuId()){ //新建学生信息
studentService.save(student);
}else{
studentService.update(student);
return "editResult";
}

this.message="学生【"+student.getStuName()+"】信息保存成功!!!";
return "save";
}

/**
* 修改学生信息
* @return
*/
public String edit(){
student = studentService.getStudent(student.getStuId());
return "edit";
}

/**
* 删除学生信息
* @return
*/
public String delete(){
System.out.println(student.getStuId());
studentService.delete(student.getStuId());
return "delResult";
}

/**
* 清空学生信息
*/
private void clearStudentInfo(){
student.setStuId(null);
student.setStuName(null);
}

public StudentService getStudentService() {
return studentService;
}

public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}

public List<Student> getStudents() {
return students;
}

public void setStudents(List<Student> students) {
this.students = students;
}
public Student getStudent() {
return student;
}

public void setStudent(Student student) {
this.student = student;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}

至此,该篇完成了struts2beanaction。下一篇开始编写JSP展示页面。

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

Spring2.5+Struts2+Jpa(Hibernate实现)整合之三:目前有4 条留言

  1. 我有个疑问?
    你的StudentAction既不继承Action接口,也不实现ActionSupport基类。不是很理解………..

    2010-08-06 13:24 [回复]
  2. 地板
    M_ChangGong:

    回复 hebiqibinqu:Structs2的action是纯pojo可以不继承任何父类,当然也可以继承。

    2010-08-06 18:06 [回复]
  3. 板凳
    Clowind:

    LZ您好,看了你的文章很有帮助,但有您是否遗漏了一些代码或贴在其他地方,com.zyg.ssj.service.StudentService这个类我在Spring2.5+Struts2+Jpa(Hibernate实现)整合之(一、二、三、四)中都没见到。望解答谢谢!

    2011-10-05 11:59 [回复]
  4. 沙发
    M_ChangGong:

    [reply]Clowind[/reply]
    不做这个很久了,留个邮箱,我找找代码给你发过去参考。

    2011-10-07 22:02 [回复]

发表评论


QQ群互动

Linux系统与内核学习群:194051772

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

魔豆之路QR

魔豆的Linux内核之路

魔豆的Linux内核之路

优秀工程师当看优秀书籍

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

赞助商广告

友荐云推荐