结果类型JSON在Struts中使用AJAX的jQuery [英] Result type JSON in Struts with AJAX jQuery

查看:119
本文介绍了结果类型JSON在Struts中使用AJAX的jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立使用Struts和jQuery的Web应用程序。

在下拉列表的变化,我需要从数据库中获取的细节。在我的struts.xml的配置,我已经定义的方法和措施如下:

 <结果名称=addressChange>
    < PARAM NAME =根> requestBean< /参数>
< /结果>
 

在我执行的应用使得在所有的各个地方发生变化后,该请求被传递到Action类和DAO方法和控制返回成功筛选。但返回的数据是无法在屏幕上,我得到了一个错误信息:

  

链接... 404未找​​到(匿名函数)阿贾克斯jQuery的。

因为我有一些其他的Ajax调用,我比他们发现,结果类型是在struts.xml中失踪,后来我改变了它同样。现在,它的工作的罚款。

 <结果名称=addressForBrigadeTYPE =json的>
    < PARAM NAME =根> requestBean< /参数>
< /结果>
 

问题是:

  1. 为什么支柱将其识别为JSON对象,它是如何覆盖它的POJO?
  2. 当我们要使用的结果类型为JSON在Struts中?
  3. 我们可以使用其他的结果类型?
  4. 如果我们需要使用的结果类型为JSON每当有一个AJAX调用?
解决方案

看来你已经回答了你自己,你加入的时候第一个问题 TYPE =json的的结果。没有它的调度员类型被隐式地使用。您应该使用 JSON 的结果类型,如果你想返回JSON对象的响应。您可以使用其他的结果类型,这取决于你怎么想返回的响应。这是有道理的指定 内容类型当你的数据格式,以响应头。你并不需要指定 JSON 结果类型结果不返回JSON对象,使用其他结果类型,但您应使用的dataType 物业 jQuery的阿贾克斯指定你希望回去的数据类型从服务器。每个结果类型是由配置定义,并拾起调度时,它的执行。如果未指定结果类型,调度员结果类型使用它调用一个请求分发转发到JSP渲染它的输出响应。它只是产生一个HTML的内容,你可以在浏览器源代码窗口中看到。

I am trying to build a web application using struts and jquery.

On change of a dropdown list, I need to fetch the details from the database. In my struts.xml configuration, I have defined the method and action as follows:

<result name="addressChange">
    <param name="root">requestBean</param>
</result>

When I executed the application after making a change in all the respective places, the request was passed on to the Action class and DAO methods and the control is returned to screen successfully. But the data returned was not available on the screen and I was getting an error message that:

the url ... 404 not found (anonymous function) ajax in jquery.

Since I had some other ajax call, I compared them and found out that the result type is missing in the struts.xml and then I changed it likewise. Its working fine now.

<result name="addressForBrigade" type="json">
    <param name="root">requestBean</param>
</result>

Questions are:

  1. How come struts identify it as json object and how does it cover it to the pojo?
  2. When should we use the result type as json in Struts?
  3. Can we use other result types?
  4. Should we need to use the result type as json whenever there is an AJAX call?

解决方案

First question seems you have answered yourself, when you added type="json" to the result. Without it the "dispatcher" type is used implicitly. You should use json result type if you want to return JSON object with a response. You can use other result types, it depends what do you want to return with the response. It makes sense to specify Content-Type header when formatting your data to the response. You don't need to specify json result type to results that don't return JSON object, use other result types, but you should use dataType property in jQuery Ajax to specify a data type you're expecting back from the server. Each result type is defined by the configuration and picked up by the dispatcher when it's executed. If you didn't specify the result type, the "dispatcher" result type is used which invokes a request dispatcher to forward to the JSP for rendering it's output to the response. It merely produces a HTML content that you can see in the browser source window.

这篇关于结果类型JSON在Struts中使用AJAX的jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆