gpt4 book ai didi

jsf -

doesn't navigate when actionListener =“#{bean.method}” is declared

转载 作者:行者123 更新时间:2023-12-04 13:47:52 25 4
gpt4 key购买 nike

我正在尝试创建一个页面,该页面允许用户登录系统然后导航到主页。我已经设法让它做一个或另一个,但无法弄清楚如何让它做两个。我已经爬过所有网站,但找不到合适的答案。请帮忙。我的代码如下:
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>

当我尝试这样做时,我将登录方法的返回值更改为“登录”。

提前感谢您的任何帮助!!

最佳答案

Action 监听器不应该做业务逻辑和导航。他们应该监听 Action 事件。业务逻辑和导航应该以真正的操作方法完成。
代替

<p:commandButton ... actionListener="#{login.login}" />
public String login(ActionEvent actionEvent) {}
经过
<p:commandButton ... action="#{login.login}" />
public String login() {}
一切都应该很好。
也可以看看:
  • Differences between action and actionListener
  • 关于jsf - <p :commandButton> doesn't navigate when actionListener =“#{bean.method}” is declared,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19250262/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com