2

我正在处理 ajax 请求/响应以更新表格,

但是当 Ajax 调用控制器时,我得到了整个页面的响应。但我想要的只是精确的表数据,我需要用我的 c:forEach 表进行映射。谢谢。

jsp视图:

      <script type="text/javascript">
       jQuery(document).ready(function(){
           function doAjaxPost() {
                // get the form values
    var contextPath ='<jsp:expression>contextPath</jsp:expression>';

                $.ajax({
                    type: "GET",
                    url: contextPath+"/noticesAjaxRequest",
                    dataType: "json",
                    contentType: 'application/json',
                    success: function(data){
                        // we have the response
                        $('#info').empty().html(data);

                    },

                    });

                }
          setInterval(doAjaxPost,10*1000);
       });

    </script>  

       <div id="info">
        <c:forEach  items="${noticeForm.noticeList}" var="notice">
            <c:out value="${notice.coreValue} "/>
            <c:out value="${notice.description} "/>
            <br/>
        </c:forEach>
   </div> 

控制器:

@Controller
public class DashboardController {

private NoticeBO noticeBO;

/*@RequestMapping("/dashboardTest")
public String printWelcome(ModelMap model) {
    List<Employee> employeeList=dashboardDAO.getAllEmployee();
    for(Employee employee:employeeList)
    model.addAttribute("msg", model.get("msg")+"<br/> Spring 3 MVC Hello World"+employee.getCustomerId());

    return "DashboardTest";

}*/

public NoticeBO getNoticeBO() {
    return noticeBO;
}

public void setNoticeBO(NoticeBO noticeBO) {
    this.noticeBO = noticeBO;
}

@RequestMapping("/dashboard")
public String dashboard(ModelMap model) {
    return "Dashboard";
}

@RequestMapping("/notices")
public ModelAndView notices(@ModelAttribute("NoticeForm") NoticeForm noticeForm, ModelMap model) {

    noticeBO.prepareNoticeList(noticeForm,model);

    return new ModelAndView("notices","noticeForm",noticeForm);
}

@RequestMapping("/noticesAjaxRequest")
public ModelAndView noticesAjaxRequest(@ModelAttribute("NoticeForm") NoticeForm noticeForm, ModelMap model) {

    noticeBO.prepareNoticeList(noticeForm,model);

    return new ModelAndView("notices", "noticeForm", noticeForm);
}
}

更新{1}:

我尝试更改控制器并开始给我错误:[object XMLHttpRequest]

 @RequestMapping("/noticesAjaxRequest")
public @ResponseBody List<Notice> noticesAjaxRequest(@ModelAttribute("NoticeForm") NoticeForm noticeForm, ModelMap model) {

    noticeBO.prepareNoticeList(noticeForm,model);

    return noticeForm.getNoticeList();
}

错误描述:

HTTP 状态 406 - 此请求标识的资源仅能够生成具有根据请求“接受”标头不可接受的特征的响应。

响应标头

Content-Length  1067
Content-Type    text/html;charset=utf-8
Date    Thu, 11 Jul 2013 12:48:19 GMT
Server  Apache-Coyote/1.1

请求标头

Accept  application/json, text/javascript, */*
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Cookie  JSESSIONID=D54D66F6B7FE05C2B6FB684BF19387F1
Host    localhost:8080
Referer http://localhost:8080/vServFinance/notices
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0
X-Requested-With    XMLHttpRequest
4

3 回答 3

5

感谢朋友们的帮助。最好最简单的一种方法是创建单独的 jsp 视图,并在从控制器返回/响应时包含该视图。

jQuery(document).ready(function(){
           function doAjaxPost() {
                // get the form values
               var contextPath ='<jsp:expression>contextPath</jsp:expression>';

                $.ajax({
                    type: "GET",
                    url: contextPath+"/noticesAjaxRequest",
                    success: function(data){
                        // we have the response

                         $('#info').html(data); 
                        /* $('#info').refresh(); */
                    },

                     error: function(e){
                        alert('Error: ' + e);
                    } 
                    });
                }
          setInterval(doAjaxPost,10*1000);
       });

控制器:

@RequestMapping(value="/noticesAjaxRequest")
public  ModelAndView noticesAjaxRequest(@ModelAttribute("NoticeForm") NoticeForm noticeForm, ModelMap model) {

    noticeBO.prepareNoticeList(noticeForm,model);

    return new ModelAndView("/noticesList","noticeForm",noticeForm);
}
于 2013-07-12T06:13:53.377 回答
1

在您的代码中,您将返回一个 ModelAndView,这将返回一个视图。如果要返回 JSON 数据,则需要将 @ResponseBody 添加到返回类型。像那样:

@RequestMapping(value = "/porUF", method = RequestMethod.GET)
    public @ResponseBody List<Municipio> municipios(
            @RequestParam(value = "uf", required = true) String uf) {

        if ( uf.length() != 2) {
            return null;    
        }

        return municipioBO.findByUf(uf);
    }   

这会将 LIST 转换为 JSON 对象。HTTP 响应将如下所示:

[{"codigo":9701,"uf":{"uf":"DF","nome":"DISTRITO FEDERAL"},"nome":"BRASILIA"}]

为此,您需要一些库来执行将对象转换为 JSON 对象的魔术。我正在使用杰克逊。将其添加到您的 POM:

<!-- Data Mapper package is a high-performance data binding package built 
        on Jackson JSON processor -->
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
    </dependency>
于 2013-07-11T16:06:03.407 回答
1

HTTP 406 可能是由于类路径中没有jackson-core-asljackson-mapper-asljar 引起的。请参见此处和此处的 Stack Overflow 帖子。

总而言之,如果您使用的是 Maven,您需要将以下两个依赖项添加到您的 pom.xml

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-lgpl</artifactId>
    <version>1.9.12</version>
</dependency>
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-lgpl</artifactId>
    <version>1.9.12</version>
</dependency>

如果不使用 Maven,只需确保上面的两个 jar 都存在于类路径中。

于 2013-07-11T14:20:48.793 回答