dimanche 14 juin 2015

Spring 3.2.5 form tag radioButtons behavior

Can we iterate a collection of objects in the form:radiobuttons in Spring 3.2.5?

For instance in the addEmployee.jsp,

<td><form:radiobuttons path="empDepartmentName" items="${departments}"/></td>

And the method which populate departments,

@RequestMapping(value = "/", method = RequestMethod.GET)
    public String homePage(ModelMap map) {
        map.addAttribute("employee", new Employee());
        populateDepartments(map);
        return "addEmployee";
    }

    private void populateDepartments(ModelMap map){
        List<String> departments = new ArrayList<String>();
        departments.add("Dept 1");
        departments.add("Dept 2");
        map.addAttribute("departments", departments);       

    }

Can be departments be List<Department> and allow client to select a Department name from UI and map the selected department directly in the Employee entity instead of going through a transient variable empDepartmentName then fetch the Department from the selected department name and assign the Department object to Employeee and persist the Employee. Am i doing this correct way?

@Entity
public class Employee {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;
    private String name;
    private String address;

    @Transient
    private String empDepartmentName;

    @OneToOne(fetch=FetchType.LAZY)
    private Department department;
}

Aucun commentaire:

Enregistrer un commentaire