Reputation: 30
In this post you made me clear how to deal with JAVA objects in Struts ;
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 Class
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
}
Now I would like to use the same JSP where I fetch data from database (and fit the values back to the form using the employee Object) for update too.
How can I map the em.firstName with the Object firstName variable ?
Edit 1 :
Edited ...
**Update 3 : **
Hope will clear it now.
First i select the employee and click Go button.
$('#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>
Action Class :
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 Button :
<td>
<button type = "button" name = "Go" id= "Go">Go</button>
<button type = "button" name = "Add" id= "Add">Add Employee</button>
</td>
Form Fields:
<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"/>
i tried both the ways and clear the path on what i want to do. removed all the unwanted things and Put the sample code only and try with the Sample one only. Its not working!! can i cleared u ?? can u help me where i missed ??
Upvotes: 0
Views: 2586
Reputation: 30
Solved :
I create an Employee DAO object and assign the DAO for Action Class Getter ,Setter Employee Object Variable.
Here my latest code.
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;
}
And i am getting the value's in the HTML forms. I am using the same page for both add and Edit the Employee Details.
As suggested by Andrea i had removed the HTML standards and Use Struts tags and accepting Andrea reply as Answer.
Thanks alot.
Upvotes: 1
Reputation: 50203
IF you are asking how to map the properties between two objects, one coming from the page, the other from the database, let's say Employee
and EmployeeDto
, you can use Apache Commons BeanUtils.CopyProperties(target,source)
.
It will copy all the properties from the source object to the target object matching them by name.
EDIT:
Since you have only one object, it is already mapped. Simply remove value=""
from all your tags.
Also note that your object must be called em
or emp
in both Action class and JSP, while now they're emp
and em
, hence not matching... and remember to put getters, setters and a default no args constructor for your Employee object too.
Finally, do not use Struts 2.0 that is from 2008, use Struts 2.3.16.x or above.
EDIT 2:
Oh dear. Didn't noticed you was using standard inputs... Use <s:textfield />
tag instead:
<s:textfield name="em.firstName" />
<s:textfield name="em.middleName" />
<s:textfield name="em.lastName" />
Or without using struts tags, with tag in value:
<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'/>" />
Upvotes: 1