Struts2 + Jquery 库 - 此 AJAX 调用出了什么问题?

发布于 2024-10-06 10:58:01 字数 2724 浏览 1 评论 0 原文

我正在尝试使用 Struts2 和 JQuery 库进行 ajax 调用。这是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>

这是带有xml的bean/action的代码:

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>

如果我在UserManager bean上返回SUCCESS,它会加载到相同的my_result上呼叫页面。否则,如果我返回输出,我会得到消息:
没有为操作模型定义结果。UserManager 和结果我已插入 aaa 和 bbb
消息(昵称=aaa,密码=bbb)。

发生了什么事?我怎样才能返回该字符串?我认为这是一个 struts.xml 错误

干杯

更新

public class UserManager extends ActionSupport { 私有字符串昵称; 私有字符串密码; 私有字符串错误;

@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>

更新2

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

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.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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉栀 2024-10-13 10:58:01

您在execute() 方法中返回的返回值必须是映射为值之一。
由于您没有这样的结果名称:

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

它将使用默认名称:“成功”。您必须在execute() 方法中返回“成功”值。

执行方法的结果值是映射的结果的名称。要发送消息,您必须在 Action 中添加 String 属性(以及获取它的 getter),并在执行方法中执行类似

 this.message = "my message";

编辑:如何使用 ajax 获取消息:
如果您在操作中的名为“String message”的属性中设置了消息,并且您有该属性的 getter (public String getMessage()),则可以通过访问 ajax 调用中获取的对象中的此属性来获取此信息。如果您使用 jquery 进行 ajax 调用,您可以通过以下方式获取它:

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

编辑:
在包标记的 struts xml 中,您必须定义 json 类型:

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

并添加可以下载的 json-plugin.jar 此处

更多信息位于 http://code. google.com/p/jsonplugin/wiki/Documentation

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文