Trouble passing a List object as hidden variable from JSP to a servlet

牧云@^-^@ 提交于 2019-12-11 18:37:42

问题


I am trying to render a list of objects in my controller which are passed from jsp as hidden varaiable.

Here is my code

in my employeeResults.jsp

<c:forEach items="${list}" var="employee"  >
<tr>
<td>${employee.empId}</td>
<td>${employee.empName}</td>
<td>${employee.empEmail}</td>
</tr>
</c:forEach> 
<form  method="POST"  action="downloadCSV.html" >
<input type="hidden" id="empList" name="empList" value="${list}"/>
<input type="submit" name="download" value="Download"/>
</form>

In my Controller

// method call for employee search

@RequestMapping(value = "/search", params = "search", method = RequestMethod.POST)
public ModelAndView lookupEmployee(@ModelAttribute("command") Employee emp) {

String lookupEmpId = null;
if(emp.getEmpId()!= null)
lookupEmpId = emp.getEmpId();
String[] line = lookupEmpId.split("\n");
List<String> eIds = new ArrayList<String>();

for(String i: line){
eIds.add(i);
}

List<Employee> listEmp = employeeDAO.searchRecords(eIds); 
ModelAndView model = new ModelAndView("lookupResults");  
model.addObject("list",listEmp);
    return model;

}// addContact()

  @RequestMapping(value = "/downloadCSV", method = RequestMethod.POST)
    public void downloadCSV(HttpServletRequest request, HttpServletResponse response) throws       IOException {

    String empList = request.getParameter("empList");
    List<String> items = Arrays.asList(empList.split("\\s*,\\s*"));
    -----
    }

when I run empList is returning a sting in array format , but I would like to do is to get the Employee object list from jsp .

Any help is greatly appreciated.

Thanks in advance !


回答1:


This is not the correct way to save the object in hidden fields.

Look at the source code in the browser, It might display something like that is default toString() representation of the List<Employee>.

<input type="hidden" id="empList" name="empList" value="[pkg.Employee@7b9bd73]"/>

Set it in session as attribute in method lookupEmployee() and get it back in downloadCSV() method.

Sample code:

public ModelAndView lookupEmployee(HttpServletRequest request,@ModelAttribute("command") Employee emp) {
     ...
     List<Employee> listEmp = employeeDAO.searchRecords(eIds); 
     request.getSession().setAttribute("empList", listEmp);
     ...
}

public void downloadCSV(HttpServletRequest request, HttpServletResponse response) throws IOException {
     List<Employee> empList = (List<Employee>)request.getSession().getAttribute("empList");
     ...
}

EDIT

I don't want to use session scope for this, I am looking for any other alternative option

Use JSON string to transfer an Object between server and client.

Use any JSON parsing library such as GSON library to form a JAVA object from JSON string and vice-verse.

Please have a look at this Example



来源:https://stackoverflow.com/questions/24658195/trouble-passing-a-list-object-as-hidden-variable-from-jsp-to-a-servlet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!