3

我正在尝试将用户定义的对象发布到 struts2 的操作,但我的操作对象仍然为空。谁能帮我?感谢帮助。

用户.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;
    }
}

这是我的操作 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;
    }
}

我在jsp页面中的ajax调用:

    $(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>
4

1 回答 1

2

最后,我解决了它:D。只需删除contentType: "application/json"ajax 调用中的行。现在的ajax调用是:

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

谢谢大家 :D

于 2013-09-11T12:44:17.447 回答