< p:commandButton>声明actionListener =“#{bean.method}"时无法导航 [英] <p:commandButton> doesn't navigate when actionListener=“#{bean.method}” is declared
问题描述
我正在尝试创建一个页面,该页面允许用户登录到系统,然后导航到主页.我设法让它可以做一个或另一个,但无法弄清楚如何让它同时做.我已经浏览了所有站点,但找不到合适的答案.请帮忙.我的代码如下: XHTML:
I am trying to create a page which allows a user to logon to the system and then navigates to the homepage. I have managed to get it to do one or the other but cannot work out how to get it to do both. I have crawled through all the sites and cannot find a suitable answer. Please help. My code is as follows: 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:
As Requested here is my 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".
When I attempted this I altered the return from my login method to "loggedin".
谢谢您的帮助!
推荐答案
动作侦听器不应该执行业务逻辑和导航.他们应该听动作事件.业务逻辑和导航应以一种真正的行动方法完成.
Action listeners are not supposed to do business logic and navigation. They are supposed to listen on action events. Business logic and navigation should be done in a true action method.
替换
<p:commandButton ... actionListener="#{login.login}" />
public String login(ActionEvent actionEvent) {}
作者
<p:commandButton ... action="#{login.login}" />
public String login() {}
一切都会好起来的.
这篇关于< p:commandButton>声明actionListener =“#{bean.method}"时无法导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!