我试图了解如何使用JSON,在此过程中,我试图从Struts2操作获取JSON响应并显示响应警报。为此,我在JavaScript中使用Ajax POST,如下所示:
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"); } }); }
我的动作类包含一个String字段,我试图将其作为JSON响应显示在警报中。但是我发现这样做有问题。我想念什么或做错什么?
String
我的动作课如下:
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 文件:
struts.xml
<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>
我的pom.xml:
pom.xml
<dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-json-plugin</artifactId> <version>2.3.15</version> </dependency>
我通过在json结果上添加myMsg解决了我的问题。谢谢你的帮助