在Spring MVC控制器中发现加载了Ajax的动态下拉列表为null [英] Dynamic drop down loaded with ajax is found as null in spring mvc controller

查看:77
本文介绍了在Spring MVC控制器中发现加载了Ajax的动态下拉列表为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过 ajax 调用动态加载了一个下拉列表,但是问题是我无法在 controller 中访问它.在控制器中,动态加载的下拉列表为 null

I've dynamically loaded a dropdown with ajax call but the problem is I can't access it in controller. the dynamically loaded dropdown is found as null in controller!

JSP代码:

<body>
    <c:url var="updateUrl" value="${actionPath}"/>
    <form:form method="post" action="${updateUrl}" commandName="complaintDetail">
        <table>
            <tr>
                <td><form:label path="complaintCategory">Category</form:label></td>
                <td>                        
                    <form:select path="complaintCategory">
                        <form:option value="NONE" label="--- Select ---" />
                        <form:options items="${categoryList}"/>
                    </form:select>
                </td> 
            </tr>
            <tr>
                <td><form:label path="complaintReasonDetail">Reason</form:label></td>
                <td>                        
                    <form:select path="complaintReasonDetail">
                        <form:option value="NONE" label="--- Select ---" />
                        <form:options items="${reasonList}" />
                    </form:select>
                </td> 
            </tr>
            <tr>
                <td colspan="2">
                    <input type="submit" value="${submitString}">
                </td>
            </tr>
        </table>  
    </form:form>
</body>

JS代码:

$('#complaintCategory').change(
function(){
    var findReasonsURL = '/Vela-web-client/complaint/getReason/' + $(this).val() + '/GENERAL.htm';
    //alert($(this).val());
    $.getJSON(findReasonsURL, {},
    function(data) {
        var html = '<option value="">--- Select ---</option>';
        var len = data.length;
        $.each(data, function(i, obj){
            html += '<option value="' + obj.idNr + '">' + obj.complaintReasonSt + '</option>';
        });

        $('#complaintReasonDetail').html(html);

    });
}); 

对于此ajax调用,相应的控制器代码:

@RequestMapping(value = "/complaint/getReason/{category}/{rType}", method = RequestMethod.GET)
public @ResponseBody 
List<ComplaintReasonDetail> getReason(
        @PathVariable(value = "category") String category,
        @PathVariable(value = "rType") String rType)
{
    List<ComplaintReasonDetail> complaintReasonDetails;

    ComplaintCategory complaintCategory = ComplaintCategory.valueOf(category);
    ReasonType reasonType = ReasonType.valueOf(rType);
    try
    {
        complaintReasonDetails = ComplaintReasonHandler.getComplaintReasonAdvanceSearchResult("", false, complaintCategory, true, reasonType, true, BigInteger.ONE);
        System.out.println("reason list count = " + complaintReasonDetails.size());
        return complaintReasonDetails;
    }
}

到目前为止,一切正常!

Up to this, everything works fine!

我提交表单时出现问题.我在控制器代码中以 null 的形式获得了 complaintReasonDetail .

The problem occurs when I submit the form. I get complaintReasonDetail as null in controller code.

控制器代码:

@RequestMapping(value = "/complaint/doadd/addnew", method = RequestMethod.POST)
public String addCompliant(@RequestBody @ModelAttribute("complaintDetail") ComplaintDetail complaintDetail, BindingResult result, Model model)
{
    System.out.println("category = "+complaintDetail.getComplaintCategory()); // OK
    System.out.println("reason = "+complaintDetail.getComplaintReasonDetail());  // print null ???
}

这是一个很长的问题,但是要弄清楚,我认为这是必要的.

It's a bit long question, but to make it clear, i think it is necessary.

那么,我到底想念什么?

So, what am I missing actually?

推荐答案

您可以通过带有onchange事件的select调用ajax,例如以下代码:

You can call ajax from select with onchange event like code below:

function onChangeAjaxMain()
        {
            var name = $('#state').val();

            $.ajax({
                type: "POST",
                url : 'mainAjax',
                data: ({
                    nameOfCategory: name
                }),
                success : function(data) {
                    $('#city').html(data);

                }
            });
        }

在jsp中使用form:spring格式:

in jsp use form:spring format:

<form:select name="state" id="state" onclick="onChangeAjaxMain()" path="state">
    <form:option value="1">blah</form:option>
    <form:option value="2">blah</form:option>
</form:select>
    <br>
    <form:select id="city" path="city">
        <form:option value="NONE">City</form:option>
    </form:select>

最终在控制器中:

@RequestMapping(value = "/mainAjax", method = RequestMethod.POST)
    public @ResponseBody
   String mainAjax(@RequestParam("state") String state){

        String cities= "<option value=\"\">City</option>";
        ArrayList<City> citySet = /*fill list respect to state*/

        for(City city : citySet){

            cities+= "<option value=\""+city.getName+"\">"+city.getName+"</option>";
        }
        cities+= "</option>";
        return cities;

在maven中添加:jackson-core和jackson-databind.我使用2.6.3版本,并在1.5.3版本中添加jackson-mapper-asl

In maven add: jackson-core and jackson-databind. I use 2.6.3 version and also add jackson-mapper-asl with 1.5.3 version

希望对您有所帮助!

这篇关于在Spring MVC控制器中发现加载了Ajax的动态下拉列表为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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