Struts2 JSON plugin with annotations

I have an Action Struts2 class configured with annotations. All the “regular” methods that are annotated using @Action work fine.

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

Here is a stripped 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 help? If I need to go along the struts.xml path, that means moving all my other actions (which work fine).

+3
source share
1 answer

I decided that I would share the answer, since anyone else with the same problem is likely to also be silent.

: FeatureClassAction FeatureClassJsonAction. FeatureClassAction :

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

FeatureClassJsonAction :

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

JSON Action :

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

public String getFeatureClassesJSON() throws Exception {

, -.

+7

Source: https://habr.com/ru/post/1765330/