简单数据表示例的排序正在使用 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>