无法将List对象作为隐藏变量从JSP传递到servlet [英] Trouble passing a List object as hidden variable from JSP to a servlet

查看:251
本文介绍了无法将List对象作为隐藏变量从JSP传递到servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在控制器中呈现从jsp传递为隐藏变量的对象列表.

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

这是我的代码

在我的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>

在我的控制器中

//方法搜索员工

@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*"));
    -----
    }

当我运行empList时,以数组格式返回字符串,但是我想从jsp获取Employee对象列表.

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 .

非常感谢您的帮助.

提前谢谢!

推荐答案

这不是将对象保存在隐藏字段中的正确方法.

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

在浏览器中查看源代码,它可能会显示类似List<Employee>的默认toString()表示形式.

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]"/>


在会话中将其设置为方法lookupEmployee()中的属性,然后在downloadCSV()方法中将其取回.


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

示例代码:

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


编辑

我不想为此使用会话范围,我正在寻找其他替代选项

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

使用 JSON 字符串在服务器和客户端之间传输对象.

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

使用任何JSON解析库(例如 GSON库形成) JSON字符串中的一个JAVA对象,反之亦然.

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

请查看以下示例

这篇关于无法将List对象作为隐藏变量从JSP传递到servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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