Welcome to OGeek Q&A 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 Reply

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...