在填写表格后,我没有在我的ModelAttribute中正确地取回我的对象

时间:2016-12-06 13:25:55

标签: spring hibernate spring-mvc spring-data modelattribute

好吧,我正在开发一个带有spring mvc和hibernate的简单项目,目前一切正常,但有些东西让我无法理解为什么它会在我的代码中发生。 我有一个带有用户表单和角色下拉列表的jsp页面

这是我的弹簧形式的表单代码:

    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
    .
    .
    .
    <form:form action="saveUser" method="POST" modelAttribute="user">
                <label>First name:</label>
                <form:input path="firstName"/>
                <br><br>
                <label>Last name:</label>
                <form:input path="lastName"/>
                <br><br>
                <label>Email:</label>
                <form:input path="email"/>
                <br><br>
                <label>Login:</label>
                <form:input path="login"/>
                <br><br>
                <label>Password:</label>
                <form:password path="pwd" />
                <br><br>
                <label>Role:</label>
                <form:select path="role">
                    <form:options items="${roles}" itemLabel="description" itemValue="id_role" />
                </form:select>
                <br><br>
            <input class="add-button" type="submit" value="Add" />
        </form:form>
        .
        .

好吧,所以当我提交表格时,我会在我的控制器上抓住它,我得到了正确的用户&#39;被填充的对象,但是角色&#39;对象不是。 例如,我选择了&#39; administrator&#39; ID = 2且DESCRIPTION =&#39;管理员&#39;所以,当我得到它时,我只能找到类似{ID = 0,DESCRIPTION = 2}的东西!

    @RequestMapping("/saveUser")
public String saveUser(@ModelAttribute("user") User user) {
    System.out.println(user.getRole()); // its gives => [id = 0, description = 2]
    int roleId = Integer.parseInt(user.getRole().getDescription()); // normally here I have to write user.getRole().getId() but you know that the id came inside description :(
    Role r = roleService.getById(roleId);
    user.setRole(r);
    userService.save(user);
    return "redirect:/user/list";
}

它让我大吃一惊..谢谢你帮助我

更新

以下是Role和RoleServiceImpl

Role.java

@Entity @Table(name = "ROLES") 
public class Role {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id_role;

    private String description;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "role")
    private List<User> users;

    public Role() {

    }

    public Role(String description) {
        super();
        this.description = description;
    }

    public Role(String description, List<User> users) {
        super();
        this.description = description;
        this.users = users;
    }

    public int getId_role() {
        return id_role;
    }

public void setId_role(int id_role) {
    this.id_role = id_role;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public List<User> getUsers() {
    return users;
}

public void setUsers(List<User> users) {
    this.users = users;
}

@Override
public String toString() {
    return "Role [id=" + id_role + ", description=" + description + "]";
}
}

RoleServiceImpl.java

@Service
public class RoleServiceImpl implements IService<Role> {

@Autowired
private IDAO<Role> roleDAO;

@Override
@Transactional
public List<Role> getAll() {
    return roleDAO.getAll();
}

@Override
@Transactional
public void save(Role role) {
    roleDAO.save(role);
}

@Override
@Transactional
public Role getById(int roleId) {
    return (Role) roleDAO.getById(roleId);
}

@Override
@Transactional
public void delete(int roleId) {
    roleDAO.delete(roleId);
}
}

更新 我的角色DAO:     @Repository 公共类RoleDAOImpl实现IDAO {

@Autowired
SessionFactory sessionFactory;

@Override
public List<Role> getAll() {
    Session session = sessionFactory.getCurrentSession();
    List<Role> roles = 
            session.createQuery("from Role order by description", Role.class)
            .getResultList();
    return roles;
}


@Override
public Role getById(int roleId) {
    Session session = sessionFactory.getCurrentSession();
    Role role = session.get(Role.class, roleId);
    return role;
}

@Override
public void save(Role role) {
    Session session = sessionFactory.getCurrentSession();
    session.saveOrUpdate(role);
}

@Override
public void delete(int roleId) {
    Session session = sessionFactory.getCurrentSession();
    Query query = session.createQuery("delete from Role where id=:roleId");
    query.setParameter("roleId", roleId);
    query.executeUpdate();
}

}

1 个答案:

答案 0 :(得分:0)

您的代码对我来说似乎是正确的。我不知道你为什么会出现这种行为!
如果使用List不起作用,请尝试使用Map。

Map<String, String> roles = roleService.getAll();

roleService.getAll()应返回<ID,Description>的地图,其中角色ID为关键,角色描述为值。

然后使用form:这样的选项

<form:select path="role">
    <form:options items="${roles}" />
</form:select>

最后,您可以获得角色ID user.getRole().getId()