DataTable排序和会话范围不起作用

时间:2013-03-19 17:12:17

标签: java spring jsf primefaces

简单数据表示例的排序适用于Controler的视图范围,但不适用于会话。 我已经提供了我的代码以获取更多信息。 用户数据列表填充表格。

有没有人知道为什么会这样做?

模型

...
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long mId;
    private String mUsername;
    private String mPassword;
    private String mEmail;
    private Date mBirthdate;

    public User() { 
    }

    public User(Long aId, String aUsername, String aPassword, String aEmail,
            Date aBirthdate) {
        super();
        mId = aId;
        mUsername = aUsername;
        mPassword = aPassword;
        mEmail = aEmail;
        mBirthdate = aBirthdate;
    }

    ....
}

控制器

...    
@Controller("register")
@Scope("session")
public class Register implements Serializable {

    private static final long serialVersionUID = 1L;

    private User mUser;
    private List<User> mUsers = new LinkedList<User>();
    private char[] mChars = "abcdefghijklmnopqrstuvwxyz".toCharArray();

    public Register() {
    }

    @PostConstruct
    public void init() {
        mUser = new User();

        for (int i = 0; i < 10; i++) {
            mUsers.add(new User(1L, randStr(3)+"_user", randStr(1)+"pass", randStr(1)+"user@mail.com", new Date()));
        }
    }

    public User getUser() {
        return mUser;
    }

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

    public void submit() {
        FacesMessage msg = new FacesMessage("Registration successful");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
    private String randStr(int len){
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < len; i++) {
            char c = mChars[random.nextInt(mChars.length)];
            sb.append(c);
        }
        return sb.toString();
    }

}

查看

<p:dataTable value="#{register.users}" var="usr" paginator="true" rows="4" sortBy="#{usr.username}">
    <p:column headerText="a" sortBy="#{usr.username}">
        <h:outputText value="#{usr.username}"></h:outputText>
    </p:column>
    <p:column headerText="b" sortBy="#{usr.password}">
        <h:outputText value="#{usr.password}"></h:outputText>
    </p:column>
    <p:column headerText="c" sortBy="#{usr.email}">
        <h:outputText value="#{usr.email}"></h:outputText>
    </p:column>
</p:dataTable>

1 个答案:

答案 0 :(得分:0)

好的,我发现了什么问题。

虽然它使用的是AJAX,但数据表必须包含在<h:form>

然而,在view范围内,不需要,奇怪......

感谢您的友好合作,感谢您。