我在部署Login.xhtml时遇到了问题。即使我使用userManagerBean.userName和userManagerBean.passWord来存储用户输入的值,我仍然为我的userName和passWord值获取空值。
任何帮助?
我的代码如下。
谢谢! :)
Login.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<h:outputText value="Username: " />
<p:inputText value="#{userManagerBean.userName}" />
<br></br>
<h:outputText value="Password: " />
<p:password value="#{userManagerBean.passWord}" />
<br></br>
<p:commandButton value="Login" id="ajax">
<p:ajax update="status" listener="#{userManagerBean.loginResult()}" />
</p:commandButton>
<br></br>
<h:outputText id="status" value="#{userManagerBean.loginStatus}" />
</h:form>
</h:body>
</html>
UserManagerBean.java
package managedbean;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.event.ActionEvent;
import javax.inject.Named;
import session.stateless.SystemUserSession;
@Named(value = "userManagerBean")
@RequestScoped
public class UserManagerBean {
@EJB
private SystemUserSession systemUser;
private String userName;
private String passWord;
private String eventNameLength;
private String loginStatus;
/**
* @return the userName
*/
public String getUserName() {
return userName;
}
/**
* @param userName the userName to set
*/
public void setUserName(String userName) {
this.userName = userName;
}
/**
* @return the passWord
*/
public String getPassWord() {
return passWord;
}
/**
* @param passWord the passWord to set
*/
public void setPassWord(String passWord) {
this.passWord = passWord;
}
/**
* @return the eventNameLength
*/
public String getEventNameLength() {
return eventNameLength;
}
/**
* @param eventNameLength the eventNameLength to set
*/
public void setEventNameLength(String eventNameLength) {
this.eventNameLength = eventNameLength;
}
public void buttonAction(ActionEvent actionEvent) {
System.out.println("Welcome to Primefaces!!");
}
public String loginResult()
{
System.out.println(userName +" "+passWord);
if(systemUser.getSystemUser(userName,passWord) == null)
{
System.out.println("No user found");
System.out.println(userName +" "+passWord);
loginStatus = "No user found";
return "No user found";
}
else
{
System.out.println("Successful login");
loginStatus = "Successful login";
return "Successful login";
}
}
/**
* @return the loginStatus
*/
public String getLoginStatus() {
return loginStatus;
}
/**
* @param loginStatus the loginStatus to set
*/
public void setLoginStatus(String loginStatus) {
this.loginStatus = loginStatus;
}
}
SystemUserSession.java
package session.stateless;
import entity.SystemUser;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
@Stateless
@LocalBean
public class SystemUserSession {
@PersistenceContext
private EntityManager entityManager;
public SystemUser getSystemUser(String userName, String passWord) {
Query query = entityManager.createQuery("SELECT u FROM SystemUser u WHERE u.userName = :inUserName AND u.password = :inPassWord");
query.setParameter("inUserName", userName);
query.setParameter("inPassWord", passWord);
SystemUser systemUser = null;
try {
systemUser = (SystemUser) query.getSingleResult();
} catch (NoResultException ex) {
//ex.printStackTrace();
}
return systemUser;
}
}
答案 0 :(得分:2)
p:ajax
默认处理@this
,这意味着inputText
的值永远不会在bean上更新。
此外,p:commandButton
本身具有ajax功能,甚至默认为process="@form"
,这就是你想要的。
所以只需将按钮更改为
即可<p:commandButton value="Login"
id="ajax"
actionListener="#{userManagerBean.loginResult()}"
update="status">
</p:commandButton>
此外我会制作bean @SessionScoped
,因为你需要记住整个会话的loginStatus
。