如何在java脚本

时间:2016-01-08 11:28:39

标签: javascript spring

我在第一次下拉时在Change上创建ajax调用。基于第一次下拉我得到第二个下拉值。虽然我正在发送选定的客户名称和触发post方法。通过后端我返回模型对象。如何在ajax成功调用中处理此模型。

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

这是控制器层代码:

public ModelAndView getenvironments(HttpServletRequest request,
            HttpServletResponse response,@ModelAttribute("customer") Customer customer,@RequestParam String selectedcustomername) throws Exception{
        System.out.println("selected cust name"+selectedcustomername);
        ModelAndView model = null;  
        Map<String, Object> map = new HashMap<String, Object>();
        List<org.mvc.domain.Customer> customerList = loginDelegate.getCustomerList();
        List<org.mvc.domain.Environments> environmentnamesList = loginDelegate.getEnvironments(selectedcustomername);
        Collections.sort(environmentnamesList, new CustomComparator());
        for(int i=0;i<environmentnamesList.size()-1;i++){
            customer.setEnvrironmentId(environmentnamesList.get(0));
            customer.setEnvironmentName(environmentnamesList.get(1));
        }
        map.put("customerList", customerList);
        map.put("environmentnamesList", environmentnamesList);
        model = new ModelAndView("welcome", "map", map);
        return model;

    }

这是我的下拉ui代码

<form:form method="get" commandName="frmSample" action="retrieve" modelAttribute="customer">
<b>Environment:</b>
                        <form:select path="EnvrironmentId" id="environmentName">                                                                                                                                                    
                        <option selected="selected" disabled="disabled">Select An Environment</option>
                        <form:options items="${map.environmentnamesList}" itemLabel="environmentName" itemValue="envrironmentId"/>                                                                                                                                                      
                        </form:select>
                     </td>

My First dropdown Ui:

<form:select path="CustomerId" id="customerDetails">
    <option selected="selected" disabled="disabled">Select Customer</option>
     <form:options items="${map.customerList}"
     itemLabel="customerName" itemValue="customerId" />
    </form:select> <br>


@RequestMapping(value = "/login", method = RequestMethod.POST)
    public ModelAndView executeLogin(HttpServletRequest request,
            HttpServletResponse response,
            @ModelAttribute("loginBean") LoginBean loginBean,
            @ModelAttribute Customer customer) {
        response.setHeader("Connection", "Keep-Alive");
        ModelAndView model = null;
        try {
            boolean isValidUser = loginDelegate.isValidUser(
                    loginBean.getUsername(), loginBean.getPassword());
            if (isValidUser) {
                System.out.println("User Login Successful");
                request.setAttribute("loggedInUser", loginBean.getUsername());
                Map<String, Object> map = new HashMap<String, Object>();
                List<org.mvc.domain.Customer> customerList = loginDelegate
                        .getCustomerList();
                map.put("customerList", customerList);
                model = new ModelAndView("welcome", "map", map);
            } else {

                model.addObject("loginBean", loginBean);
                request.setAttribute("message", "Invalid credentials!!");
                model = new ModelAndView("login");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return model;
    }

1 个答案:

答案 0 :(得分:0)

对于ModelAndView,返回类型使用ModelAndViewResolverMethodReturnValueHandler,因此它不会返回一个json字符串。我建议你返回一张地图,并使用@ResponseBody,如:

public @ResponseBody Map<String, Object> getEnvironments(HttpServletRequest request,
            HttpServletResponse response,@ModelAttribute("customer") Customer customer,@RequestParam String selectedcustomername) throws Exception{
        System.out.println("selected cust name"+selectedcustomername);

        Map<String, Object> map = new HashMap<String, Object>();
        List<org.mvc.domain.Customer> customerList = loginDelegate.getCustomerList();
        List<org.mvc.domain.Environments> environmentnamesList = loginDelegate.getEnvironments(selectedcustomername);
        Collections.sort(environmentnamesList, new CustomComparator());
        for(int i=0;i<environmentnamesList.size()-1;i++){
            customer.setEnvrironmentId(environmentnamesList.get(0));
            customer.setEnvironmentName(environmentnamesList.get(1));
        }
        map.put("customerList", customerList);
        map.put("environmentnamesList", environmentnamesList);
        return map;

    }