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
}

when user calls MANAGE_EMPLOYEE url then I return employeeView(jsp) where I have to display list of employees so that user can edit and save it back again. I know I can user @JsonSerialize(using=EmployeeDTOSerializer.class) at my DTO with http request to Controller and annotating @ResponseBody but here I am adding it to model attribute so i want to know how to serialize list of object before i send it to JSP.

jny
  • 8,007
  • 3
  • 37
  • 56
user007
  • 9
  • 3

1 Answers1

0

You'll have to do it yourself using one of the libraries. For example you could use ObjectMapper from Jackson :

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

and

    //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;
}

This (probably with some modifications) will write json string to the model. However I would not recommend this. I suggest adding an AJAX call to your jsp which would retrieve the list of employees. Then you'll have to add a method to your controller which would return a list and annotate it with @ResponseBody.

jny
  • 8,007
  • 3
  • 37
  • 56