Java 使用Hibernate在ArrayList中实现Ajax响应

Java 使用Hibernate在ArrayList中实现Ajax响应,java,jquery,ajax,hibernate,arraylist,Java,Jquery,Ajax,Hibernate,Arraylist,我正在从事一个项目,其中客户需求是…如果用户从下拉菜单中选择一种特定类型,那么数据应该显示在下一个下拉菜单上,就像有3个列表(pricelist、slablist、zonelist)。若用户选择价目表,则下一个下拉菜单上只应显示价目表数据。 我几乎到达了那里,但我得到了ArrayList格式的数据(响应) 我不知道下一步该怎么办。我的意思是如何将响应数据放在下拉菜单中 这是我的密码: PriceListMapping.jsp <select name="pricelisttype" id=

我正在从事一个项目,其中客户需求是
…如果用户从下拉菜单中选择一种特定类型,那么数据应该显示在下一个下拉菜单上,就像有3个列表(pricelist、slablist、zonelist)
。若用户选择价目表,则下一个下拉菜单上只应显示价目表数据。 我几乎到达了那里,但我得到了ArrayList格式的
数据(响应)
我不知道下一步该怎么办。我的意思是如何将响应数据放在下拉菜单中

这是我的密码:

PriceListMapping.jsp

<select name="pricelisttype" id="pricelisttype" onchange="getPriceListType()">
   <option value="" >Select PriceList Type </option>
   <option value="G" >General Price List</option>
   <option value="S" >Slab Price List</option>
   <option value="Z" >Zone Price List</option>
</select>

<select name="pricinglist" id="pricinglist">
   <option value='0' >Select Price List </option>
</select>
PriceListMappingView.java

String pricelisttype = request.getParameter("pricelisttype");
if (pricelisttype.equalsIgnoreCase("G"))
{
  transaction.setBiginTransaction();
  List<Object[]> l = transaction.s.createQuery("select generalPricingIds from Pricelistmaster").list();
  transaction.setCommitTransaction();
  response.getWriter().print(l);
}
String pricelisttype=request.getParameter(“pricelisttype”);
if(pricelisttype.equalsIgnoreCase(“G”))
{
transaction.setBiginTransaction();
List l=transaction.s.createQuery(“从Pricelistmaster中选择GeneralPricingId”).List();
transaction.setCommitTransaction();
response.getWriter().print(l);
}


在您的特定情况下,我建议您避免尝试使用Javascript在网页中过滤价目表,而是在控制器中影响它

@RequestMapping("/pricelists")
public @ResponseBody getPriceLists(@RequestParam("type") String type) {
  if ( "G".equals( type ) ) {
    // ignore this type, return an empty list perhaps??
    return Collections.emptyList();
  }
  else {
    // query the database doing something like
    // SELECT * FROM PriceListTable WHERE PriceListType = :priceListType
    return priceLists.getPriceListsByType( type );
  }
}
在页面中,您需要关心的就是获取json数组并将其附加到
html标记中

function(data) {
  var toAppend = '';
  $.each (data, function( i, o ) ) {
    toAppend = '<option value=' + o.value + '>' + o.name + '</option>';
  } );
  $( "pricingList" )
   .find("option")
   .remove()
   .end()
   .append( toAppend );
}
功能(数据){
var toAppend='';
$。每个(数据、函数(i、o)){
toAppend=''+o.name+'';
} );
$(“pricingList”)
.查找(“选项”)
.删除()
(完)
.附加(to append);
}

对于使用服务器端标记而不是依赖JavaScript来渲染组件的逻辑的情况,我建议您考虑使用HTML部分。原始页面仍然发出AJAX请求,但与控制器返回JSON不同,它会像原始页面一样转发到视图页面。AJAX请求需要

html
而不是
json
的响应,并且可以简单地替换dom元素。

在您的特定情况下,我建议您避免尝试使用Javascript在网页中过滤价目表,而是在控制器中影响它

@RequestMapping("/pricelists")
public @ResponseBody getPriceLists(@RequestParam("type") String type) {
  if ( "G".equals( type ) ) {
    // ignore this type, return an empty list perhaps??
    return Collections.emptyList();
  }
  else {
    // query the database doing something like
    // SELECT * FROM PriceListTable WHERE PriceListType = :priceListType
    return priceLists.getPriceListsByType( type );
  }
}
在页面中,您需要关心的就是获取json数组并将其附加到
html标记中

function(data) {
  var toAppend = '';
  $.each (data, function( i, o ) ) {
    toAppend = '<option value=' + o.value + '>' + o.name + '</option>';
  } );
  $( "pricingList" )
   .find("option")
   .remove()
   .end()
   .append( toAppend );
}
功能(数据){
var toAppend='';
$。每个(数据、函数(i、o)){
toAppend=''+o.name+'';
} );
$(“pricingList”)
.查找(“选项”)
.删除()
(完)
.附加(to append);
}

对于使用服务器端标记而不是依赖JavaScript来渲染组件的逻辑的情况,我建议您考虑使用HTML部分。原始页面仍然发出AJAX请求,但与控制器返回JSON不同,它会像原始页面一样转发到视图页面。AJAX请求需要的响应是

html
而不是
json
,并且可以简单地替换dom元素。

使用javascript进行循环itreator或jQuery.each()并在循环中迭代。如何做到这一点…它与java类似,仅用于(i=0;ii也使用javascript进行循环,但得到响应。成功:函数(数据){var i;for(i=0;i为循环itreator或jQuery.each()使用javascript,并在循环中迭代。如何执行此操作…它与java类似,仅用于(i=0;ii也为循环使用javascript,但得到响应。成功:函数(数据){var i;for(i=0;i//我的工作代码函数getPriceListType(){var pricelisttype=$('#pricelisttype').val();$.ajax({url:“getPriceListType”,方法:“GET”,数据:{pricelisttype:pricelisttype},数据类型:'json',成功:函数(数据){var select=$('#pricinglist');select.find('option').remove();$.each(数据、函数(索引、值){$).val(值[0]).text(值[1]).appendTo(选择);}}}PriceListMappingView.java
String pricelisttype=request.getParameter(“pricelisttype”);if(pricelisttype.equalsIgnoreCase(“G”){transaction.setBiginTransaction();List l=transaction.s.createQuery(“从Pricelistmaster中选择GeneralPricingId”).List();transaction.setCommitTransaction();字符串json=new Gson().toJson(l);response.getWriter().write(json);}
并添加gson.xxxx.jar库…我希望能为其他开发人员提供帮助..../My的工作代码函数getPriceListType(){var pricelisttype=$('#pricelisttype').val();$.ajax({url:“getPriceListType”,方法:“GET”,数据:{pricelisttype:pricelisttype},数据类型:'json',成功:函数(数据){var select=$('#pricinglist');select.find('option').remove();$.each(数据,函数(索引,值){$('.val(值[0])。文本(值[1])。appendTo(选择);}}PriceListMappingView.java
String pricelisttype=request.getParameter(“pricelisttype”);if(pricelisttype.equalsIgnoreCase(“G”){transaction.setBiginTransaction();List l=transaction.s.createQuery(“从Pricelistmaster中选择GeneralPricingId”).List();transaction.setCommitTransaction();String json=new Gson().toJson(l);response.getWriter().write(json);}
并添加Gson.xxxx.jar库……我希望能为其他开发人员提供帮助。。。。