Spring 页面刷新后,我无法获取下拉列表值

Spring 页面刷新后,我无法获取下拉列表值,spring,jsp,Spring,Jsp,我有两个下拉列表。 1.cusotmer 2.环境 一旦更改了customer下拉列表,我将使用rest调用和onchange方法获取环境名称 我有一个GO按钮。单击GO按钮后,我的页面必须刷新。基于选定的客户和环境。我得到了一些结果。但当时我的环境下拉列表没有值。如果我改变我的客户,那么它的加载 问题是在页面刷新后,我的下拉列表没有保留值 这是我的密码 <script> $(document).ready(function() { $("#customerD

我有两个下拉列表。 1.cusotmer 2.环境

一旦更改了customer下拉列表,我将使用rest调用和onchange方法获取环境名称

我有一个GO按钮。单击GO按钮后,我的页面必须刷新。基于选定的客户和环境。我得到了一些结果。但当时我的环境下拉列表没有值。如果我改变我的客户,那么它的加载

问题是在页面刷新后,我的下拉列表没有保留值

这是我的密码

<script>
    $(document).ready(function() {
        $("#customerDetails").change(function() {
            var value1 = $('#customerDetails :selected').text();
            $.ajax({
                type : 'POST',
                url : 'environments',
                data : {
                    selectedcustomername : value1
                },
                success : function(result){
                    getEnvNames(result);
                }

            });
        });
    });


function getEnvNames(result){
    $('#environmentName').empty().append('<option selected="selected" disabled="disabled">Select An Environment</option>');
    var data = JSON.parse(result);
    $.each(data, function(key, value)
    {
        $("#environmentName").append("<option>" + value.environments_name +" - "+ value.environments_purpose + "</option>");

    });

} 

$(文档).ready(函数(){
$(“#customerDetails”).change(函数(){
var value1=$('#customerDetails:selected')。text();
$.ajax({
键入:“POST”,
url:'环境',
数据:{
selectedcustomername:value1
},
成功:功能(结果){
getEnvNames(结果);
}
});
});
});
函数getEnvNames(结果){
$('#environmentName').empty().append('Select An Environment');
var data=JSON.parse(结果);
$。每个(数据、函数(键、值)
{
$(“#环境名称”).append(“+value.environments_name+”-“+value.environments_purpose+”);
});
} 

环境:
选择一个环境
这是我的控制器

@RequestMapping(value = "/environments", method = RequestMethod.POST)
      public @ResponseBody String getEnvironmentNames(HttpServletRequest request,
                HttpServletResponse response,@RequestParam String selectedcustomername) throws SQLException {

        request.setAttribute("selectedcustomername", selectedcustomername);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("environments", new Environments());
         List<Environments>  environmentnamesList= loginDelegate.getEnvironments(selectedcustomername);
         Collections.sort(environmentnamesList, new CustomComparator());
         Gson gson = new Gson();
         System.out.println("gson"+gson);
         String jsonString = gson.toJson(environmentnamesList);
         System.out.println("jsonString"+jsonString);
         return jsonString;
    }
@RequestMapping(value=“/environments”,method=RequestMethod.POST)
public@ResponseBody字符串getEnvironmentNames(HttpServletRequest,
HttpServletResponse响应,@RequestParam String selectedcustomername)引发SQLException{
setAttribute(“selectedcustomername”,selectedcustomername);
ModelAndView ModelAndView=新建ModelAndView();
addObject(“环境”,新环境());
列表环境名称列表=loginDelegate.getEnvironments(selectedcustomername);
Collections.sort(environmentnamesList,newcustomcomparator());
Gson Gson=新的Gson();
系统输出打印项次(“gson”+gson);
字符串jsonString=gson.toJson(环境名称列表);
System.out.println(“jsonString”+jsonString);
返回jsonString;
}
这是我的按钮

<form:form method="get" action="retrieve" modelAttribute="customer" commandName="customer">

in between customer and environment dropdown ui..finally
 <input name="Submit" value="Go" type="submit" class="boldFontSize3" /> 

在客户和环境之间的下拉菜单ui..最后

解决方案1: 不要重新加载页面,而是使用ajax填充数据并显示在ui上

解决方案2: 使用javascript cookies存储下拉列表的当前状态,读取cookie并在页面加载时设置下拉列表

使用cookies:

html:


简单的方法是在控制器检索操作中读取选定的客户值,并将相关的环境值设置为模型。因此,在页面刷新后,您也将获得下拉列表值。

我无法停止页面加载。您能告诉我如何设置此页面的cookie吗。我对Java不熟悉。我正在使用。因为此页面正在刷新。可以告诉我如何设置cookie它很简单,在单击按钮或选择第二个下拉列表时,使用javascript设置cookie。然后在页面加载时读取cookie并设置下拉列表。如果您不介意,请使用此$.cookie(“select2”,select2)帮助处理示例代码。我正在存储刚刚选定的环境。但我想存储整个下拉列表值。您能发布您的检索操作方法吗
<form:form method="get" action="retrieve" modelAttribute="customer" commandName="customer">

in between customer and environment dropdown ui..finally
 <input name="Submit" value="Go" type="submit" class="boldFontSize3" /> 
<select id='select1'><option value='set1'>set1</option></select>

<select id='select2'><option value='set2'>set2</option></select>
$(document).ready(function(){

$('#select2').change(function(){

var select2=$(this).val()
var select1=$('#select2').val()

$.cookie("select1", select1)
$.cookie("select2", select2)

})

//page load

var select1=$.cookie("select1")|| "default"
var select2=$.cookie("select2")|| "default"

$('#select1').val(select1)
$('#select2').val(select2)

})