3

I have a Struts2 Action Class configured via annotations. All of the "normal" methods that are annotated with @Action work fine.

However, I need to add a method into the action that returns JSON.

Here is a trimmed down version of my class (dao autowired with Spring):

@Namespace("featureClass")
// define success and input actions for class here
public class FeatureClassAction extends ActionSupport {

    FeatureClassDao featureClassDao;

    @Autowired
    public setFeatureClassDao(FeatureClassDeao featureClassDao) {
        this.featureClassDao = featureClassDao;
    }

    List<FeatureClass> featureClasses;

    // snip normal actions

    @Action("/featureClassesJSON")
    @JSON
    public String getFeatureClassesJSON() throws Exception {

        featureClasses = featureClassDao.getAll();
        return SUCCESS;
    }
}

Can anyone assist? If I have to go the struts.xml route, that means moving all of my other actions (which work fine) into it.

1 Answer 1

8

I figured I would share the answer, since anyone else with the same problem would likely also face the silence.

I created two actions: FeatureClassAction and FeatureClassJsonAction. FeatureClassAction was annotated as such:

@ParentPackage("struts-default")
@Namespace("/featureClass")
public class FeatureClassAction extends ActionSupport {

FeatureClassJsonAction is annotated like this:

@ParentPackage("json-default")
@Namespace("/featureClass")
public class FeatureClassJsonAction extends ActionSupport {

The method in the JSON Action was annotated like this:

@Action(value="featureClassesJson", results = {
    @Result(name="success", type="json")
})

public String getFeatureClassesJSON() throws Exception {

Hope it helps someone.

Sign up to request clarification or add additional context in comments.

2 Comments

Can you please let me know what URL you used to access the action? No matter what I tried it just gives me "There is no Action mapped for namespace [/featureClass] and action name [featureClassesJson] associated with context path [/myapp]." I tried localhost:8080/myapp/featureClass/featureClassesJson.action and .do and lots of other permutations.
The url should be featureClass/featureClassesJson.action

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.