Romeo
Romeo

Reputation: 30

Struts 2.0 request getparameter values to Bean object

I have a jsp page having form of employee details as below.

AddEmp.jsp

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

After Submit the form i got the values in the controller

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

The Struts 2.0 automatically set the values(defined as Public) and can able to access it.

My question is --!! i have a DAO of Employee having all the details like below.

 public class Employee implements java.io.Serializable{
     public String firstName;
     public String lastName;

    public String getFirstName(){
     return firstName;
    }
    public void setFirstName(String firstName){
     this.firstName = firstName;
    }
    public String getLastName(){
     return lastName;
    }
    public void setLastName(String lastName){
     this.lastName = lastName;
    }
 }

Once my HTML form submits from the JSP i want all the fields to be set to the DAO and will directly call the DAO inside the controller instead manually defined each fields in the controller. Can Anybody help on this.? Thx in advance.

P.S : i am new to struts 2.0.

Update 1 : Thanks Guys For your response. And i am Jealous on your Knowledge.

I tried way and thats what the result i exactly want. I accept reply as the Answer. can u please tell the name of the concept we implemented and i like to learn and knew the benefits of the ModelDriven Logic suggested by our friend .

Can any one pls elaborate with some simple example?

Upvotes: 0

Views: 721

Answers (2)

Abhijeet Panwar
Abhijeet Panwar

Reputation: 1857

You have to implement Model ModelDriven interface to your action class.which has method getModel() method to return the Employee’s object. Refer this link : http://struts.apache.org/release/2.3.x/docs/model-driven.html

When using the ModelDriven method you need to initialize the Model object((Employee)) , so the framework will automatically transfers the form data into the Employee class

Upvotes: 0

MSR
MSR

Reputation: 533

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

  //Create Setter and Getter of emp1 object
}

and in AddEmp.jsp

<th>First Name :</th>
<td><input type = "text" id ="firstName" name = "emp1.firstName" value= ""></td>

<th>Last Name :</th>
<td><input type = "text" id ="lastName" name = "emp1.lastName" value= ""></td>

Upvotes: 3

Related Questions