这个简单数据表的排序示例是在controller的视图作用域下工作的,而不是在会话下工作。我已经提供了我的代码下面的更多信息。一个用户数据列表填充一个表。
有人知道为什么会有这样的行为吗?
...
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> 视图/strong> <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>
好的,我已经找到问题所在了。
虽然它是使用AJAX的数据表必须包装在<h:form>
.
然而,在view
范围内,这是不需要的,奇怪…
谢谢您的合作,谢谢。