【发布时间】:2013-10-15 12:39:04
【问题描述】:
我正在尝试创建一个允许用户登录系统然后导航到主页的页面。我已经设法让它做一个或另一个,但不知道如何让它做这两个。我爬遍了所有网站,找不到合适的答案。请帮忙。我的代码如下: XHTML:
<h:form>
<p:growl id="growl" showDetail="true" life="3000" />
<h:panelGrid columns="2" cellpadding="5">
<h:outputLabel for="username" value="Username: " />
<p:inputText value="#{login.username}" id="username" required="true"
label="username" />
<h:outputLabel for="password" value="Password: " />
<h:inputSecret value="#{login.password}" id="password" required="true"
label="password" />
<p:commandButton ajax="false" id="loginButton" value="Login"
update="growl" actionListener="#{login.login}" />
</h:panelGrid>
</h:form>
Java 类:
@ViewScoped
@ManagedBean
@SessionScoped
public class Login implements Serializable
{
private static final long serialVersionUID = 1L;
private String username;
private String password;
public String login(ActionEvent actionEvent)
{
RequestContext context = RequestContext.getCurrentInstance();
FacesMessage msg = null;
boolean loggedIn = false;
if(username != null && username.equals("admin") && password != null && password.equals("admin"))
{
loggedIn = true;
msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Welcome", username);
}
else
{
loggedIn = false;
msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Login Error", "Invalid Credentials");
}
FacesContext.getCurrentInstance().addMessage(null, msg);
context.addCallbackParam("loggedIn", loggedIn);
FacesContext context2 = FacesContext.getCurrentInstance();
context2.getExternalContext().getFlash().setKeepMessages(true);
return "ProEJT?faces-redirect=true";
}
这里要求的是我的 faces-config.xml:
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<navigation-rule>
<from-view-id>/login.xhtml</from-view-id>
<navigation-case>
<from-action>#{login.login}</from-action>
<from-outcome>loggedin</from-outcome>
<to-view-id>/ProEJT.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
当我尝试这样做时,我将登录方法的返回值更改为“loggedin”。
提前感谢您的帮助!
【问题讨论】:
-
什么是jsf和primefaces版本?
-
嗨,我正在使用 JSF 2.1 和 PrimeFaces 3.4。我已经看到了类似的建议,但是,它对我不起作用。我将使用我尝试的 faces-config.xml 编辑我的问题
-
我忘了说我也在使用 TomCat 7。谢谢
-
一些提示:使用非 ajax 命令按钮时,请使用
action而不是actionListener。不要同时使用隐式导航 (return "ProEJT?faces-redirect=true") 和 faces-config 导航规则,它们是多余的。您已将托管 bean 声明为@ViewScoped和@SessionScoped,应该是什么?
标签: jsf primefaces facelets