开发者

Struts2 + Jquery Library - What is wrong on this AJAX call?

Im trying some ajax call with Struts2 and the JQuery library. This is the code of the jsp :

<s:div id="my_result" >
    Nothing
</s:div>

<s:form action="UserManager" theme="simple">
    <s:textfield name="nickname" />
    <s:password name="password" />
    <s:label cssClass="menu_span">
        <sj:submit targets="my_result" value="Login" />
    </s:label>
</s:form>

this is the code of the bean/action with the xml :

public class UserManager extends ActionSupport {
    private String nickname;
    private String password;

    @Override
    public String execute() throws Exception {
        String output="i have insered "+this.getNickname()+" and "+this.getPassword();
        System.out.println(output);
        return output;
    }

    public String getNickname() { return nickname; }
    public void setNickname(String newValue) { nickname=newValue; }

    public String getPassword() { return password; }
    public void setPassword(String newValue) { password=newValue; }
}

<package name="model" extends="struts-default">
    <action name="UserManager" class="model.UserManager">
        <result>index.jsp</result>
    </action>
</package>

If i return SUCCESS on the UserManager bean, it load on my_result the same calling page. Else, if i return output i get Messages:

No result defined for action model.UserManager and result i have insered aaa and bbb messagge (with nickname=aaa and password=bbb).

What's happened? How can i return that string? I think it's a struts.开发者_JAVA技巧xml fault

Cheers

UPDATE

public class UserManager extends ActionSupport { private String nickname; private String password; private String error;

@Override
public String execute() throws Exception {
    error="i have insered "+this.getNickname()+" and "+this.getPassword();
    return SUCCESS;
}

public String getNickname() { return nickname; }
public void setNickname(String newValue) { nickname=newValue; }

public String getPassword() { return password; }
public void setPassword(String newValue) { password=newValue; }

public String getError() { return error; }
public void setError(String newValue) { error=newValue; }
}

<s:div id="my_result" >
    <s:property value="error" />
</s:div>

<s:form action="UserManager" theme="simple">
    <s:textfield name="nickname" />
    <s:password name="password" />
    <s:label cssClass="menu_span">
        <sj:submit targets="my_result" value="Login" />
    </s:label>
</s:form>

UPDATE 2

<package name="model" extends="json-default">
    <action name="UserManager" class="model.UserManager">
        <result type="json" />
    </action>
</package>


The return value that you return in the execute() method must be one of the mapped as values. As you have not a result name like this:

<result name="resultName"></result>

It will use the default one: "success". You have to return "success" value in execute() method.

The result value of the execute method is the name of the result mapped. To send a message you have to add a String attribute in your Action (and a getter to obtain it), and in the execute method do something like

 this.message = "my message";

Edit: how to get the message with ajax: If you have set the message in your action in an attribute called "String message" and you have a getter for that attribute (public String getMessage()) you can get this information accesing this property in the object you get in the ajax call. If you do the ajax call for example with jquery you can get it in this way:

$.ajax({
    url: '/yourAjaxUrl',
    success: function(data) {
       var message = data.message;
       alert('The message is '+message);
       $('#my_result').html(message);
    }
});

Edit: In the struts xml in your package tag you have to define the json type:

<result-types>
    <result-type name="json" class="com.googlecode.jsonplugin.JSONResult">
    ...
</result-types>

and add the json-plugin.jar which can be downloaded here

more info in http://code.google.com/p/jsonplugin/wiki/Documentation

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜