2014-06-20 191 views
0

this post您让我清楚如何处理Struts中的JAVA对象;struts2 +将表单值设置为POJO并将POJO值设置为

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> 

Action类

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其中I取从数据库中的数据(和使用雇员对象适合值返回到表格)也用于更新。

如何映射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

Go按钮:

<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"/> 

我试用过的方式和清除是我想做的路径。删除了所有不需要的东西,并仅放入示例代码并仅尝试使用示例代码。它不工作!我可以清理你吗?你能帮助我,我错过了吗?

+0

请停止使用[tag:]在您的问题描述中打电话给别人...... 1)不工作,2)错了,3)很丑,4)没用, –

+0

雅凛当然..不会那么做时间。你不知道答案? – Romeo

+0

您在这里没有任何jQuery。你的对象是'emp'而不是'em'。 –

回答

1

如果你问如何将性能两个对象之间的映射,从一个网页来了,其他的从数据库中,让我们说EmployeeEmployeeDto,您可以使用Apache Commons BeanUtils.CopyProperties(target,source)

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

编辑:

由于您只有一个对象,它已被映射。只需从您的所有标签中删除value=""

另外请注意,您的对象必须调用em或在两个Action类和JSP emp,而现在他们empem,因此不匹配...并记得把干将,setter和一个默认的无参数的构造你的Employee对象也是如此。

最后,不要使用来自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'/>" /> 
+0

但我没有2对象的,我有一个JSP页面的HTML表单,为添加我使用了上述逻辑。我只想同样显示现有的数据,意味着我从数据库中检索数据并将其存入Employee对象,然后我想在JSP中显示这些值。在这里,我的名字和ID属性都以“em”开头。我如何映射它? – Romeo

+0

将尝试它,并与代码取回你。非常感谢。 – Romeo

+0

你有没有试过@SankarNadar? –

1

解决:

我创建一个Employee DAO对象并为Action类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答复作为答案。

非常感谢。