带注释的Struts2 JSON插件 [英] Struts2 JSON Plugin With Annotations

查看:63
本文介绍了带注释的Struts2 JSON插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过注释配置的Struts2动作类.使用@Action注释的所有常规"方法都可以正常工作.

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

但是,我需要在返回JSON的操作中添加一个方法.

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

这是我的课程的精简版(与Spring自动连线的dao):

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;
    }
}

有人可以协助吗?如果我必须走struts.xml路线,则意味着将我所有其他动作(可以正常运行)移入其中.

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.

推荐答案

我认为我会分享答案,因为其他遇到相同问题的人也可能会面临沉默.

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

我创建了两个动作:FeatureClassAction和FeatureClassJsonAction. FeatureClassAction的注释如下:

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

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

FeatureClassJsonAction的注释如下:

FeatureClassJsonAction is annotated like this:

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

JSON Action中的方法是这样标注的:

The method in the JSON Action was annotated like this:

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

public String getFeatureClassesJSON() throws Exception {

希望它对某人有帮助.

这篇关于带注释的Struts2 JSON插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆