我试图使用带有struts 2和jquery的Json,我能够达到一个级别,但是我在过去的两天里遇到了一些问题。以下是我用于此
的代码和配置<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;
}
我在项目中也使用了spring和hibernate。当我点击调用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时此对象为state full,但在输出中我们可以看到courseInfo带有空花括号。请帮助解决这个问题我经历过几个帖子并且google了很多。现在除了询问我别无选择。请帮忙
答案 0 :(得分:1)
作为点击refrehButton的首发,为什么要调用refreshData javascript方法?
onclick="refreshData()"
因为您已经为该
声明了jQuery操作 $("#refreshBtn").click(function()
,对于同一事件没有两个侦听器,请删除onclick="refreshData()
,如果需要,请从jQuery侦听器调用refreshData
,如:
$(document).ready(function(){
$("#refreshBtn").click(function(){
//refreshData(); call this method here if you need to call
$.getJSON("/SASIS/showUpdateAjax.action?updateCourseId=4",
function(data){
alert('its working '+data);
}, function(error) {
alert('Error here');
});
});
});
如果您的包裹延长json-default
,则不需要struts-default
json-default
延长struts-default
而不是extends="struts-default,json-default"
使用此extends="json-default"
3同时检查您是否实际从数据库中检索结果
courseInfo=dao.getCourse(Integer.parseInt(updateCourseId)); // Check if your object has field values
你能否在发表你的发现后做这些改变
答案 1 :(得分:1)
我看了你的代码(没有关于struts的知识),我注意到了,
<action name="showUpdateAjax" class="courseCreateAction" method="showUpdateAjax">
你的ajax电话是,
"/SASIS/showUpdateAjax.action?updateCourseId=4"
我相信您的控制器是'courseCreateAction',操作是'showUpdateAjax',典型的GET网址看起来像/controller/action?param1=value1¶m2=value2...
但您的网址不包含控制器。
处理此请求的操作,
public String showUpdateAjax(){
try{
System.out.println("Ajax calling...........");
showUpdateCourse();
}catch (Exception e) {
e.printStackTrace();
}
return SUCCESS;
}
courseId传递给showUpdateAjax()的位置在哪里?
最后,就像上面提到的mprabhat一样,你已经绑定了按钮来点击一个ajax调用。所以不需要 onclick="refreshData()"
答案 2 :(得分:1)
感谢您对mprabhat先生和Rahul先生的回复。我使用firebug来调试这个问题,因为Rahul告诉我使用webinspector用于此目的,因为我使用firefox我更喜欢firebug。我确实得到了帮助来解决mprabhat建议的jquery问题。
现在,实际的问题是使用struts-jquery插件和spring。在我的'dao'代码中,对象通过spring注入。当json插件试图序列化对象时,它创建了一个具有公共访问修饰符的数据库对象的方法。以某种方式创造了所有问题。我尝试在没有'includeProperties
'param
org.apache.struts2.json.JSONException:
org.apache.struts2.json.JSONException:
org.apache.struts2.json.JSONException:
org.apache.struts2.json.JSONException: java.lang.IllegalAccessException:
Class org.apache.struts2.json.JSONWriter can not access a member of class
org.hibernate.persister.entity.AbstractEntityPersister$3 with
modifiers "public"
org.apache.struts2.json.JSONWriter.bean(JSONWriter.java:230)
org.apache.struts2.json.JSONWriter.process(JSONWriter.java:160)
org.apache.struts2.json.JSONWriter.value(JSONWriter.java:126)
因此,我决定在struts配置中排除属性'dao',而不是包含其余的属性。
<action name="showUpdateAjax" class="courseCreateAction" method="showUpdateAjax">
<result type="json">
<param name="excludeProperties">dao</param>
<param name="ignoreHierarchy">false</param>
<param name="excludeNullProperties">true</param>
</result>
</action>
问题解决了。