Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在SpringMVC中将列表从控制器传递到jsp并在javascript中迭代_Java_Javascript_Spring_Jsp_Spring Mvc - Fatal编程技术网

如何在SpringMVC中将列表从控制器传递到jsp并在javascript中迭代

如何在SpringMVC中将列表从控制器传递到jsp并在javascript中迭代,java,javascript,spring,jsp,spring-mvc,Java,Javascript,Spring,Jsp,Spring Mvc,我有一张单子 List<Integer> ids = new ArrayList<Integer>(); idArray具有所有带逗号和空格的ID。现在这是不对的。我知道我做这件事的方式不对。有人能帮忙吗? 我需要在列表中找到的id是在javascript中获得的$(“#idUser”).val()中 请帮忙。如果我正确理解了你的问题,那么你想做什么 假设您的jsp表单如下所示: <c:url value="/formaction" var="postUrl"

我有一张单子

  List<Integer> ids = new ArrayList<Integer>();
idArray具有所有带逗号和空格的ID。现在这是不对的。我知道我做这件事的方式不对。有人能帮忙吗? 我需要在列表中找到的id是在javascript中获得的$(“#idUser”).val()中


请帮忙。

如果我正确理解了你的问题,那么你想做什么

假设您的jsp表单如下所示:

<c:url value="/formaction" var="postUrl"/>
    <form:form id="myForm" method="post" action="${postUrl}" modelAttribute="formBean"> 
        <form:hidden path="ids"/>

        <label>Name:</label>
        <form:input path="name"/><br/>

        <label>Select Id:</label>
        <form:input path="id"/>
        <%-- <form:select path="id">
            <form:option value="0">--select id--</form:option>
            <form:options items="${formBean.ids}"/>
        </form:select> --%>

        <label id="idErr" style="color: red; display: none;">Please select another, id exists</label>
        <input type="submit"/>
    </form:form>

但是,您最好使用
您能给我们提供
idArray
的内容吗?List id=new ArrayList();对于(BeanClass user:userList){ids.add(user.getUserId());}modelAndView.addObject(“userIds”,ids);明白了吗?
#BranchId列表
在您的示例中不存在。已更新代码。那是个打字错误。branchIdList应该是idList对不起,我的意思是,如果你在JS中记录
idArray
的内容,那是什么?
    var idArray = new Array();
    idArray = $("#idList").val();
<c:url value="/formaction" var="postUrl"/>
    <form:form id="myForm" method="post" action="${postUrl}" modelAttribute="formBean"> 
        <form:hidden path="ids"/>

        <label>Name:</label>
        <form:input path="name"/><br/>

        <label>Select Id:</label>
        <form:input path="id"/>
        <%-- <form:select path="id">
            <form:option value="0">--select id--</form:option>
            <form:options items="${formBean.ids}"/>
        </form:select> --%>

        <label id="idErr" style="color: red; display: none;">Please select another, id exists</label>
        <input type="submit"/>
    </form:form>
public class FormBean {

    private Long id;
    private String name;
    private List<Integer> ids;

    //getters and setters
}
@RequestMapping(method=RequestMethod.GET)
public String showForm(Model model){

    FormBean formBean = new FormBean();
    formBean.setIds(new ArrayList<Integer>(Arrays.asList(1,2,3,4,5)));
    model.addAttribute("formBean", formBean);               
    return "form";
}
$("form#myForm").on('submit', function() {
   var ids = $('#ids').val(); //get values of <form:hidden path="ids"/> ,values will look like: 1,2,3,4,5
   var idsArray = ids.split(","); //split by comma
   var result = jQuery.inArray($('#id').val(), idsArray);//check user entered id exists in ids array
    if(result==-1){ 
        $('#idErr').hide();
        return true; //id not exists, submit form
    }
    else {
        //id exists, show error message
        $('#idErr').show();
        return false;
    }
});