有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何将一个数组从jsp页面中的JavaScript函数传递到控制器类?

我实现了一个JavaScript按钮操作,将jsp页面中所有选中的元素推送到一个数组中。现在我需要将这个数组传递给控制器类。我需要删除控制器函数中该数组的元素。我该怎么做?我需要的只是pas JavaScript函数、jsp表单和控制器类函数如下所示

<script type="text/javascript"> function selected(){ var all_checked = document.querySelectorAll('input[name=checkbox]:checked'); //document.write(all_checked.length); var selectedIds = []; for(var x = 0, l = all_checked.length; x < l; x++) { selectedIds.push(all_checked[x].value); //document.write(all_checked[x].value); } return selectedIds; } </script> > selectedIds is containing the checked items in the jsp page.
<form:form method="post" action="createnewstory/${userstoryId} }" modelAttribute="selectedIds" modelAttribute="fulluserstory">
<div class="container">
<div class="deletebutton">
<input id="deleteAllButton"  onClick="selected()" class="btn-lg btn-primary pull-right" type="submit" name="actionButton" value="Delete Selected"></input>
</div>
<div class="content">
<c:if test="${!empty storyList}">

		<table class="table">
		<thead>
			<tr>
			<th></th>
				<th>User Story Id</th>
				<th>User Story Name</th>
				<th>Status</th>
			</tr>
			</thead>
			
		<tbody>
			<c:forEach items="${storyList}" var="userstory">
			
				<tr>
				<td class="checkbox"><input name="checkbox" type="checkbox" value="${userstory.userstoryId}" id="####" ></td>
				<td class="uId"><c:out value="${userstory.userstoryId}"/></td>
				<td class="uName">
					 <a href="<c:url value='/viewuserstory/${userstory.userstoryId}'/>"><c:out value="${userstory.userstoryname}"/></a>
				</td>
				<td class="uStatus"><c:out value="${userstory.status}"/></td>	
					
				</tr>
		</c:forEach>		
		</tbody>
		
		</table>
		</c:if>
		</div>
		</div>
</form:form>

@RequestMapping(value="/createnewstory",method=RequestMethod.POST) public String createNewStory(Map<String,Object> map,@ModelAttribute("fulluserstory") Fulluserstory fulluserstory,@RequestParam String actionButton,HttpServletRequest request){ if (actionButton.equals("Delete Selected")){ int [] checkedlist={73,74}; for(int i=0;i<checkedlist.length;i++){ int userstoryId=checkedlist[i]; userstoryService.delete(userstoryId); System.out.println(checkedlist[i]); System.out.println("delete#######selected inside for"); } }

I need to get setectedIds array in controller,as checkedlist array.


共 (3) 个答案

  1. # 1 楼答案

    第一种方法是 通过表单操作传递Javascript数组变量以获取控制器上的值

    像这样把它放在控制器上

    String selectedIds[] = request.getParameterValues("selectedIds");
    

    使varselectedIds = [];全局

  2. # 2 楼答案

    您可以对数组进行字符串化

    JSON。字符串化(选定的EDID)

    在控制器中,将其作为字符串参数接受,并根据需要将其转换回数组或列表

    谢谢, 拉文德拉

  3. # 3 楼答案

    我建议您使用primefacesremote command。您可以在here中找到详细信息