最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - How to pass an object from front-end to Struts 2 backend through JavaScript? - Stack Overflow

programmeradmin3浏览0评论

I'm trying to send value of a field to Struts 2 backend through JavaScript, but it returns NullpointerException.

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

Once the form is submitted, the request will be sent to the following JavaScript method to be sent to the backend.

 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 the backend, 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
}

I'm trying to send value of a field to Struts 2 backend through JavaScript, but it returns NullpointerException.

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

Once the form is submitted, the request will be sent to the following JavaScript method to be sent to the backend.

 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 the backend, 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
}
Share Improve this question edited Nov 24, 2024 at 17:35 Roman C 1 asked Oct 31, 2013 at 1:46 J888J888 1,9648 gold badges42 silver badges76 bronze badges 9
  • the problem is serialize in js, and unserialize in java. so you should think is the serialized pass to java correctly and is java unserialize correctly after it got the right string – farmer1992 Commented Oct 31, 2013 at 6:01
  • the problem is that I do not know how to figure that out? – J888 Commented Oct 31, 2013 at 22:16
  • 1 What framework are you using to parse the parameter in HTTP request to the Java classes? – leesei Commented Nov 7, 2013 at 4:11
  • 1 Should http://localhost/payslip/pay/employee.payslip.id = 5 be http://localhost/payslip/pay/employee.payslip.id=5 (no space)? – leesei Commented Nov 7, 2013 at 4:12
  • 1 The shown URL 'localhost/payslip/pay' does not have '?' but the code does so it should be fine. I am guessing 'pay' action is mapped to 'payslip' class and the 'pay' method. Also that by employee.payslip.id you actually mean employee.getPayslip().getId(). They should work just fine. Can you say if employee reference itself is null or if getPayslip() returns null ? – Ravindra HV Commented Nov 8, 2013 at 12:37
 |  Show 4 more comments

8 Answers 8

Reset to default 3

Null pointer exception means employee or payslip is not initialized. If youre using struts2 then using param interceptor and model driven approach should solve your issue.

  • You need to convert your form to json (like here) and send to the back end
  • Then - in java you need to deserialize the json to a java object (for example, with Jackson)

You can do like Alexey has said or you can follow this approach. You can use the interfaces ServletRequestAware and ServletResponseAware and then use request.getParameter() to get the value that is passed. Below is an example of how to do this.

Action class struts config

<action name="hello" method="execute"
            class="com.home.struts2.HelloAction">
            <result name="success">hello.jsp</result> 
        </action>

Action Class

 public class HelloAction extends ActionSupport implements ServletRequestAware,ServletResponseAware{
        HttpServletResponse response;
        HttpServletRequest request;

        public String execute() {

             System.out.println("AjaxCall" + request.getParameter("param"));
              String infoXml = "Parameter passed: " + request.getParameter("param");
              response.setContentType("text/html");
                response.setHeader("Cache-Control", "no-cache");
                try {
                    response.getWriter().write(infoXml);
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }

                return null;

        }

        public void setServletResponse(HttpServletResponse response) {
            this.response = response;
        }

        public void setServletRequest(HttpServletRequest request) {
            this.request = request;
        }

        public HttpServletRequest getServletRequest() {
            return this.request;
        }

    }

JavaScript

var xmlHttp;
function sendreq(){  

        var URL = "hello.action?param=sandy";
        try{
            xmlHttp=new XMLHttpRequest();
        }catch (e){
            try{
                xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
            }catch (e){
                try{
                    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
                }catch (e){
                    alert("Your browser does not support AJAX!");
                    return false;
                }
            }
        }

        xmlHttp.onreadystatechange = showMessage;

        xmlHttp.open("GET", URL, true);

        xmlHttp.send(null);
    }
  1. Implement the serializable interface for Employee & Payslip. Also, make sure both have empty constructors.

  2. Enable struts2 devMode for additional logs. After enabling you'll see OGNL exceptions, about which field in particular is giving the issue. That error must be very straight forward to handle. http://www.mkyong.com/struts2/struts-2-development-mode-example/

  3. I guess there's some issue with the interceptor stack used for that action, just try using defaultStack.

<action name="payslip/pay" class="...">
    <interceptor-ref name="defaultStack"/>
    <result name="success">/success.jsp</result>
    <result name="input">/error.jsp</result>
</action>

Are you using a framework, or just a plain servlet? If you're using a framework like struts, then all of these answers are way too complicated.

I think you are using just a mapped servlet by the looks of it, which means the answer above is correct about passing named parameters through the url. You can hide these named parameters a little more using post, rather than get though.

Here's how I would do it using just a servlet. Though, if I were you and doing a full project, I would use JSF, for easier passing of data.

java

public void doPost(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
    String payslipID = request.getParameter("payslipid");
}

js

<script>
var payslipid = $('#employee_payslip_id')
xmlhttp.open("POST","../payslip/pay",false);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("payslipid="+payslipid);
</script>

As mentioned in the other post though, you would have to get (and place in post or get get data) all the parameters individually unless you did some separated list of strings. This isn't really a ridiculous thing to do that because you will need to separate all of those values into their own variables in the java anyway.

EDIT:

The above answer actually says what you need to do but maybe it would help to make it more clear. In your struts configuration files where you define the action such as

<action class="org.example.Payslip" name="payslip">
            <interceptor-ref name="params"/>
            <result name="success">/success.jsp</result>
</action>

Make sure to have the params interceptor there. It will make it so the server maps the params to the setters. Then you won't need to do the getparameter call for each thing you send.

Also the url should be something like .../payslip.action!pay?payslipid=1234&employeeid=1234 if you have it mapped like above. This tell it to use the payslip.class and the pay function.

add input param in action tag; that lets you communicate the parameter from struts action class to the form/.jsp

struts.xml

<action name="..." class="UserAction">
 <param name="id"></param>
 <result name="input">/WEB-INF/jsp/User.jsp</result>
</action>

Action class

public class UserAction {
private int id;

// this is called by the struts.xml to set the value
public void setId(int id) {
this.id = id;
}

//a getter is needed as well to display value in the .jsp

}

since, it's a hidden variable not part of valuestack you can try passing it this way.

if this is not your problem; I think you have to inject the bean into the Action Class. @Inject Employee; bean into Action class would help.

Also debug the valuestack.

Struts2 uses params interceptor to populate form values submitted by HTTP request.

It's recommended to use POST method, so if you have sensitive data and you want it is not reflected in the URL.

Parameter names are used by the interceptor to pass through OGNL runtime to evaluate the expression which should find reference in the valueStack.

You've got NullPointerException because you didn't initialize or inject the dependencies to the object which reference is evaluated by OGNL. Because you are using nested beans, all nested properties should be accessible and initialized (or at least Struts2 knows how to create these objects), and belong to the action class that is placed on the top of the value stack.

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

Struts2 Documentation

发布评论

评论列表(0)

  1. 暂无评论