在Spring MVC中动态填充下拉列表

xj3cbfub  于 2022-11-14  发布在  Spring
关注(0)|答案(3)|浏览(216)

我是Spring MVC的新手。我在JSP中有两个下拉列表。一个是国家,另一个是州。只要我从列表中选择国家,州列表就应该用各自的列表填充。
我的ajax程序:

$("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");
        }          

       });

我的控制器程序:

public @ResponseBody HashMap<String, String> 
    showstates(@RequestParam(required = false, value =      "") 
    String country,@Valid    @ModelAttribute("employee")Login employee, 
    BindingResult result, ModelMap model) {  
    HashMap<String,String>  statelist = new HashMap<String,String>();
    List<States> statelist = new ArrayList<States>();
    if (country.equals("UnitedStates")) {
    statelist.put("Al","Alaska");
    statelist.put("Tl","Texas");
    } else if (country.equals("India")) {
    statelist.put("Mh","Maharashtra");
    statelist.put("WB","West Bengal");
    statelist.put("KR","Karnataka");
    statelist.put("AP","Andhra Pradesh");
    statelist.put("TN","Tamil Nadu");
    } 
    return statelist;
    }

我没有得到控制器的响应。如何在jsp中访问hasmap。请帮助我。谢谢。

rekjcdws

rekjcdws1#

我认为你应该改变你的方法如下。

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

    return stateMap;
    }

然后在JSP上,您可以简单地迭代这个Map,如下所示。

<c:forEach items="${state.stateMap}" var="data" varStatus="status">
            <tr>
                <td>${data.value}</td>
            </tr>
        </c:forEach>
dgjrabp2

dgjrabp22#

剪切我的解决方案。因为我被困在同样的条件。
超文本标记语言页面
第一个

第一个下拉列表的控制器代码

@RequestMapping(value = "/dropdown", method = RequestMethod.GET)
public String dropDown(Model model) {
    List<Tag> data = new ArrayList<>();
    data.add(new Tag(1, "ruby"));
    data.add(new Tag(2, "rails"));
    data.add(new Tag(3, "c / c++"));
    data.add(new Tag(4, ".net"));
    data.add(new Tag(5, "python"));
    data.add(new Tag(6, "java"));
    data.add(new Tag(7, "javascript"));
    data.add(new Tag(8, "jscript"));
    model.addAttribute("dropdown", data);
    return "dropDown";
}

第二个下拉代码为

Map <\string, string> udata = null;
@RequestMapping(value = "ajaxAction0", method = RequestMethod.GET)
public @ResponseBody Map<String, String> findAllAgencies(
        @RequestParam(value = "ptype", required = true) String ptype) {
    udata = new HashMap<>();
    EntityManager em = entityManagerFactory.createEntityManager();
    em.getTransaction().begin();
    List<UserRecord> user = em
            .createNativeQuery("Select id, name, email from user_Record where id = :uid", UserRecord.class)
            .setParameter("uid", ptype).getResultList();
            for(UserRecord ur:user)     {
        udata.put(ur.getEmail(), ur.getName());
    }
    return udata;   }

希望能有所帮助。

0wi1tuuw

0wi1tuuw3#

您可以根据国家a地区获取州a省列表:

@Autowired
StateService stateService;

@GetMapping("/states")
public @ResponseBody List<State> getStates(@RequestParam String country) {
    return stateService.getStates(country) ;
}

State类将包含代码和名称;

$("select#country").change(function() {
    var countryVal = $("#country").val();
    $.ajax({
       url : '/states?country='+countryVal,
       method : 'GET',
    },
    success: function (states) {
        var stateSelect = $('#state'); // the state select element
        stateSelect.find('option').remove();
        for (i = 0; i < states.length; i++) { 
            stateSelect.append("<option value=" + states[i].code + " >" + states[i].name + "</option>")
        }
    },

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

});

相关问题