Home > Blockchain >  Post json to struts 2 action
Post json to struts 2 action

Time:11-27

I need to post a simple json object to a Struts 2 action, could you tell me what I miss with this:

the Java object to save:

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Integer id;
@Column(name = "code")
private String code;
@Column(name = "libelle_fr")
private String libelleFr;
@Column(name = "libelle_nl")
private String libelleNl;

I use alpine.js but it's a detail, the script called to send the request is this one:

<script>
    function postForm() {
        return {
            indicateurDictionnaireForm: {
                libelleFr: '',
                libelleNl: '',
                code: ''
            },
            message: '',

            submitData() {
                this.message = ''

                fetch('<%= request.getContextPath() %>/mesures.ce.save.action', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    dataType:"json",
                    body: JSON.stringify(this.indicateurDictionnaireForm)
                })
                    .then(() => {
                        this.message = 'Form sucessfully submitted!'
                    })
                    .catch(() => {
                        this.message = 'Ooops! Something went wrong!'
                    })
            }
        }
    }
</script>

the json sent to the action is:

{
"libelleFr":"fr",
"libelleNl":"nl",
"code":"sample"
}

from my action file there is my method called from the front:

private IndicateurDictionnaire indicateurDictionnaireForm;

// Action to save an indicateurDictionnaireto database
@Action(value = "mesures.indicateurs.ce.save", results = {
            @Result(name = "success", type = "json", params = {"root", "indicateurDictionnaireForm"}),
            @Result(name = "input", type = "tiles", params = {"root", "indicateurDictionnaireForm"}, location = "viewMesureCoutUnitaire")})
    public String save(IndicateurDictionnaire indicateurDictionnaire, String libelleFr, String libelleNl, String code) {
        dictionnaireIndicateurBo.saveIndicateurDictionnaire(indicateurDictionnaire);
        return SUCCESS;
    }

According the struts2 json pluggin, the json should be mapped to my object if it's properly formatted but the fields are empty if I look in debug.

Do you know how I can proceed to at least see the json request in my action method?

CodePudding user response:

The methods that are mapped to the actions not use any parameters in the method signature. So you need to remove those parameters and add json interceptor to the action config.

This field is used as root object by the json interceptor and should not be null.

IndicateurDictionnaire indicateurDictionnaire;
// getter and setter should be added

@Action(value = "mesures.indicateurs.ce.save", results = {
    @Result(name = "success", type = "json", params = {"root", "indicateurDictionnaire"})
}, interceptorRefs = @InterceptorRef(value = "json", params = {"root", "indicateurDictionnaire"}))
public String save() {       
  dictionnaireIndicateurBo.saveIndicateurDictionnaire(indicateurDictionnaire);
  return SUCCESS;
}
  • Related