Skip to content
Advertisement

How to pass an object from front-end to Struts 2

I am trying to send value of a field to Struts2 back-end through JavaScript but it returns NullpointerException.

<input type="hidden" id="employee.payslip.id" name="employee.payslip.id" value="5"/>
....

Once form is submitted the request will be sent to the following JavaScript method to be sent to back-end.

 function payslipPayment(){

     var formValues = $('#myform').serialize();
     ....
     xmlhttp.open("get","../payslip/pay?"+formValues,false);
     xmlhttp.send();

 }

the request will be created and sent as following

http://localhost/payslip/pay/employee.payslip.id=5&employee.payslip.year=2013&....

But in back-end when I try to show the value it returns NullPointerException.

Java:

public class payslip {

 private Employee employee;

 public String pay{
    System.out.println("Id:"+employee.payslip.id):
    System.out.println("Year:"+employee.payslip.year;
    ...
 }

 getter and setter 

}

Classes:

public class Employee {
   private Payslip payslip;
   ....
   getter and setter
}

public class Payslip{
  private long id;
  ...
  getter and setter
}

Advertisement

Answer

You need to use ModelDriven interface. Implement the ModelDriven and override its method then return your object using method of modelDriven.

Struts2 Documentation

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement