属性setData的不兼容类型[JSON + Struts2插件] [英] Incompatible types for property setData [JSON+Struts2 plugin]

查看:259
本文介绍了属性setData的不兼容类型[JSON + Struts2插件]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要配置JSON插件( https://struts.apache.org/docs /json-plugin.html )与我当前的JAVA Web应用程序配合使用.我所做的:

I need to configure JSON Plugin (https://struts.apache.org/docs/json-plugin.html) with my current JAVA web application. What I've done:

1)将插件的所有依赖项添加到pom.xml文件中.

1) Added to pom.xml file all dependencies for the plugin.

<dependency>
   <groupId>org.apache.struts</groupId>
   <artifactId>struts2-json-plugin</artifactId>
   <version>2.3.24.1</version>
</dependency>

2)用json-default

<package name="my-test-package" extends="xxx, json-default"
    namespace="/my-test-package">

3)在我的操作中添加了json的拦截器和结果类型

3) Added to my action the interceptor and result type of json

<action name="myMainAction"
    class="org.com.action.myMainAction">
        <interceptor-ref name="json">
           <param name="enableSMD">true</param>
        </interceptor-ref>
        <result type="json">                    
        </result>           
    <param name="requiredAuthorities">F_ADD</param>
</action>

4)创建了一个仅包含setter/getter的Model类Marriage.

4) Created a Model class Marriage with only setters/getters in it.

5)使用以下代码创建名为myMainAction的Action类

5) Created the Action class, named as myMainAction with following code

private List <Marriage> data;

public List<Marriage> getData() {
    return data;
}
public void setData(List<Marriage> data) {
     System.out.println("Inside the setter ");
    this.data = data;
}


@Override
public String execute() throws Exception {

    System.out.println(data.get(0).getLastNameHe());

    return SUCCESS;
}

6)创建一个JSON对象,并将其通过ajax传递给服务器.我可以通过Google Chrome控制台看到它发送数据,并且确实解析JSON对象,并且所有数据都在其中.但是,它的确会在我的Eclipse和Chrome控制台中引发异常:

6) Created a JSON object and passed it via ajax to the server. I can see through Google Chrome Console, that it sends the data and it does parse the JSON object and all data is in there. However, it does throw an exception in my Eclipse and also in Chrome Console:

在处理请求期间发生异常:属性setData的类型不兼容(CommonsLogger.java [qtp1144623021-22]) org.apache.struts2.json.JSONException:属性setData的类型不兼容

Exception occurred during processing request: Incompatible types for property setData (CommonsLogger.java [qtp1144623021-22]) org.apache.struts2.json.JSONException: Incompatible types for property setData

能否请您指出正确的方向,这几乎是我正在努力的第二天,但似乎还没有开始.

Could you please point me to some right direction, it's almost the second day which i'm working on it but it doesn't seem to start working.

正在发送到Java端的数据,我可以通过Chrome控制台查看.

The data which is being sent to java side and I can see it via Chrome console.

{"data":{"recordId":"123","registrationDate":"20-07-2016","hisId":"","herId":"","lastNameHe":"Asd","firstNameHe":"Asd","middleNameHe":"Asd","workPlaceHe":"","educationHe"}}

这是JSON.stringify -ed之后的对象. 如您所见,它具有根data变量,然后所有其他数据都在它下面.根据我的逻辑,ti必须在我的java类中找到数据的setter,然后调用该类的setters/getters. P.S我重命名了类的名称,等等,所以不用担心命名约定.)我在生产代码中确实有正确的东西.

This is the object after it has been JSON.stringify-ed. As you can see it has the root data variable and then all the rest of the data goes under it. According to my logic ti has to find the setter of the data in my java class and then call the setters/getters of that class. P.S I renamed the name of the classes and etc so no worries about naming conventions) I do have the right things on my production code.

感谢您指出我的错误@AleksandrM.现在,它引发了一个非常奇怪的错误:

Thanks for pointing out to my mistake @AleksandrM. Now It throws a very strange error:

不可解析的日期:"20-07-2016"(CommonsLogger.java [qtp212900572-17]) java.text.ParseException:无法解析的日期:"20-07-2016"

Unparseable date: "20-07-2016" (CommonsLogger.java [qtp212900572-17]) java.text.ParseException: Unparseable date: "20-07-2016"

这部分的json变量是registrationDate及其在JAVA中的实现:

The json variable for this part is registrationDate and its implementation in JAVA:

 private Date registrationDate;
    public Date getRegistrationDate() {
    return registrationDate;
}

public void setRegistrationDate(Date registrationDate) {
    this.registrationDate = registrationDate;

据我所知,它试图将string设置为date,但它无法做到.为什么json-plugin无法转换?我应该在选项/设置中写些东西吗?

As I understand it tries to set a string to date and it can't do it. Why the json-plugin can't convert it? Should I write something in the options/settings?

推荐答案

您期望在Java代码中使用List<Marriage> data,但是您的JSON对象不包含任何数组.将Java代码更改为Marriage data或将对象数组发布为JSON.

You are expecting List<Marriage> data in the Java code but your JSON object doesn't hold any array. Either change Java code to Marriage data or post array of objects in JSON.

您可能还想为该动作定义除json以外的其他拦截器. 创建自定义拦截器堆栈,并在您的操作配置中引用它.

You may also want to define other interceptors, besides json, for the action. Create custom interceptor stack and reference it in your action configuration.

<interceptor-stack name="json-stack">
    <interceptor-ref name="json" />
    <interceptor-ref name="defaultStack" />
</interceptor-stack>

对于日期字符串到Date对象的转换,请使用默认格式(为yyyy-MM-dd'T'HH:mm:ss)或创建自定义.

For the date string to Date object conversion use default format (which is yyyy-MM-dd'T'HH:mm:ss) or create a custom Struts type converter. or use @JSON(format = "dd.MM.yyyy") on the setter.

请参见此后续问题.

这篇关于属性setData的不兼容类型[JSON + Struts2插件]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆