Struts 2.x学习---------------结合VO的输入

关于Struts 2.x的学习记录:

十一、结合VO的输入
从2005年开始,VO的开发模式已经深入人心,所有的开发框架必定都要支持VO转换,并且在Struts 2.x里面也支持了VO转换,并且可以多级设置

范例:定义两个VO类以及其关系
package com.lyt.vo;
import java.io.Serializable;
@SuppressWarnings("serial")
public class Dept implements Serializable {
    private Integer deptno;
    private String dname;
    public Integer getDeptno() {
        return deptno;
    }
    public void setDeptno(Integer deptno) {
        this.deptno = deptno;
    }
    public String getDname() {
        return dname;
    }
    public void setDname(String dname) {
        this.dname = dname;
    }
    public String toString() {
        return "Dept [deptno=" + deptno + ", dname=" + dname + "]";
    }
}
package com.lyt.vo;
import java.io.Serializable;
import java.util.Date;
public class Emp implements Serializable {
    private Integer empno;
    private String ename;
    private Double sal;
    private Date hiredate;
    private Dept dept=new Dept();
    public Integer getEmpno() {
        return empno;
    }
    public void setEmpno(Integer empno) {
        this.empno = empno;
    }
    public String getEname() {
        return ename;
    }
    public void setEname(String ename) {
        this.ename = ename;
    }
    public Double getSal() {
        return sal;
    }
    public void setSal(Double sal) {
        this.sal = sal;
    }
    public Date getHiredate() {
        return hiredate;
    }
    public void setHiredate(Date hiredate) {
        this.hiredate = hiredate;
    }
    public Dept getDept() {
        return dept;
    }
    public void setDept(Dept dept) {
        this.dept = dept;
    }
    public String toString() {
        return "Emp [empno=" + empno + ", ename=" + ename + ", sal=" + sal
                + ", hiredate=" + hiredate + ", dept=" + dept + "]";
    }
}
范例:建立一个EmpAction的程序类,在这个程序类里面准备好要接收的数据
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class EmpAction extends ActionSupport {
    private Emp emp=new Emp();
    public Emp getEmp(){
        return emp;
    }
    public String execute() throws Exception{
        System.out.println();
        return "emp.insert";
    }
}
范例:在struts.xml文件里面配置此Action
<action name="EmpAction" class="com.lyt.action.EmpAction">
    <result name="emp.insert">emp_insert.jsp</result>
</action>
后面的任务就是要进行表单的编写,但是在编写表单的时候轻要考虑到名称问题
范例:定义emp_insert.jsp页面
<form action="EmpAction.action" method="post">
          雇员编号:<input type="text" name="emp.empno" value="1009"><br>
         雇员姓名:<input type="text" name="emp.ename" value="张三"><br>
         雇员工资:<input type="text" name="emp.sal" value="1000.0"><br>
         雇员日期:<input type="text" name="emp.hiredate" value="2001-01-01"><br>
         部门编号:<input type="text" name="emp.dept.deptno" value="20"><br>
         部门名称:<input type="text" name="emp.dept.dname" value="开发部"><br>
         <input type="submit" value="提交">
</form>
现在发现整个的操作都可以自动向指定的类型进行转换,这一点的确是很方便,这种转换严格来讲并不安全,因为它必须要求前台用户输入正确格式的数据才可以进行操作

猜你喜欢

转载自blog.csdn.net/amuist_ting/article/details/80904840