0

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

有人知道为什么会有这种行为吗?

模型

...
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>
4

1 回答 1

0

好的,我发现了问题所在。

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

但是在view不需要的范围内,奇怪...

感谢您的友好合作,不胜感激。

于 2013-03-20T10:08:59.750 回答