Domanda

Simply I want to evaluate a property of my action and use it's value within an annotation. The following is exactly where I want to use it.

I want to define a excludeProperties parameter at run time.

Consider the following annotation which currently works on the action:

    @Result(name = "success", type = "json", params = {"root", "model", "excludeProperties", "myCollection"})

There the actions model has a myCollection collection which I do not want serialized.

However I would like to create an exclusion String (a string will do for now).

If I create a getter setter for exclusion, I would expect the following annotation to work (which does not):

@Result(name = "success", type = "json", params = {"root", "model", "excludeProperties", "${exclusion}"})

Any ideas?

I have created actions similar to this answer which shows resolving a parameter within an annotation. I am using the named variable pattern matcher to extract values from the namespace... but I just can't seem to set this parameter no matter what I do.

È stato utile?

Soluzione

Part of the issue was that I was working with entity objects and serializing collections was an issue. With your own custom JSON result type you can do what ever you want. Since created getter setter for jsonModel, I just constructed what I needed there. I don't need to worry about lazy initialization errors because you need to explicitly include collections with flexjson so if you just want the primitives (which I did) then flexjson is perfect.

This very simple result type using flexjson which worked for my needs:

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));
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top