0
@Controller
public class ManageEmployee{
@ModelAttribute("employeeForm")
public EmployeeForm createEmployeeForm(Model model, HttpSession session){
EmployeeForm eform = new EmployeeForm ();
List<EmployeeDTO> eList = employeeService.getEmployeeList(employeeId)//employeeId comes from session
eform.setEmployeeDTO(eList );
model.addAttribute("empoyeeList",eList );
return eform;
}
@RequestMapping(value = LogInUris.MANAGE_EMPLOYEE, method =   RequestMethod.GET)
public String showEmployee(Model model, ModelMap map) throws     ServiceException{ 
    return "employeeView";
}
}

public class EmployeeDTO{
 private String eId;
 private String eName;
 private String eLastName;
 private String positon;
 private String role;
//getter//setter
}

当用户调用MANAGE_EMPLOYEEurl 然后我返回employeeView(jsp),我必须在其中显示员工列表,以便用户可以编辑并再次保存它。我知道我可以@JsonSerialize(using=EmployeeDTOSerializer.class)在我的 DTO 上使用 http 请求Controller和注释来用户,@ResponseBody但在这里我将它添加到模型属性中,所以我想知道如何在将对象列表发送到之前将其序列化JSP

4

1 回答 1

0

您必须使用其中一个库自己完成。例如,您可以ObjectMapperJackson使用:

   // In configuration:
    ObjectMapper mapper=new ObjectMapper();

    //In Controller
    @ModelAttribute("employeeForm")
     public EmployeeForm createEmployeeForm(Model model, HttpSession session){
           EmployeeForm eform = new EmployeeForm ();
           List<EmployeeDTO> eList = employeeService.getEmployeeList(employeeId)//employeeId comes from session
           eform.setEmployeeDTO(eList );
           model.addAttribute("empoyeeList", mapper.writeValueAsString(eList) );
          return eform;
}

这(可能有一些修改)会将 json 字符串写入模型。但是我不会推荐这个。我建议向您的 jsp 添加一个 AJAX 调用,该调用将检索员工列表。然后,您必须向控制器添加一个方法,该方法将返回一个列表并使用@ResponseBody.

于 2015-03-11T15:23:54.847 回答