从ActionClass到jsp Struts2获取数据

问题描述:

我目前正在研究一个学校项目,该项目包括使用Struts2/jsp开发游戏. 我的问题是我无法使用jQuery将数据从ActionClass传递到JSP. 反之,它很好用,我找到了多个来源.

I'm currently working on a school project which consists of developing a game using Struts2/jsp. The problem I have is I can't get Data from my ActionClass to my JSP using jQuery. It works great the other way and I found multiple sources to do so.

这是我最小化的"GameAction.class":

Here is my minimized "GameAction.class" :

private String playerColor;
private Map<String,Object> applicationMap;

public String execute (){

playerColor = ((Joueur)applicationMap.get("joueur")).getPlayerColor();
    return SUCCESS;
}

注意:一切都有一个getter/setter.

NOTE: everything has a getter/setter.

game.js:

var $playerColor;
$(window).on('load', function () {

$.ajax({
    type : "GET",
    url : "gotoGameAction",
    data : "playerColor=",
    success : function (data) {
    $playerColor = data;
    var html = "<h2>" + $playerColor.toString() + "</h2>";
    $("#playerColor").html(html);
  }

})

});

Struts.xml:

Struts.xml :

<package name="default" extends="json-default" namespace="/">
    <action name="gotoGameAction" class="actions.logins.GameAction">
        <result name="success" type="json">/WEB-INF/views/game.jsp</result>
    </action>
</package>

我在JSP上的输出是:[object Object].

This output I have on my JSP is : [object Object].

我真的不知道发生了什么事,有人可以帮忙吗? 谢谢!

I really can't get what's going on there can someone help? Thank you!

您可能不了解什么是JSON结果类型以及执行此结果类型的操作如何提供内容.

You might not understand what is a JSON result type and how the content is provided by the action which is executing this result type.

要了解有关插件和文档的更多信息,请参见 JSON插件.

To get more about plugin and documentation see JSON Plugin.

您还可以在正在执行的类的源代码中查找

You can also look in the source code for the class which is being executed the JSONResult. Then you see that

/**
 * This result type doesn't have a default param, null is ok to reduce noise in logs
 */
public static final String DEFAULT_PARAM = null;

但是,如果您正在调试代码,这种噪声有时会有所帮助.

这在您的代码中毫无意义

That makes no any sense in your code

<result name="success" type="json">/WEB-INF/views/game.jsp</result>

您需要深入研究本教程和示例,以及如何使用此结果.其中之一是您可以在此处找到.

The you need to dig into the tutorial and examples how to use this result. One of them you can find here.

另一个链接可以帮助您确定是否需要json结果或使用其他适合JSON响应的结果.

Another link might help you to decide whether you need a json result or use any other result suitable for JSON response.

将所有内容组合在一起将产生一个想法,以重写您的代码以使其正常工作.

Combining all that together will make an idea to rewrite your code to get it working.