1

我正在尝试将 Json 与 struts 2 和 jquery 一起使用,我能够做到这一点,但过去两天我一直遇到一些问题。这是我用于此的代码和配置

<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 时该对象处于状态满状态,但在输出中我们可以看到 courseInfo 带有空花括号。请帮助解决这个问题我已经浏览了几篇文章并在谷歌上搜索了很多。现在除了问我别无选择。请帮忙

4

3 回答 3

1

作为点击 refrehButton 的启动器,为什么要调用 refreshData javascript 方法?

onclick="refreshData()"

因为您已经为此声明了 jQuery 操作

$("#refreshBtn").click(function(), 对于同一事件没有两个侦听器onclick="refreshData(), 如果需要refreshData从 jQuery 侦听器调用, 请删除, 如:

    $(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-defaultjson-defaultstruts-default

而不是extends="struts-default,json-default"使用这个extends="json-default"

第三还要检查您是否真的从数据库中检索结果

courseInfo=dao.getCourse(Integer.parseInt(updateCourseId)); // Check if your object has field values

您能否做这些更改以发布您的发现

于 2012-05-23T04:46:05.103 回答
1

我看了你的代码(不了解struts),我注意到,

<action name="showUpdateAjax" class="courseCreateAction" method="showUpdateAjax">

你的ajax调用是为了,

"/SASIS/showUpdateAjax.action?updateCourseId=4"

我相信您的控制器是“courseCreateAction”,动作是“showUpdateAjax”,典型的 GET url 看起来像/controller/action?param1=value1&param2=value2...

但是您的网址不包含控制器。

处理此请求的操作,

public String showUpdateAjax(){
  try{
  System.out.println("Ajax calling...........");
  showUpdateCourse();
  }catch (Exception e) {
      e.printStackTrace();
  }
  return SUCCESS;
}

courseId 在哪里传递给 showUpdateAjax() ?

最后,就像 mprabhat 提到的那样,您已经绑定了按钮以在单击时触发 ajax 调用。所以没有必要有一个 onclick="refreshData()"

于 2012-05-23T05:04:54.467 回答
1

感谢您的答复 mprabhat 先生和 Rahul 先生。我使用 firebug 来调试这个问题,因为 Rahul 告诉我为此目的使用 webinspector,因为我使用的是 firefox,所以我更喜欢 firebug。而且我确实得到了解决 mprabhat 建议的 jquery 问题的帮助。

现在,实际问题出在 struts-jquery 插件和 spring 上。在我的“dao”代码中,对象是通过弹簧注入的。当 json 插件试图序列化对象时,它创建了一个具有公共访问修饰符的数据库对象的方法。这以某种方式造成了所有问题。includeProperties我在尝试返回没有“ ”参数的 json 时遇到如下异常

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>

问题就解决了。

于 2012-05-23T20:40:53.607 回答