3

i'm trying to post user-defined object to struts2's action, but my object in action is still null. Can anyone help me? Thanks for help.

User.java

public class User {
    private String username;
    private String password;

    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

This is my action Login.java

public class Login extends ActionSupport {
    private User user;

    @Override
    public String execute() throws Exception {
        System.out.println(user.getUsername() + " - " + user.getPassword());
        return "success";
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}

my ajax call in jsp page:

    $(function() {
        var user = {'user.username': 'abc', 'user.password' : '123'};
        //var user = {username: 'abc', password : '123'};
        $.ajax({
            type : 'POST',
            url : 'login',
            data : JSON.stringify(user),
            //data: user,
            contentType : 'application/json',
            success : function(result) {
                alert("hh");
            }
        });
    });

struts.xml

<struts>
    <constant name="struts.devMode" value="true" />
    <package name="struts2jquery" extends="struts-default, json-default">
        <action name="login" class="com.myapp.action.Login">
            <result name="success">result.jsp</result>
        </action>
    </package>
</struts>
6
  • +1, nicely spelled and formatted answer, with all the relevant parts and no noise around, you're starting good... BTW, I found strange that you are performing an AJAX call (that should stay in the same page after doing its business) and then returning a JSP file as a result. You probably want to return a JSON result, parse it into page then perform a redirect, or something similar... the returned result of an AJAX call can't change the whole page, that is the business of a standard call... Commented Sep 11, 2013 at 8:25
  • Thanks for your advice. I'll return a JSON result, but I don't have my post data in struts action, so I just return a jsp page for test^^. Commented Sep 11, 2013 at 8:52
  • What ? Return a json for test... :) And put a Log line in each setter (of the user object in the Action, and inside the user Object), to see what is happening. Commented Sep 11, 2013 at 8:59
  • Yes, I want to know why struts cannot map my post data to object in action, while everything work fine if I use struts tag. I am trying to debug into struts framework but it seem vary complicated @@ Commented Sep 11, 2013 at 9:13
  • To debug in Eclipse, run Debug Configuration -> Remote Java Application, set the Host and the DEBUG PORT (your Application Server must run with debug mode enabled) and press DEBUG. Then set your breakpoints and use the WebApp Commented Sep 11, 2013 at 9:17

1 Answer 1

2

Finally, I resolved it :D. Just remove line contentType: "application/json" in the ajax call. The ajax call now is:

        var u = {'user.username': 'abc', 'user.password' : '123'};
        $.ajax({
            type : 'POST',
            url : 'login',
            data: u,
            //contentType: "application/json",
            success : function(result) {
                //alert("hh");
            }
        });

Thanks everyone :D

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

Comments

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.