Plugins 如何在结果中使用约定插件配置自定义结果类型

Plugins 如何在结果中使用约定插件配置自定义结果类型,plugins,struts2,conventions,Plugins,Struts2,Conventions,我已经创建了自定义结果类来将json数据序列化为xml。我想通过约定插件将此结果类配置为某些特定操作的结果类型。 但它在启动容器时给出了错误。下面给出了我的代码和错误 我的自定义结果类: package actions; import example.*; import java.io.PrintWriter; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionInvoc

我已经创建了自定义结果类来将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;
}
 * */
}
包动作;
导入com.opensymphony.xwork2.ActionSupport;
导入java.util.HashMap;
导入java.util.Map;
导入org.apache.struts2.convention.annotation.Action;
导入org.apache.struts2.convention.annotation.Result;
导入org.apache.struts2.convention.annotation.Namespace;
导入org.apache.struts2.convention.annotation.ParentPackage;
导入org.apache.struts2.interceptor.ParameterAware;
@父包(“操作”)
@名称空间(“/actions”)
公共类ZipDataSupplier扩展ActionSupport实现Parameterware
{
私有映射参数;
私有对象jsonModel;
公共映射getParameters()
{
返回此参数;
}
公共void setParameters(映射参数)
{
这个参数=参数;
}
公共对象getJsonModel()
{
返回this.jsonModel;
}
公共void setJsonModel(对象jsonModel)
{
this.jsonModel=jsonModel;
}
@操作(value=“/getZipData”,results={@Result(name=“success”,location=“ajaxCall”,**type=“actions.JSONResult”)**)
公共字符串getZipData()
{
System.out.println(“内部getZipData……”);
Map Map=getParameters();
System.out.println(“参数映射=“+map”);
字符串htmlIds=((字符串[])map.get(“htmlIds”)[0];
System.out.println(“htmlIds=“+htmlIds”);
字符串jsonIds=((字符串[])map.get(“jsonIds”)[0];
System.out.println(“jsonIds=“+jsonIds”);
ZipData ZipData=新ZipData();
zipData.getCity().put(“Dulles”,“Dulles”);
zipData.getCity().put(“纽约”、“纽约”);
setJsonModel(zipData);
回归成功;
}
}
类ZipData
{
私有字符串zipCode=“20101”;
私有字符串stateCode=“VA”;
私有字符串stateName=“Virginia”;
私有HashMap city=新HashMap();
//私有JSONObject city=null;//JSONArray.fromObject(getCityMap());
/**
*@返回zipCode
*/
公共字符串getZipCode(){
返回zipCode;
}
/**
*@param zipCode要设置的zipCode
*/
public void setZipCode(字符串zipCode){
this.zipCode=zipCode;
}
/**
*@返回状态码
*/
公共字符串getStateCode(){
返回状态码;
}
/**
*@param stateCode要设置的stateCode
*/
public void setStateCode(字符串stateCode){
this.stateCode=stateCode;
}
/**
*@返回stateName
*/
公共字符串getStateName(){
返回stateName;
}
/**
*@param stateName要设置的stateName
*/
public void setStateName(字符串stateName){
this.stateName=stateName;
}
/**
*@返回城市
/
公共JSONObject getCity(){
this.city=JSONObject.fromObject(getCityMap());
返回这个城市;
}
/**
*@param city要设置的城市
/
公共城市(JSONObject城市){
this.city=城市;
}
/**
*@返回城市地图
*/
公共HashMap getCity(){
//城市。put(“杜勒斯”、“杜勒斯”);
//市售债券(“ABC”、“ABC”);
回归城市;
}
/**
*@param city要设置的城市
*/
公共城市(HashMap城市){
this.city=城市;
}
/**
*@返回城市
/
公共字符串getCity(){
回归城市;
}
/**
*@param city要设置的城市
/
公共城市(字符串城市){
this.city=城市;
}
/**
*@返回城市
/
公共地图getCity(){
回归城市;
}
/**
*@param city要设置的城市
/
公共城市(地图城市){
this.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

因此,您可以做的是,编写一个序列化操作,并将该操作作为返回json格式的主操作的结果页,以便json也可用于结果操作。如果您希望将json作为字符串对象,则可以使用GSON API将任何对象、列表等转换为json字符串


如果这不是您的解决方案,请发表评论。我们可以进一步行动。

请看一下这一点并重新编排您的问题:非常感谢您宝贵的答复。当然,我会尽力做到你上面所说的,并向你通报最新情况。再次感谢你的帮助。