struts2 +将Form值设置为POJO并设置POJO Value To form

时间:2014-06-20 06:06:16

标签: java jsp jdbc struts2

this post中,您明确了如何处理Struts中的JAVA对象;

JSP

<th>First Name :</th>
<td><input type="text" id ="em.firstName" name="em.firstName" value=""></td>
<th>Middle Name :</th>
<td><input type="text" id ="em.middleName" name="em.middleName" value=""></td>
<th>Last Name :</th>
<td><input type="text" id ="em.lastName" name="em.lastName" value=""></td>

行动类

public class contTest extends ActionSupport{
  public Employee  em;
  public String execute(){
    System.out.println("firstName-->>>"+em.getFirstName());
    System.out.println("lastName-->>>"+em.getLastName());
    return SUCCESS;
  }

  //Setter and Getter of em object
}

现在我想使用相同的JSP,我从数据库中获取数据(并使用employee对象将值返回到表单)以进行更新。

如何将em.firstName映射到Object firstName变量?

修改1:

已编辑...

**更新3:**

希望现在能清除它。

首先,我选择员工,然后点击“转到”按钮。

$('#Go').click(function(){
    if($("#txtEmpId").val() == "" || $("#txtEmpId_name").val()==""){
        alert("Please Select Employee.");
        return false;
    }
    $.ajax({
        type: 'POST',
        url: 'EmpView.action',
        data: $("#FrmEmp").serialize(),
    }).done(function(data) {
        $("#FrmEmp").submit();
    });
    $('#EmployeeDet,#divEmpId').show();
});

struts.xml中

<action name="EmpView" class="com.Test.Controller.EmpCntr" method= "EmpView">
    /*I tried with Both*/
    <result name = "success" type="json"/>

    or

    <result name="success">/AddEmp.jsp</result>
</action>

行动类:

public String EmpView() throws SQLException, IOException{
    System.out.println("em.firstName--->>>"+em.getFirstName());
    System.out.println("em.lastName--->>>"+em.getLastName());
    try {
        CachedRowset tmList = TmMo.GetEmpViewData(ProcName,ParaName);

        int numcols = tmList.getMetaData().getColumnCount();
        List <String> row = new ArrayList<>(numcols);
        while (tmList.next()) {
            for (int i=1; i<= numcols; i++) {
                row.add(tmList.getString(i));
                em.setEmployeeId(tmList.getString("employeeId"));
                em.setFirstName(tmList.getString("firstName"));
            }
        }
        System.out.println("em.firstName--->>>"+em.getFirstName());
        System.out.println("em.lastName--->>>"+em.getLastName());
        /*Here i am getting the Name */
    } catch (Exception e) {
        e.printStackTrace();
    }
    return Action.SUCCESS;
}

AddEmp.jsp

转到按钮:

<td>
<button type = "button" name = "Go" id= "Go">Go</button>
<button type = "button" name = "Add" id= "Add">Add Employee</button>
</td>

表单字段:

<th>First Name :</th>
<td><input type = "text" id ="em.firstName" name = "em.firstName" value = "<s:property value='em.firstName'/>" ></td>           
    or 
<s:textfield id ="em.firstName" name = "em.firstName"/>

我尝试了两种方式并清除了我想要做的事情。删除了所有不需要的东西并仅放置示例代码并仅尝试使用示例代码。它不工作!!我可以清除你吗?你可以帮助我,我错过了吗?

2 个答案:

答案 0 :(得分:1)

如果你问如何在两个对象之间映射属性,一个来自页面,另一个来自数据库,让我们说EmployeeEmployeeDto,你可以使用{ {3}}

它会将源对象中的所有属性复制到按名称匹配的目标对象。

编辑:

由于您只有一个对象,因此已经映射了该对象。只需从所有代码中删除value=""即可。

另请注意,您的对象必须在Action类和JSP中调用ememp,而现在他们只需要empem,因此不会匹配...并记得为你的Employee对象设置getter,setter和默认的no args构造函数。

最后,不要使用2008年的Struts 2.0,使用Struts 2.3.16.x或更高版本。

编辑2:

亲爱的,亲爱的。没有注意到您使用的是标准输入...请改用<s:textfield />标记:

<s:textfield name="em.firstName"  />
<s:textfield name="em.middleName" />
<s:textfield name="em.lastName"   />

或者不使用struts标签,标签值为:

<input type="text" name="em.firstName"  value="<s:property value='em.firstName'/>"  />
<input type="text" name="em.middleName" value="<s:property value='em.middleName'/>" />
<input type="text" name="em.lastName"   value="<s:property value='em.lastName'/>"   />

答案 1 :(得分:1)

解决:

我创建了一个Employee DAO对象,并为Action Class Getter,Setter Employee Object Variable分配了DAO。

这是我最新的代码。

public String EmpView() throws SQLException, IOException{
    System.out.println("em.firstName--->>>"+em.getFirstName());
    System.out.println("em.lastName--->>>"+em.getLastName());
    try {
        CachedRowset tmList = TmMo.GetEmpViewData(ProcName,ParaName);

        while (tmList.next()) {
            for (int i=1; i<= numcols; i++) {
                Employee emp = new Employee();
                emp.setEmployeeId(tmList.getString("employeeId"));
                emp.setFirstName(tmList.getString("firstName"));
                em = emp;
            }
        }
        System.out.println("em.firstName--->>>"+em.getFirstName());
        System.out.println("em.lastName--->>>"+em.getLastName());
        /*Here i am getting the Name */
    } catch (Exception e) {
        e.printStackTrace();
    }
    return Action.SUCCESS;
}

我在HTML表单中获得了价值。我使用相同的页面添加和编辑员工详细信息。

正如Andrea所建议的那样,我删除了HTML标准和使用Struts标签并接受Andrea回复作为答案。

非常感谢。