【发布时间】:2014-06-20 06:06:15
【问题描述】:
在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 从数据库中获取数据(并使用员工对象将值重新放入表单)进行更新。
如何将 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"/>
我尝试了这两种方法并为我想做的事情扫清了道路。删除了所有不需要的东西并仅放置示例代码并仅尝试使用示例之一。它不工作!我可以清除你吗?你能帮我在哪里错过吗??
【问题讨论】:
-
请停止使用 [tag:] 来称呼您的问题描述中的人...1)它不起作用,2)它是错误的,3)它很难看,4)它没用
-
你确定..下次不会那样做。而你不知道答案仪式?
-
这里没有任何 jQuery。您的对象是
emp而不是em。 -
你的意思是当网页加载它时从数据库中获取数据,或者当你点击提交按钮时它会从数据库中获取数据并再次加载相同的页面?
-
我正在使用提交按钮将表单添加到数据库中。对于更新,我想选择员工 ID 并将数据从数据库获取到员工对象以及从对象到 HTML 表单(如果可能通过 ajax 不重新加载)。我可以从数据库中设置员工对象。但是我怎样才能设置回 HTML 呢??