plugins - 如何在结果中使用约定插件配置我的自定义结果类型

标签 plugins struts2 conventions

我创建了自定义结果类来将 json 数据序列化为 xml。我想通过约定插件将此结果类配置为某些特定操作的结果类型。 但是它在启动容器时出错。我的代码和错误如下。

我的自定义结果类:

package actions;

import example.*;
import java.io.PrintWriter;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.Result;
import com.opensymphony.xwork2.util.ValueStack;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver;


public class JSONResult implements Result {

public static final String DEFAULT_PARAM = "classAlias";
String classAlias;

public String getClassAlias() {
    return classAlias;
}

public void setClassAlias(String classAlias) {
    this.classAlias = classAlias;
}

public void execute(ActionInvocation invocation) throws Exception {
    System.out.println("executing JSONResult execute()");
    ServletActionContext.getResponse().setContentType("text/plain");
    PrintWriter responseStream = ServletActionContext.getResponse().getWriter();
    /* Retrieve Objects to Serialize to JSON from ValueStack */
    ValueStack valueStack = invocation.getStack();
    Object jsonModel = valueStack.findValue("jsonModel");

    XStream xstream = new XStream(new JsonHierarchicalStreamDriver());

   /*
     * If there's no parameter passed in, just write the objects under a
     * default name.
     */
    if (classAlias == null) {
        classAlias = "object";
    }
    xstream.registerConverter(new XStreamHashConverter());
    xstream.alias(classAlias, jsonModel.getClass());

    /* Write to the response stream */
    System.out.println("xstream.toXML(jsonModel) == "+xstream.toXML(jsonModel));
    responseStream.println(xstream.toXML(jsonModel));
}
}

我的带注释的 Actions 类如下:

package actions;

import com.opensymphony.xwork2.ActionSupport;
import java.util.HashMap;
import java.util.Map;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.interceptor.ParameterAware;


@ParentPackage("actions")
@Namespace("/actions")
public class ZipDataSupplier extends ActionSupport implements ParameterAware
{
 private Map parameters;
 private Object jsonModel;

 public Map getParameters()
 {
    return this.parameters;
 }
 public void setParameters(Map parameters)
 {
   this.parameters=parameters;
 }
public Object getJsonModel()
{
   return this.jsonModel;
}
public void setJsonModel(Object jsonModel)
{
   this.jsonModel = jsonModel;
}

@Action(value="/getZipData",results={@Result(name="success",location="ajaxCall", **type="actions.JSONResult")**})
public String getZipData()
{
   System.out.println("inside getZipData ... ...");
   Map map =  getParameters();
   System.out.println("parameter map = "+map);
   String htmlIds = ((String[])map.get("htmlIds"))[0];
   System.out.println("htmlIds = "+htmlIds);
   String jsonIds = ((String[])map.get("jsonIds"))[0];
   System.out.println("jsonIds = "+jsonIds);
   ZipData zipData = new ZipData();
   zipData.getCity().put("Dulles", "Dulles");
   zipData.getCity().put("New York", "New York");
   setJsonModel(zipData);
   return SUCCESS;
 }
}

class ZipData
{
private String zipCode = "20101";
private String stateCode = "VA";
private String stateName = "Virginia";

private HashMap<String,String> city=new HashMap<String, String>();

//private JSONObject city = null;//JSONArray.fromObject( getCityMap());

/**
 * @return the zipCode
 */
public String getZipCode() {
    return zipCode;
}

/**
 * @param zipCode the zipCode to set
 */
public void setZipCode(String zipCode) {
    this.zipCode = zipCode;
}

/**
 * @return the stateCode
 */
public String getStateCode() {
    return stateCode;
}

/**
 * @param stateCode the stateCode to set
 */
public void setStateCode(String stateCode) {
    this.stateCode = stateCode;
}

/**
 * @return the stateName
 */
public String getStateName() {
    return stateName;
}

/**
 * @param stateName the stateName to set
 */
public void setStateName(String stateName) {
    this.stateName = stateName;
}

/**
 * @return the city
 /
public JSONObject getCity() {
    this.city = JSONObject.fromObject( getCityMap());
    return this.city;
}

/**
 * @param city the city to set
 /
public void setCity(JSONObject city) {
    this.city = city;
}

/**
 * @return the cityMap
 */
public HashMap<String, String> getCity() {
    //city.put("Dulles", "Dulles");
    //city.put("ABC", "ABC");
    return city;
}

/**
 * @param city the city to set
 */
public void setCity(HashMap<String, String> city) {
    this.city = city;
}



/**
 * @return the city
 /
public String getCity() {
    return city;
}

/**
 * @param city the city to set
 /
public void setCity(String city) {
    this.city = city;
}

/**
 * @return the city
 /
public Map<String, String> getCity() {
    return city;
}

/**
 * @param city the city to set
 /
public void setCity(Map<String, String> city) {
    this.city = city;
}
 * */
}

Struts.XML 文件如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true"/>
    <constant name="struts.configuration.xml.reload" value="true"/>
</struts>

最佳答案

据我了解,您想在渲染某处之前使用自定义类将 json 数据序列化为 xml。

所以你可以做的是,写一个序列化的 Action ,并把 Action 作为返回json格式的主 Action 的结果页面,这样json也可以用于结果 Action 。如果你想要 json 作为字符串对象,可以使用 GSON API 将任何对象、列表等转换为 json 字符串。

如果这不是您的解决方案,请发表评论。我们可以走得更远。

关于plugins - 如何在结果中使用约定插件配置我的自定义结果类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3069274/

相关文章:

wordpress - 如何在不编辑插件的情况下修改 wordpress 插件功能?

插件架构的安全/认证

java - 从 Date 对象中删除毫秒部分

jsp - Struts 2 + 禁用表单自动完成

java - 对于(嵌套)类来说,简单到什么程度算太简单?

c# - 当 TryXxxx 方法返回 false 时,out 参数的标准行为是什么?

jquery - Jquery UI 是否取代了 Jquery 中的任何功能?

jquery - 设计可折叠的响应式导航栏

jquery - JQGrid : inline editing in client side

python - Python 中的最优定向 Dijkstra 搜索