私は Json を struts 2 と jquery で使用しようとしています。ある程度までは実行できますが、過去 2 日間、いくつかの問題で立ち往生しています。これが私がこれに使用したコードと構成です
<package name="default" extends="struts-default,json-default" namespace="/">
<result-types>
<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"></result-type>
</result-types>
<action name="showUpdateAjax" class="courseCreateAction" method="showUpdateAjax">
<result type="json">
<param name="includeProperties">courseInfo, fetchedAllSubjects, selectedFetchedSubs, subjectList, fetchedAllExamType, examTypeList, selectedExmTypes, fetchedAllCourse, isUpdate, btnPressed, updateCourseId, nextCourse, previousCourse</param>
<param name="prefix">true</param>
<param name="ignoreHierarchy">false</param>
</result>
</action>
これがこの部分の私のJspコードです
<tr><td><input type="submit" value="Refresh" onclick="refreshData()" id="refreshBtn"/></td></tr>
<tr><td><s:a href="/SASIS/showUpdateAjax.action?updateCourseId=4">click Here 2</s:a></td></tr>
</table>
</center>
<script type="text/javascript">
$(document).ready(function(){
$("#refreshBtn").click(function(){
$.getJSON("/SASIS/showUpdateAjax.action?updateCourseId=4",function(data){
alert('its working '+data);
});
});
});
</script>
そして、ここに私のJavaコードがあります
public class CourseCreateAction extends ActionSupport implements ServletRequestAware{
private CourseInfo courseInfo;
private List fetchedAllSubjects;
private List<SubjectInfo> selectedFetchedSubs;
private List subjectList;
private List fetchedAllExamType;
private List examTypeList;
private List<ExamType> selectedExmTypes;
private List fetchedAllCourse;
private String nextCourse;
private String previousCourse;
private DAOInterface dao;
private String btnPressed;
private String updateCourseId;
private HttpServletRequest request;
private boolean isUpdate;
public void setServletRequest(HttpServletRequest request) {
request=request;
}
public List getFetchedAllSubjects() {
return fetchedAllSubjects;
}
public void setFetchedAllSubjects(List fetchedAllSubjects) {
this.fetchedAllSubjects = fetchedAllSubjects;
}
public List<SubjectInfo> getSelectedFetchedSubs() {
return selectedFetchedSubs;
}
public void setSelectedFetchedSubs(List<SubjectInfo> selectedFetchedSubs) {
this.selectedFetchedSubs = selectedFetchedSubs;
}
public List getSubjectList() {
return subjectList;
}
public void setSubjectList(List subjectList) {
this.subjectList = subjectList;
}
public CourseInfo getCourseInfo() {
return courseInfo;
}
public void setCourseInfo(CourseInfo courseInfo) {
this.courseInfo = courseInfo;
}
public List getFetchedAllExamType() {
return fetchedAllExamType;
}
public void setFetchedAllExamType(List fetchedAllExamType) {
this.fetchedAllExamType = fetchedAllExamType;
}
public List getExamTypeList() {
return examTypeList;
}
public void setExamTypeList(List examTypeList) {
this.examTypeList = examTypeList;
}
public List<ExamType> getSelectedExmTypes() {
return selectedExmTypes;
}
public void setSelectedExmTypes(List<ExamType> selectedExmTypes) {
this.selectedExmTypes = selectedExmTypes;
}
public List getFetchedAllCourse() {
return fetchedAllCourse;
}
public void setFetchedAllCourse(List fetchedAllCourse) {
this.fetchedAllCourse = fetchedAllCourse;
}
public String getNextCourse() {
return nextCourse;
}
public void setNextCourse(String nextCourse) {
this.nextCourse = nextCourse;
}
public String getPreviousCourse() {
return previousCourse;
}
public void setPreviousCourse(String previousCourse) {
this.previousCourse = previousCourse;
}
public DAOInterface getDao() {
return dao;
}
public void setDao(DAOInterface dao) {
this.dao = dao;
}
public String getBtnPressed() {
return btnPressed;
}
public void setBtnPressed(String btnPressed) {
this.btnPressed = btnPressed;
}
public String getUpdateCourseId() {
return updateCourseId;
}
public void setUpdateCourseId(String updateCourseId) {
this.updateCourseId = updateCourseId;
}
public boolean getIsUpdate() {
return isUpdate;
}
public void setIsUpdate(boolean isUpdate) {
this.isUpdate = isUpdate;
}
public String showUpdateAjax(){
try{
System.out.println("Ajax calling...........");
showUpdateCourse();
}catch (Exception e) {
e.printStackTrace();
}
return SUCCESS;
}
public String showUpdateCourse(){
try {
isUpdate=true;
courseInfo=dao.getCourse(Integer.parseInt(updateCourseId));
System.out.println(courseInfo.getCourseId());
selectedFetchedSubs=courseInfo.getSubjectList();
selectedExmTypes=courseInfo.getExamTypeList();
fetchedAllSubjects=dao.getAllSubjects();
fetchedAllExamType=dao.getActiveExamTypes();
for(ExamType exmType:selectedExmTypes)
for (int i = 0; i < fetchedAllExamType.size(); i++) {
if(exmType!=null&&((ExamType)fetchedAllExamType.get(i)).getExamTypeId()==exmType.getExamTypeId()){
fetchedAllExamType.remove(i);
break;
}
}
for(SubjectInfo subInfo:selectedFetchedSubs)
for (int i = 0; i < fetchedAllSubjects.size(); i++) {
if(subInfo!=null&&((SubjectInfo)fetchedAllSubjects.get(i)).getSubjectId()==subInfo.getSubjectId()){
fetchedAllSubjects.remove(i);
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return SUCCESS;
}
プロジェクトでも春と休止状態を使用しました。showUpdateAction.action を呼び出しているリンクをクリックすると、これを実行しているときに、次のような応答が得られました
{}&& {"btnPressed":null,"courseInfo":{},"examTypeList":null,"fetchedAllCourse":[],"fetchedAllExamType":[],"fetchedAllSubjects":[],"isUpdate":true,"nextCourse":null,"previousCourse":null,"selectedExmTypes":[],"selectedFetchedSubs":[],"subjectList":null,"updateCourseId":"4"}
しかし、ボタンをクリックすると何も表示されませんが、関数が呼び出されます。さらに、ここで CourseInfo は setter getter メソッドを持つ変数があるオブジェクトであり、このオブジェクトは showUpdateAction.action が呼び出されるとフル状態になりますが、出力では courseInfo が空の中括弧で囲まれていることがわかります。この問題を解決するのを手伝ってください。私はいくつかの投稿を見て、たくさんグーグルで調べました。今はお願いするしかありません。助けてください