回到哈希映射从控制器到JSP用SpringMVC中 [英] Returning Hashmap From controller to JSP in Springmvc

查看:289
本文介绍了回到哈希映射从控制器到JSP用SpringMVC中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JSP中一,二dropdownlists的国家和其他的国家。当我选择国statelist应自动与各自的列表来填充。但我得到整个JSP页面响应的Ajax调用。 我的AJAX程序:

  $(#选择国)。改变(函数(){
。VAR VAL = $(#国)VAL();
警报(VAL);
$阿贾克斯({
网址:'的getStates,
方法:'得到',
的contentType:应用/ JSON的,
 数据 :{
          国家:VAL
        },

  成功:功能(数据){
  警报(成功应对+数据);

  },

   错误:函数()
   {
           警报(错误);
    }

   });
 

我的控制器程序

  @RequestMapping(值=/的getStates,方法= RequestMethod.GET)
 公众的ModelAndView showstates(@RequestParam(所需=假,值=)
     字符串的国家,@有效@ModelAttribute(雇员)登录的员工,
     BindingResult结果,型号model){
    HashMap的<字符串,字符串> stateMap =新的HashMap<字符串,字符串>();
     //把你的逻辑,增加对国家的基础状态

     如果(country.equals(UnitedStates的)){
         stateMap.put(基地,阿拉斯加);
         stateMap.put(铊,得克萨斯);

     }否则,如果(country.equals(来自国家UnitedStates)){
         stateMap.put(是,贝德福德郡);
         stateMap.put(误码率,伯克希尔);

     }否则,如果(country.equals(印度)){
         stateMap.put(MH,马哈拉施特拉邦);
         stateMap.put(WB,西孟加拉邦);
         stateMap.put(KR,卡纳塔克邦);
         stateMap.put(AP,安得拉邦);
         stateMap.put(TN,泰米尔纳德邦);
     }


     返回新的ModelAndView(LoginForm的,状态,stateMap);
 }
 

我使用的弹簧的形式。我需要只Staemap为respone,但我得到整个JSP页面响应。

解决方案
  

我需要只Staemap为respone,但我得到整个jsp页面   作为响应。

由于您正在返回这里的视图中的的ModelAndView 对象,

 返回新的ModelAndView(LoginForm的,状态,stateMap);
 

如果您需要返回 respone 独自一人从method.However你不能在的HashMap 直接打印控制器在你的JSP中的 AJAX 响应。恕我直言,你可以将其转换为 JSONArray

  JSONArray jarray = JSONArray.fromObject(statemap);
 

I have two dropdownlists in Jsp One for state and other for country. As soon as i select country the statelist should be populated automatically with respective lists. But i am getting whole jsp page as response in ajax call. My ajax Program:

$("select#country").change(function() {
var val=$("#country").val();
alert(val);
$.ajax({
url : 'getstates',
method : 'get',
contentType: 'application/json',
 data :{
          country : val
        },

  success: function (data) {
  alert("Success Response"+ data);

  },

   error :function()
   {
           alert("error");
    }          

   });

My controller program

 @RequestMapping(value = "/getstates", method = RequestMethod.GET)
 public ModelAndView  showstates(@RequestParam(required = false, value = "") 
     String country,@Valid    @ModelAttribute("employee")Login employee, 
     BindingResult result, Model model) {  
    HashMap<String,String>  stateMap = new HashMap<String,String>();
     //put your logic to add state on basis of country

     if (country.equals("UnitedStates")) {
         stateMap.put("Al","Alaska");
         stateMap.put("Tl","Texas");

     } else if (country.equals("UnitedKingdom")) {
         stateMap.put("Be","Bedfordshire");
         stateMap.put("Ber","Berkshire");

     } else if (country.equals("India")) {
         stateMap.put("Mh","Maharashtra");
         stateMap.put("WB","West Bengal");
         stateMap.put("KR","Karnataka");
         stateMap.put("AP","Andhra Pradesh");
         stateMap.put("TN","Tamil Nadu");
     } 


     return new ModelAndView("LoginForm","state" ,stateMap);
 }

I am using spring form. I need to get only Staemap as respone but i am getting whole jsp page as response.

解决方案

I need to get only Staemap as respone but i am getting whole jsp page as response.

Because you are returning the ModelAndView object with the view here,

return new ModelAndView("LoginForm","state" ,stateMap);

If you need to return the respone alone from the controller method.However you cant print the HashMap directly in the ajax response on your jsp. IMHO you can convert it to JSONArray

JSONArray jarray = JSONArray.fromObject(statemap);

Read ,

这篇关于回到哈希映射从控制器到JSP用SpringMVC中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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