Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

java - How to send JSON data via POST (Ajax) and receive JSON response from Struts 2 action

I am trying to understand how to use JSON and in the process I'm trying to get a JSON response from Struts2 action and display an alert for the response. For this I'm using Ajax POST in JavaScript as follows:

function checkButtonClick(id){  

        var btnSave = 'saveAttendees';  
            var atNameList = $('#attName'+id).val();
            var ptNameList = $('#postName'+id).val();
            var aId = $('#at_id'+id).val();
            
            alert("here");
            var arr = {buttonName:  btnSave,
                    attendeesNameList: atNameList,
                    attendeesPostList: ptNameList,              
                    hidden_At_id: aId
                    };
            $.ajax({                            
                data: arr,
                type: 'POST',
                dataType: 'json',               
                url:"meeting_record_form",
                
                success:function(result){
                    alert(result.myMsg);
              },
                error:function(result){
                    alert("error");
              }
            });
}

My Action class contains a String field that I'm trying to display in alert as JSON response. But I'm finding problem doing this. What am I missing or doing wrong?

My action class is as follows:

private String myMsg;

    public String getMyMsg() {
        return myMsg;
    }

    public void setMyMsg(String myMsg) {
        this.myMsg = myMsg;
    }

private String updateAttendeesRecord() {
        
        
        meetingRecordService.updateAttendeesRecord(attendeesListMethod(), meeting_record);
        setMyMsg("Update Successful!");
            return SUCCESS;
    }

struts.xml file:

 <package name="default" extends="struts-default, json-default">
    <result-types>
      <result-type name="json" class="org.apache.struts2.json.JSONResult" />
    </result-types>
    <interceptors>
      <interceptor name="json" class="org.apache.struts2.json.JSONInterceptor" />
    </interceptors>
    
    <action name="meeting_record_form" class="com.task.action.MeetingRecordAction" method="updateAttendeesRecord">
     <result name="success" type="json" />
    </action>
</package>

My pom.xml:

<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-json-plugin</artifactId>
    <version>2.3.15</version>
</dependency>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In the action configuration you should not override the default configuration from json-default package just extend it and all. Also json-default extend a struts-default, so multiple inheritance is not required.

<package name="default" extends="json-default">
  <action name="meeting_record_form" class="com.task.action.MeetingRecordAction" method="updateAttendeesRecord">
    <result name="success" type="json" />
  </action>
</package>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...