只有dataTable中的第一个commandLink是可点击的

时间:2015-12-01 16:36:39

标签: java jsf java-ee datatable

我是JPA的新手,我正在尝试创建一个数据表格式,允许您按下"删除"从课程中删除用户。问题是只有数据表中的第一个(最上面的)是可点击的,并从该用户中删除该过程。 (当它被删除时,列表中的下一个现在是最重要的并且正在工作。)

问题是其他链接(除最上层之外的所有人)都刷新了没有URL参数的站点(重置会话?),并且没有调用removeUserFromCourse(..)方法。如果有人知道为什么会这样,请随时回答并指出我的错误。

控制器(缩短):

@Model
public class UserController{
    private UserDAO persister;
    private User user;
    // 103 is just a default value
    private int selectedID = 103;

    @Inject
    public UserController(UserDAO persister) {
        this.persister = persister;
    }

    @PostConstruct
    public void init() {
        this.user = new User();
    }

    public int getSelectedID() {
        return selectedID;
    }

    public void setSelectedID(int selectedID) {
        this.selectedID = selectedID;
    }

    public void removeUserFromCourse(int courseID){
        persister.removeFromCourse(selectedID, courseID);
        user = persister.getUser(selectedID);
    }
}

edit-user.xhtml中的表单:

         <h:form>
            <h:dataTable id="mdtb" value="#{userController.getUserCourses()}" var="course"
                         styleClass=" col-lg-6">
                <h:column>
                    <h:outputText value="#{course.name} (#{course.id})" styleClass="col-lg-6"/>
                    <h:commandLink update="mdtb" value="Remove"
                                   action="#{userController.removeUserFromCourse(course.id)}">
                        <f:param name="id" value="#{userController.selectedID}"/>
                    </h:commandLink>
                </h:column>
            </h:dataTable>
        </h:form>

用户JPA(缩写):

@Stateless
public class JPAUserDao implements UserDAO {
    EntityManagerFactory entityManagerFactory;
    @PersistenceContext(name = "Egentreningprosjekt")
    EntityManager entityManager;

    public JPAUserDao() {
    }

    public JPAUserDao(EntityManager entityManager){
        this.entityManager = entityManager;
    }

    @Override
    public User update(User user) {
        System.out.println("updating user " + user);
        entityManager.merge(user);
        return user;
    }

    @Override
    public void removeFromCourse(int userID, int courseID) {
        User user = getUser(userID);
        List<Course> courses = user.getCourses();
        Course courseToBeDeleted = null;

        for(Course course : courses){
            if(course.getId() == courseID){
                courseToBeDeleted = course;
            }
        }

        if(courseToBeDeleted != null){
            courses.remove(courseToBeDeleted);
            courseToBeDeleted.getUsers().remove(user);
        }

        user.setCourses(courses);
        update(user);
        entityManager.merge(courseToBeDeleted);
    }

User.java(缩写):

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Min(value = 0, message = "ID cannot be negative")
    private int id;

    @NotNull
    @Pattern(regexp = "^([A-Z|a-z|0-9](\\.|_){0,1})+[A-Z|a-z|0-9]\\@([A-Z|a-z|0-9])+((\\.){0,1}[A-Z|a-z|0-9]){2}\\.[a-z]{2,3}$")
    private String email;

    @ManyToMany(mappedBy = "users", cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER)
    private List<Course> courses;

    // getters/setters
}

1 个答案:

答案 0 :(得分:0)

现在4年后回答这个问题。正如评论所暗示的,这是由于对103进行了硬编码。