0

I have following code and configuration

Pojo

public class MyInfo {
    private String name;
    private String desc;

    //... getters, setters ...
}

My Action

package demo;

//... import statements ...

public class MyAction extends ActionSupport {

    public static final String FAILURE = "failure";

    private MyInfo info;
    private String result;
    private String message;

    public String execute() {
        result = SUCCESS;
        return result;
    }

    public String processInfo() {
        result = FAILURE;
        try {
            String name = info.getName();
            //... More Statements //
            result = SUCCESS;
        } catch(Exeption e) {
            message = "Unable to process information : " + e.getMessage;
        }

        return result;
    }

    //Getter and Setter methods of info, result, and message.


}

Struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">  
<struts>
    <constant name="struts.devMode" value="true" />
    <package name="base-ajax" namespace="/" extends="json-default,struts-default" abstract="true" >
        <global-results>
            <result type="json"></result>
            <result name="failure" type="json"></result>
        </global-results>
    </package>

    <package name="info-ajax" namespace="/" extends="base-ajax">
        <action name="processInfo" method="processInfo" class="demo.MyAction">
            <result type="json"></result>
        </action>
    </package>
<struts>

Rendered JSP snippet

<form id="infoForm" method="post" action="processInfo.action">
    <input id="infoName" type="text" name="info.name"></input>
    <input id="infoDesc" type-"text" naame="info.desc"></input>
    <a id="btn-submit" href="#">Submit</a>
</form>

jQuery in the head section of JSP.

var jQ = jQuery.noConflict();

jQ(document).ready(function() {
    jQ("#btn-submit").click(function() {
        //perform some validation
        var formData = jQ("#infoForm").serialize();
        jQ.ajax({
            url: "processInfo.action",
            data: formData,
            dataType: "json",
            error: function() {
                alert("Some error has occurred while processing request.");
            },
            success: function(response) {
                if(response.result = "failure") {
                    alert("Information processing failed.");
                } else if(response.result) {
                    alert("Information processed successfully.");
                }
            }
        });
    });
});

In most cases it runs smoothly. But sometimes I get NullPointerException in MyAction.processInfo() on info.getName(). It seems info is not populated. I have seen form is being submitted with proper values (used Firebug, and tamper data plugin to analyze). I don't beleive params interceptor skips creating info. There may be something missing in my configuration. Can anyone figure it out or guide me what is happening behind the scene?

2
  • After careful log analysis it is found that this is not struts2 issue. When request is sent to web server, then prior to send it to application, web server tries to parse the request. Log tells that sometimes web server is unable to parse the request parameters. so web server omits parameters from request, and send it to application for processing. I will share the findings after complete analysis and conclusion. Commented Nov 23, 2012 at 4:38
  • Good, I'm curious now :) Commented Nov 23, 2012 at 8:35

1 Answer 1

1

It is not possible it's working that way.

1) You have your Action's methods declared as private;

2) your return SUCCUESS instead of SUCCESS from inside processInfo

I guess they're all errors caused by code-cleaning before posting it here, but be careful :)

P.S: I don't know if it is a bad practice or not, but i strongly suggest you to make Action's names and classes to be related (example, processInfo and demo.myAction pair can work in a demo, but when you will have 100 Actions, you will go mad).

My 2 cents... I know this doesn't answer your question but, as you said, it works most of the time, so this is a random problem hard to debug not being on your machine (and with fake code posted :))

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your correction and suggestions. Let me update the code. And you are too clever, I admit. It is really fake code but I tried to sketch the real implementation. I can't share the real code due to a lot of reasons.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.