struts 2多对一关联示例

时间:2017-07-19 06:37:37

标签: java hibernate jsp struts2 ognl

在jsp中我使用ognl表达式来获取部门列表。并且有两个bean类学生和部门。使用Hibernate多对一个关联。学生bean包含Department dept_id;字段。

Student.class

public class Student {

    private int id;
    private String studentName;
    private Department dept_id;

    public Student() {}

    public Student(int id,String studentName,Department dept_id) {
        this.id = id;
        this.studentName = studentName;
        this.dept_id = dept_id;

    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public Department getDept_id() {
        return dept_id;
    }

    public void setDept_id(Department dept_id) {
        this.dept_id = dept_id;
    }
}

Department.class

public class Department {

    private int dept_id;
    private String deptName;

    public Department() {}

    public Department(int dept_id,String deptName) {
        this.dept_id = dept_id;
        this.deptName = deptName;
    }

    public int getDept_id() {
        return dept_id;
    }

    public void setDept_id(int dept_id) {
        this.dept_id = dept_id;
    }

    public String getDeptName() {
        return deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }
}

的index.jsp

<s:form action="insertData">
<s:textfield name = "studentName" label = "Name"/>
<s:select  list="#{'1':'IT', '2':'COM', '3':'EC'}" name="dept_id" 
label="Select Department"/> 
<s:submit value = "Submit"/>
</s:form>

现在获取select标签value.it是在字符串中我必须以对象形式存储dept_id,因为我在database.how中使用外键来获取动作类中的对象值?

0 个答案:

没有答案