使用json插件时带参数的Struts2注释
我只想计算我的操作的属性,并在注释中使用它的值。 下面就是我想要使用它的地方 我想在运行时定义excludeProperties参数 考虑以下当前对操作有效的注释:使用json插件时带参数的Struts2注释,struts2,annotations,struts2-json-plugin,Struts2,Annotations,Struts2 Json Plugin,我只想计算我的操作的属性,并在注释中使用它的值。 下面就是我想要使用它的地方 我想在运行时定义excludeProperties参数 考虑以下当前对操作有效的注释: @Result(name = "success", type = "json", params = {"root", "model", "excludeProperties", "myCollection"}) 在那里,actions模型有一个myCollection集合,我不想序列化它 但是,我想创建一个排除字符串(字符
@Result(name = "success", type = "json", params = {"root", "model", "excludeProperties", "myCollection"})
在那里,actions模型有一个myCollection集合,我不想序列化它
但是,我想创建一个排除字符串(字符串现在就可以了)
如果我创建一个用于排除的getter setter,我希望下面的注释能够工作(但实际上没有):
有什么想法吗
我创建了类似于此的操作,它显示了在注释中解析参数。我正在使用命名变量模式匹配器从名称空间中提取值。。。但不管我做什么,我似乎都无法设置这个参数。问题的一部分是我在处理实体对象,序列化集合是一个问题。使用您自己的自定义JSON结果类型,您可以随心所欲。自从为jsonModel创建了getter setter之后,我就在那里构建了我需要的东西。我不需要担心延迟初始化错误,因为您需要显式地使用flexjson包含集合,所以如果您只需要原语(我确实需要),那么flexjson是完美的 使用flexjson的这个非常简单的结果类型满足了我的需要:
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.Result;
import com.opensymphony.xwork2.util.ValueStack;
import flexjson.JSONSerializer;
import java.io.PrintWriter;
import org.apache.struts2.ServletActionContext;
public class Kjson implements Result {
@Override
public void execute(ActionInvocation invocation) throws Exception {
ServletActionContext.getResponse().setContentType("text/plain");
PrintWriter responseStream = ServletActionContext.getResponse().getWriter();
ValueStack valueStack = invocation.getStack();
Object jsonModel = valueStack.findValue("jsonModel");
//create json and put it into response stream
responseStream.println(new JSONSerializer().exclude("class").serialize(jsonModel));
}
}
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.Result;
import com.opensymphony.xwork2.util.ValueStack;
import flexjson.JSONSerializer;
import java.io.PrintWriter;
import org.apache.struts2.ServletActionContext;
public class Kjson implements Result {
@Override
public void execute(ActionInvocation invocation) throws Exception {
ServletActionContext.getResponse().setContentType("text/plain");
PrintWriter responseStream = ServletActionContext.getResponse().getWriter();
ValueStack valueStack = invocation.getStack();
Object jsonModel = valueStack.findValue("jsonModel");
//create json and put it into response stream
responseStream.println(new JSONSerializer().exclude("class").serialize(jsonModel));
}
}