gpt4 book ai didi

java - 如何使命令按钮在 JSF 2.0 的窗体外工作

转载 作者:行者123 更新时间:2023-11-30 09:29:54 24 4
gpt4 key购买 nike

我是 JSF 编程的初学者,所以我有一个简单的问题。

我有以下 jsf 文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">

<h:head>
<title>Insert Title Here</title>
</h:head>

<body>
<h:form>
<h:panelGrid columns="3">

/* Here I take the username */

<h:outputLabel for="username">Username</h:outputLabel>
<h:inputText id="username" value="#{register.user.username}"
required="true" requiredMessage="Enter username">
<f:ajax event="blur" render="usernameMessage" />
</h:inputText>
<h:message id="usernameMessage" for="username" />

/* Here I take the password */

<h:outputLabel for="password">Password</h:outputLabel>
<h:inputSecret id="password" value="#{register.user.password}"
required="true" redisplay="true" requiredMessage="Enter password">
<f:ajax event="blur" render="passwordMessage" />
</h:inputSecret>
<h:message id="passwordMessage" for="password" />

/* Here I take the email */

<h:outputLabel for="email">Email</h:outputLabel>
<h:inputText id="email" value="#{register.user.email}"
required="true" requiredMessage="Enter email">
<f:ajax event="blur" render="emailMessage" />
</h:inputText>
<h:message id="emailMessage" for="email" />

/* And these are my 3 command buttons, and they are working only when the username, password and email are filled in. */

<h:panelGroup>
<h:commandButton value="Register" action="#{register.submit}">
<f:ajax execute="@form" render="@form" />
</h:commandButton>
<h:commandButton value="setDB" action="#{register.setDB}">
<f:ajax execute="@form" render="@form" />
</h:commandButton>
<h:commandButton value="getDB" action="#{register.getDB}">
<f:ajax execute="@form" render="@form" />
</h:commandButton>
<h:commandButton value="reset" action="#{register.reset}">
<f:ajax execute="@form" render="@form" />
</h:commandButton>
</h:panelGroup>

<h:messages globalOnly="true" layout="table" />
</h:panelGrid>
</h:form>

/* And this is the button that I want to work without the need to fill in the username, password and email */

<h:panelGroup rendered="#{register.user.ini!=true}">
<h3>Result</h3>
<h:commandButton value="getDB" action="#{register.getDB}" />
</h:panelGroup>

</body>
</html>

所以我的问题是如何使以下命令按钮起作用:

<h:panelGroup rendered="#{register.user.ini!=true}">
<h3>Result</h3>
<h:commandButton value="getDB" action="#{register.getDB}" />
</h:panelGroup>

当我启动应用程序时它没有出现。我只想获取数据库信息,而无需填写 :) 中的字段 ... 其他按钮在表单中,只有当字段的用户名、密码和电子邮件包含内容时它们才有效。此代码既不会产生异常也不会产生错误,但按钮不起作用(表单外的按钮)。

最佳答案

不能使用h:commandButton,除非它在h:form

我不确定您要实现什么目标,但请看一下这个 PreRenderViewEvent

此外,如果你想在创建 bean 时运行一些逻辑,你可以在你的 bean 中使用 @PostConstruct

@PostConstruct
public void init() {
retrieveDB();
}

此外,为 bean 操作方法提供 get/set 前缀是一种非常糟糕的做法,get/set 最好仅用于 geters/setter

关于java - 如何使命令按钮在 JSF 2.0 的窗体外工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13469105/

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