【问题标题】:The action of a p:commandButton reload the page just once at the first click (or action) when the navigator is fresh started当导航器重新启动时,p:commandButton 的操作仅在第一次单击(或操作)时重新加载页面
【发布时间】:2018-11-27 02:31:35
【问题描述】:

希望你一切都好。

我的 Maven Web 应用程序的登录页面有问题。连接用户时,连接按钮在第一次单击时刷新页面并且不联系我的 ManagedBean,在第二次单击时一切正常。我不得不看到类似的问题,但在我的情况下,它仅在第一次启动浏览器时发生(即当我完全关闭浏览器并重新启动它并尝试第一次连接到应用程序时,问题就出现了,但是当我已经登录过一次,浏览器一直在运行,没问题)。 我已经尝试了一些提出的解决方案,但它们根本不起作用......我想我已经寻求但我希望如果不是这种情况,有人可以指出我的解决方案,如果没有,我希望有人可以有一个关于这个问题的想法。 我正在使用带有 JSF (2.2)/primefaces (6.1) 和 Maven 的 Java EE 7。

这是我的登录表单:

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:p="http://primefaces.org/ui">

<h:head>
    <f:facet name="first">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
        <meta name="apple-mobile-web-app-capable" content="yes" />
        <link type="image/vnd.microsoft.icon" rel="shortcut icon" href="./resources/serenity-layout/images/favicon.ico"/>
    </f:facet>
    <title>#{appBean.appName} #{appBean.appVersion}</title>
    <h:outputScript name="js/layout.js" library="serenity-layout" />
    <h:outputScript name="js/ripple.js" library="serenity-layout" />
</h:head>

<h:body styleClass="login-body">
    <h:form>
        <p:growl id="growl" showDetail="true" autoUpdate="true" life="10000"/>
        <div class="login-panel ui-fluid">
            <div class="login-panel-header">
                <a href="./"><p:graphicImage name="images/logo-slim.png" library="serenity-layout" /></a>
            </div>
            <div class="login-panel-content">
                <div class="ui-g">
                    <div class="ui-g-12">
                        <h1>Connexion à #{appBean.appName} #{appBean.appVersion}</h1>
                    </div>
                    <div class="ui-g-12">
                        <h:panelGroup styleClass="md-inputfield">
                            <p:inputText autocomplete="off" value="#{loginBean.user.username}" required="true" requiredMessage="Votre nom d'utilisateur est requis"/>
                            <label>Nom d'utilisateur</label>
                        </h:panelGroup>
                    </div>
                    <div class="ui-g-12">
                        <h:panelGroup styleClass="md-inputfield">
                            <p:password autocomplete="off" value="#{loginBean.user.password}" required="true" requiredMessage="Votre mot de passe est requis"/>
                            <label>Mot de passe</label>
                        </h:panelGroup>
                    </div>
                    <div class="ui-g-12">
                        <p:commandButton id="saveBtn" action="#{loginBean.login()}" ajax="false" value="Connexion" icon="ui-icon-person" />
                    </div>

                </div>
            </div>
            <div style="border-top: #E3E3E3 solid 1px;text-align: center;">
                <p>Copyright © 2016 - #{appBean.currentYear} | <a href="http://******" target="_blank">****</a>. Tous droits réservés.
                </p>
            </div>
            <div class="login-panel-header">
                <span style="color: whitesmoke;">Avec l'appui du programme</span> <a href="https://******" target="_blank">
                    <p:graphicImage name="images/****.jpg" library="serenity-layout" />
                </a>
            </div>
        </div>
    </h:form>
    <h:outputStylesheet name="css/layout-#{userPreferences.layout}.css" library="serenity-layout" />
</h:body>

</html>

这是我的 ManagedBean:

@ManagedBean
@SessionScoped
public class LoginBean implements Serializable {

    private User user = new User();

    public String login() {
        try {
            Gson gson = new Gson();
            Client client = ClientBuilder.newClient();

            WebTarget target = client.target(contextPath).path("users").path("login");
        String response = target.request(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)
                .post(Entity.json(new Login(user.getUsername(), user.getPassword())), String.class);
        user = gson.fromJson(response, User.class);

        HttpSession session = SessionUtils.getSession();
        session.setAttribute("username", user.getUsername());
        session.setAttribute("token", user.getToken());

        return "dashboard?faces-redirect=true";
    } catch (Exception e) {
        FacesContext.getCurrentInstance().addMessage(
                null,
                new FacesMessage(FacesMessage.SEVERITY_ERROR,
                        "Problème de connexion",
                        "Nom d'utilisateur ou mot de passe incorrect"));
    }
    return null;
}

public String logout() {
    HttpSession session = SessionUtils.getSession();
    try {
        Client client = ClientBuilder.newClient();
        WebTarget target = client.target(contextPath).path("usertasks").path("unclaim").path("all");
        String response = target.request().accept(MediaType.APPLICATION_JSON)
                .header("Authorization", "Bearer " + session.getAttribute("token"))
                .method("put", String.class);
        return "login?faces-redirect=true";
    } catch (Exception e) {
        System.err.println(e.getMessage());
    } finally {
        session.invalidate();
    }
    return null;
}

public void keepSessionAlive() {
    FacesContext fc = FacesContext.getCurrentInstance();
    HttpServletRequest request = (HttpServletRequest) fc.getExternalContext().getRequest();
    request.getSession();
}

public void onIdle() {
    RequestContext.getCurrentInstance().execute("PF('timeoutDialog').show()");
}

public User getUser() {
    return user;
}

public void setUser(User user) {
    this.user = user;
}
}

【问题讨论】:

  • 确定您没有任何安全过滤器吗?下次,请尝试通过删除所有不相关的代码来创建minimal reproducible example,同时仍然能够重现问题
  • 你的标题令人困惑......听起来它只在第一次点击时有效,但实际上它只在第一次点击后有效,对吧?请阅读 How to Ask 并说明您尝试/发现的内容/... 它是否适用于普通的 jsf h:commandButton 和输入?
  • 感谢您的回答。你是对的,它只有在第一次点击后才有效。

标签: jsf primefaces jsf-2.2


【解决方案1】:

您在哪个服务器上运行?

我的 web 应用程序也遇到了同样的问题,在 Tomcat 上运行时也遇到了 JEE 7 + Primefaces(由于服务器上的给定环境,我不得不使用它)。

切换到真正的 JEE 应用程序服务器 (WildFly) 后,它无需修复代码中的某些内容即可工作。我没有进一步调查,但也许它也适合你。

顺便说一句,如果你使用 JEE 7,你应该避免使用旧的 JSF 特定的 bean、注入、作用域,而是使用 CDI 注释(所以使用 @Named 和 javax.enterprise 中的 @SesscionScoped 代替 @ManagedBean。上下文包)。

【讨论】:

  • 并为 JSF 使用“现代”命名空间,可能会链接到一些 Stackoverflow 相关文章以获取所有建议。他们在那里。
  • 我正在使用widlfly服务器
猜你喜欢
  • 2013-10-25
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 2015-11-05
  • 2014-03-25
  • 1970-01-01
  • 2013-03-21
  • 2022-11-10
相关资源
最近更新 更多