1

我正在使用实现ModelDriven. 我能够从 jQuery 传递数据并将数据保存在数据库中。

当我尝试检索数据并将其传递回 jQuery 时,我不确定为什么它在 jQuery 中不可用。我确信我在基本流程中遗漏了一些东西。

这是我的动作课:

public HttpHeaders index() {
    model = projectService.getProjectDetails(project.getUserID());
    return new DefaultHttpHeaders("success").setLocationId("");
} 

@Override
public Object getModel() {
    return project;
}

public Project getProject() {
    return project;
}

public void setProject(Project project) {
    this.project = project;
}

这是我的 jQuery:

function getProjectDetails() {
    var userID = localStorage.getItem('userID');
    var request = $.ajax({
        url : '/SUH/project.json',
        data : {
            userID : userID
        },
        dataType : 'json',
        type : 'GET',
        async : true
    });

    request.done(function(data) {
        console.log(JSON.stringify(data));
        $.each(data, function(index, element) {
            console.log('element project--->' + index + ":" + element);
            
        });
    });

    request.fail(function(jqXHR, textStatus) {
        console.log('faik');
    });
}

类中的模型对象Action具有所有可用数据,但我尝试返回模型或项目对象,但两者均无效。

4

1 回答 1

1

默认情况下,Struts2 REST 插件使用json-lib序列化你的 bean。如果您正在使用,ModelDriven那么它会在处理结果时直接访问您的模型。由于您.json在请求 URL 中使用扩展名,因此内容类型处理程序由扩展名选择。应该是JsonLibHandler

此处理程序使用JSONArray.fromObject(obj)ifobj是一个数组或列表或JSONObject.fromObject(obj)其他方式来获取JSONObejct可以序列化并写入响应的内容。

obj是返回的值,getModel()在您的情况下它将是project

因为JsonLibHandler使用默认值JsonConfig,所以您不能从要序列化的 bean 中排除属性,除非它们是public字段。

json-lib 的以下功能可以使用JsonConfig

  • 循环检测,有两种默认策略(默认抛出异常),可以自己注册
  • 转为 JSON 时跳过瞬态字段(默认=不跳过)转为 JSON 时跳过 JAP @Transient 注释方法(默认=不跳过)
  • 序列化为 JSON 时排除 bean 属性和/或映射键 (default=['class','metaClass','declaringClass'])
  • 过滤器为序列化为 JSON 或转换回 Java 时排除/包含属性提供了更精细的细节

您可以找到允许您排除某些属性的代码片段。

排除属性

String str = "{'string':'JSON', 'integer': 1, 'double': 2.0, 'boolean': true}";  
JsonConfig jsonConfig = new JsonConfig();  
jsonConfig.setExcludes( new String[]{ "double", "boolean" } );  
JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON( str, jsonConfig );  
assertEquals( "JSON", jsonObject.getString("string") );        
assertEquals( 1, jsonObject.getInt("integer") );        
assertFalse( jsonObject.has("double") );     
assertFalse( jsonObject.has("boolean") );     

排除属性(带过滤器)

String str = "{'string':'JSON', 'integer': 1, 'double': 2.0, 'boolean': true}";  
JsonConfig jsonConfig = new JsonConfig();  
jsonConfig.setJsonPropertyFilter( new PropertyFilter(){    
   public boolean apply( Object source, String name, Object value ) {    
      if( "double".equals(value) || "boolean".equals(value) ){    
         return true;    
      }    
      return false;    
   }    
});    
JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON( str, jsonConfig );  
assertEquals( "JSON", jsonObject.getString("string") );        
assertEquals( 1, jsonObject.getInt("integer") );        
assertFalse( jsonObject.has("double") );     
assertFalse( jsonObject.has("boolean") );

但是您可以选择使用自己的ContentTypeHandler来覆盖默认值。

另一种方法是使用 Jackson 库来处理请求。如文档页面所述:使用 Jackson 框架作为 JSONContentTypeHandler

默认的 JSON 内容处理程序构建在 JSON-lib 之上。如果您更喜欢使用 Jackson 框架进行 JSON 序列化,您可以JacksonLibHandler为您的 json 请求配置 as Content Handler。

首先,您需要通过下载 jar 文件并将其放在 WEB-INF/lib 下或通过在pom.xml使用 maven 作为构建系统时将以下 xml 片段添加到您的依赖项部分来将 jackson 依赖项添加到您的 Web 应用程序。

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-jaxrs</artifactId>
    <version>1.9.13</version>
</dependency>

现在您可以在以下位置使用 Jackson 内容处理程序覆盖内容处理程序struts.xml

<bean type="org.apache.struts2.rest.handler.ContentTypeHandler" name="jackson" class="org.apache.struts2.rest.handler.JacksonLibHandler"/>
<constant name="struts.rest.handlerOverride.json" value="jackson"/>

<!-- Set to false if the json content can be returned for any kind of http method -->
<constant name="struts.rest.content.restrictToGET" value="false"/> 

<!-- Set encoding to UTF-8, default is ISO-8859-1 -->
<constant name="struts.i18n.encoding" value="UTF-8"/>

之后,您可以使用@JsonIgnore注释。

于 2016-06-26T13:29:59.890 回答