【问题标题】:<p:commandButton> doesn't navigate when actionListener=“#{bean.method}” is declared<p:commandButton> 在声明 actionListener="#{bean.method}" 时不导航
【发布时间】: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


【解决方案1】:

动作侦听器不应该执行业务逻辑和导航。他们应该监听动作事件。业务逻辑和导航应该在真正的操作方法中完成。

替换

<p:commandButton ... actionListener="#{login.login}" />
public String login(ActionEvent actionEvent) {}

通过

<p:commandButton ... action="#{login.login}" />
public String login() {}

一切都会好起来的。

另见:

【讨论】:

  • 谢谢 BalusC。你有所有的答案!!我设法解决了这个问题,但不知道它为什么会起作用。现在我愿意。如果你有时间看的话,我在这个 [stackoverflow.com/questions/19677288/… 上还有一个关于 primefaces 的问题。再次感谢 BalusC!!!!
猜你喜欢
  • 1970-01-01
  • 2014-03-18
  • 1970-01-01
  • 1970-01-01
  • 2015-01-22
  • 1970-01-01
  • 1970-01-01
  • 2014-12-26
  • 1970-01-01
相关资源
最近更新 更多