有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法访问struts操作类中的JSON请求对象

我通过JavaScript函数中的Ajax调用传递JSON对象。我已将生成的操作链接到struts操作类。我有action类中的控件,但是没有调用我的setter,因此我面临一个NullPointerException

这是我在JavaScript中的Ajax调用

var arr = {entity:entity,subEntity:subEntity,choice:choice,drinkingWaterChosen:drinkingWaterChosen,roadChosen:roadChosen,groupChoice:groupChoice};   
console.log(JSON.stringify(arr));

url = "<s:url action='FetchQuery'/>";
$.ajax({
  type : 'POST',
  url : url,
  dataType : 'json',
  contentType: 'application/json; charset=utf-8',
  data:JSON.stringify(arr),
  async : false,
  success : function(data) {
      console.log(data);
      alert("I am back "+data);
  }
});

我的Struts配置文件如下所示

<struts>
<package name="default" extends="json-default">
    <result-types>
        <result-type name="json" class="org.apache.struts2.json.JSONResult" />
    </result-types>
    <interceptors>
        <interceptor name="json" class="org.apache.struts2.json.JSONInterceptor" />
    </interceptors>

    <action name="FetchQuery" class="serp.FetchQuery">
        <result type="json"/>
    </action>
</package>

我的动作类如下所示

private String entity;//getter and setters for all variables
private String subEntity;
private String choice;
private String drinkingWaterChosen;
private String roadChosen;
private String groupChoice;

public String execute(){

    System.out.println("Hello world>>>>>>>>>>>>>>>>>");
    String msg = constructQuery();
    return msg;
}

private String constructQuery() {

    if (entity.equals("Hello")) {} //Null pointer exception is raised at this line
}

我认为这个错误是因为没有预先设置实体。 所以我的问题是如何处理传递给action类的JSON对象。我在这个网站上读到了很多问题,并尝试了所有的方法,但我仍然无法找出问题所在。任何帮助都会很好


共 (2) 个答案

  1. # 1 楼答案

    要处理JSON对象,需要将json拦截器添加到操作配置中

    <action name="FetchQuery" class="serp.FetchQuery">
        <result type="json"/>
      <interceptor-ref name="json">
      <interceptor-ref name="defaultStack">
    </action>
    

    json拦截器能够解析请求,并用数据填充操作对象。这是文档页面的摘录:

    If the interceptor is used, the action will be populated from the JSON content in the request, these are the rules of the interceptor:

    • The "content-type" must be "application/json"
    • The JSON content must be well formed, see json.org for grammar.
    • Action must have a public "setter" method for fields that must be populated.
    • Supported types for population are: Primitives (int,long...String), Date, List, Map, Primitive Arrays, Other class (more on this later), and Array of Other class.
    • Any object in JSON, that is to be populated inside a list, or a map, will be of type Map (mapping from properties to values), any whole number will be of type Long, any decimal number will be of type Double, and any array of type List.
  2. # 2 楼答案

    错误可能是您的数组,缺少双引号。将其更改为以下表格:

    var arr = {
                  'entity' : entity, 
               'subEntity' : subEntity, 
                  'choice' : choice,
     'drinkingWaterChosen' : drinkingWaterChosen, 
              'roadChosen' : roadChosen, 
             'groupChoice' : groupChoice
    };