[英]JSF Keep Parameter in XHTML
如何在 XHMTL 文件中保留參數。
例如我想保留參數
....param name="name" value="john"....
然后另一行我想用調用名稱參數打印它。
我想做這個。 我在多次調用 sessionscoped bean 時遇到問題。
我會這樣解決。
登錄 Bean 在這里:
@ManagedBean(name = "UserLogin")
@RequestScoped
public class Login {
@ManagedProperty(value="#{LoggedUser}")
private LoginUser loginUser;
private String username = null;
private String password = null;
private String message = null;
private Boolean isLoggedin = false;
private Boolean isAdmin = false;
private User loggedinUser = null;
public User getLoggedinUser() {
this.loggedinUser = loginUser.getLoggedUser();
return loggedinUser;
}
public void setLoggedinUser(User loggedinUser) {
this.loggedinUser = loggedinUser;
}
public LoginUser getLoginUser() {
return loginUser;
}
public void setLoginUser(LoginUser loginUser) {
this.loginUser = loginUser;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Boolean getIsLoggedin() {
if(this.loginUser.getLoggedUser() != null){
this.isLoggedin = true;
}
else{
this.isLoggedin = false;
}
return isLoggedin;
}
public void setIsLoggedin(Boolean isLoggedin) {
this.isLoggedin = isLoggedin;
}
public Boolean getIsAdmin() {
if(this.loginUser.getLoggedUser() != null){
if(this.loginUser.getLoggedUser().getAdmin()){
this.isLoggedin = true;
}
else{
this.isLoggedin = false;
}
this.isLoggedin = true;
}
else{
this.isLoggedin = false;
}
return isAdmin;
}
public void setIsAdmin(Boolean isAdmin) {
this.isAdmin = isAdmin;
}
public void doLogin() {
System.out.println(username+"");
System.out.println(password+"");
System.out.println(isLoggedin+"");
RequestContext context = RequestContext.getCurrentInstance();
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();// static
Session hiberSession = sessionFactory.openSession();// session a çevir
ArrayList<User> userList = null;
try {
Criteria crt = hiberSession.createCriteria(User.class);
crt.add(Restrictions.eq("username", this.username));
crt.add(Restrictions.eq("password", this.password));
userList = (ArrayList<User>) crt.list();
if (userList.size() == 0) {
this.message = "Incorrect username or password!";
this.isLoggedin = false;
this.isAdmin = false;
} else {
this.loggedinUser = userList.get(0);
this.loginUser.setLoggedUser(loggedinUser);
this.isLoggedin = true;
this.isAdmin = userList.get(0).getAdmin();
this.message = "";
FacesContext.getCurrentInstance().addMessage(
null,
new FacesMessage(FacesMessage.SEVERITY_INFO, "Welcome,"
+ this.loggedinUser.getName() + " ("
+ this.loggedinUser.getUsername() + ")", ""));
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
hiberSession.flush();
hiberSession.close();
}
context.addCallbackParam("isLoggedIn", isLoggedin);
context.addCallbackParam("isAdmin", isAdmin);
context.addPartialUpdateTarget("growl");
}
public void doLogout() {
this.loginUser.setLoggedUser(null);
this.isAdmin = false;
this.isLoggedin = false;
}
public void registerLogin(User user) {
setUsername(user.getUsername());
setPassword(user.getPassword());
doLogin();
}
XML 部分在這里:
<p:outputPanel id="loginOut">
<p:panel id="loginPanel" header="User Login" align="center"
rendered="#{LoggedUser.getLoggedUser == null}"
style="text-align:center; margin-bottom:10px;">
<h:form>
<div style="padding: 6px">
<div>
<table style="width: 100%; text-align: left;"
align="left">
<tr>
<td><h:outputLabel for="loginusername"
value="Username : " /></td>
</tr>
<tr>
<td><p:inputText id="loginusername"
value="#{UserLogin.username}" size="26"
label="Name and Surname" /> <p:watermark
for="loginusername" value="Username" /></td>
</tr>
<tr>
<td><h:outputLabel for="loginpassword"
value="Password : " /></td>
</tr>
<tr>
<td><p:keyboard id="loginpassword"
value="#{UserLogin.password}" size="26"
label="Password" password="true" keypadOnly="false" />
<p:watermark for="loginpassword" value="Password" /></td>
</tr>
</table>
</div>
<div>
<table style="width: 100%; padding-top: 5px;">
<tr>
<td><p:commandButton value="Register"
image="ui-icon ui-icon-disk" id="loginLink"
onclick="regdlg.show()" title="login">
</p:commandButton></td>
<td style="text-align: right; padding-right: 5px;">
<p:commandButton value="Login"
image="ui-icon ui-icon-arrow-1-ne"
actionListener="#{UserLogin.doLogin()}"
update="userPanelOut, adminPanelOut, loginmessage"
oncomplete="handleLogin(xhr, status, args)" />
</td>
</tr>
</table>
</div>
<div
style="padding-left: 5px; padding-bottom: 6px; padding-top: 6px; text-align: left;">
<p:commandLink value="I lost my password!"
onclick="recdlg.show()">
</p:commandLink>
</div>
<div style="padding-bottom: 6px; padding-top: 6px">
<h:outputText id="loginmessage"
value="#{UserLogin.message}"> </h:outputText>
</div>
</div>
</h:form>
</p:panel>
</p:outputPanel>
<p:outputPanel id="userPanelOut">
<p:panel id="rightUserPanel" header="User Panel"
align="center"
style="text-align:center; margin-bottom:10px;"
rendered="#{LoggedUser.getLoggedUser != null}">
<div style="padding: 6px; height: auto; text-align: left;">
<ul class="ymenu">
<h:form>
<li><p:commandLink value="Edit Profile"
onclick="profedlg.show()"
style="display: block;text-decoration: none;color: #333;
font-size: 14px;padding-left: 18px;padding-right: 18px;
padding-top: 10px;padding-bottom: 5px;
height: 20px;background-color: transparent;">
</p:commandLink></li>
<h:form>
<li><h:commandLink value="Logout"
action="#{UserLogin.doLogout()}" oncomplate="refreshPage(xhr, status, args)"
style="display: block;text-decoration: none;color: #333;
font-size: 14px;padding-left: 18px;padding-right: 18px;
padding-top: 10px;padding-bottom: 5px;
height: 20px;background-color: transparent;">
<f:ajax execute="" render="@all" />
</h:commandLink></li>
</h:form>
</h:form>
</ul>
</div>
</p:panel>
</p:outputPanel>
<p:outputPanel id="adminPanelOut">
<p:panel id="rightAdminPanel" header="Administration"
align="center"
style="text-align:center; margin-bottom:10px; text-align:center;"
rendered="#{LoggedUser.getLoggedUser != null}">
<div style="padding: 6px; height: auto; text-align: left;">
<ul class="ymenu">
<li><a href="edit_user.jsf">Users</a></li>
<li><a href="edit_category.jsf">Categories</a></li>
<li><a href="#">Posts</a></li>
<li><a href="#">Comments</a></li>
<li><a href="#">General Settings</a></li>
</ul>
</div>
</p:panel>
</p:outputPanel>
<p:outputPanel id="rightMenuOut">
<p:panel id="rightCategoriesPanel" header="Categories"
align="center"
style="text-align:center; margin-bottom:10px;">
<div style="padding: 6px; height: auto; text-align: left;">
<ul class="ymenu">
<ui:repeat var="category" value="#{ListCategories.catList}">
<li><h:outputLink
value="#{ListCategories.getURL(category)}"
style="display: block;text-decoration: none;color: #333;
font-size: 14px;padding-left: 18px;padding-right: 18px;
padding-top: 10px;padding-bottom: 5px;
height: 20px;background-color: transparent;">
<h:outputText value="#{category.name}" />
</h:outputLink></li>
</ui:repeat>
<li><a href="category.jsf?name=Uncategorized">Uncategorized</a></li>
</ul>
</div>
</p:panel>
</p:outputPanel>
可能有更好的方法,但您可以嘗試使用 JSTL 標簽,例如<c:set name="name" value="john"/>
。
但是請注意,您需要記住何時評估 JSTL 標記處理程序以及何時開始使用 JSF。這里有一篇文章應該可以幫助您入門: https : //rogerkeays.com/jsf-c-foreach-vs -ui-重復
此外,您可能想告訴我們為什么不能多次調用 bean。 您的設計可能存在您可以解決的問題。 例如,盡量不要在 getter 中計算事物,因為它們可能會被多次調用,即使是頁面上的單個表達式。 將這些計算/日期檢索移動到只執行一次的操作。 或者使用一個標志來檢查數據是否已經被計算/檢索。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.