如何发送表单输入值并调用 JSF bean 中的方法

发布于 2024-09-18 15:59:24 字数 296 浏览 8 评论 0原文

我正在构建一个 JSF 应用程序。我定义了 GUI 并使用 select 执行 select 语句查询数据库。

现在我必须执行插入语句,但我不知道如何读取 JSF 输入组件(如 )的 value 并将其发送到我的执行插入的 bean。

值是否应该通过 faces-config.xml 映射,以便我可以在 Java 代码中使用它?

I am building a JSF application. I defined the GUI and did the select statements query the database using select.

Now I must do the insert statements, but I don't know how to read the value of a JSF input component like <h:inputText> and send it to my bean which performs the insert.

Should <h:inputText> value be mapped through faces-config.xml, so I can have it in my Java code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水晶透心 2024-09-25 15:59:24

您需要将所有 / 组件放入 中并绑定它们的 value 属性通过像 #{bean.property} 这样的 EL 表达式传递给 bean 属性,并由 getter/setter 对支持。如果设置正确,当通过同一个表单中的 组件提交表单时,JSF 将自动设置 bean 中的值。您可以通过 EL 表达式(如 #{bean.action} 组件的 action 属性中指定 bean 操作方法>,它指向裸方法 action()。所有提交的值都可以按照通常的 Java 方式立即获得。

给定这个包含一个输入字段和一个选择字段的 JSF 表单示例:

<h:form>
    <h:inputText value="#{bean.text}" required="true" />
    <h:selectOneMenu value="#{bean.choice}" required="true">
        <f:selectItem itemValue="#{null}" />
        <f:selectItem itemValue="One" />
        <f:selectItem itemValue="Two" />
        <f:selectItem itemValue="Three" />
    </h:selectOneMenu>
    <h:commandButton value="submit" action="#{bean.submit}" />
    <h:messages />
    <h:outputText value="#{bean.result}" />
</h:form>

以下 bean 将提交的值打印到 stdout,证明 JSF 早在您在操作方法中访问它之前就已经设置了这些值。

package com.example;

import javax.inject.Named;
import javax.enterprice.context.RequestScoped;

@Named // Use @javax.faces.bean.ManagedBean on outdated environments.
@RequestScoped // Use @javax.faces.bean.RequestScoped on outdated environments.
public class Bean {

    private String text;
    private String choice;
    private String result;

    public void submit() {
        result = "Submitted values: " + text + ", " + choice;
        System.out.println(result);
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getChoice() {
        return choice;
    }

    public void setChoice(String choice) {
        this.choice = choice;
    }

    public String getResult() {
        return result;
    }
}

就这样。将常规形式转换为 ajax 形式只需在命令组件中嵌套 即可,如下所示。

<h:commandButton value="submit" action="#{bean.submit}">
    <f:ajax execute="@form" render="@form" />
</h:commandButton>

您可以在我们的 JSF wiki 页面底部找到另一个示例和有价值的链接。

请注意,无论您打算如何处理提交的值,JSF 都不负责。例如,操作它、传递到另一个类、将其保存在数据库中等等。所有这些都与 JSF 无关。作为一个基于 HTML 表单的框架,它已经完成了为您提供可用 Java 变量形式提交的值的工作。剩下的就看你的了。

为了研究下一步,此时您应该就好像您已经准备好/硬编码变量而不是整个基于 JSF 的用户界面一样。例如,为了保存数据库中的值,人们通常使用像EJB这样的业务服务层框架,而EJB又使用像JPA这样的持久层框架。有些人甚至使用“plain vanilla”JDBC 来实现这一点。有关具体示例的更多链接,请从此处开始:JSF 控制器、服务和 DAO

You need to put all <h:inputXxx>/<h:selectXxx> components in a <h:form> and bind their value attribute to a bean property via an EL expression like #{bean.property}, backed by a getter/setter pair. When properly set, JSF will automatically set the values in the bean when the form is submitted via a <h:commandXxx> component in the very same form. You can specify a bean action method in action attribute of the <h:commandXxx> component via an EL expression like #{bean.action}, which points to the bare method action(). All submitted values are available right away there the usual Java way.

Given this JSF form example with one input field and one select field:

<h:form>
    <h:inputText value="#{bean.text}" required="true" />
    <h:selectOneMenu value="#{bean.choice}" required="true">
        <f:selectItem itemValue="#{null}" />
        <f:selectItem itemValue="One" />
        <f:selectItem itemValue="Two" />
        <f:selectItem itemValue="Three" />
    </h:selectOneMenu>
    <h:commandButton value="submit" action="#{bean.submit}" />
    <h:messages />
    <h:outputText value="#{bean.result}" />
</h:form>

The following bean prints the submitted values to the stdout, proving that JSF has already set the values long before the moment you access it in the action method.

package com.example;

import javax.inject.Named;
import javax.enterprice.context.RequestScoped;

@Named // Use @javax.faces.bean.ManagedBean on outdated environments.
@RequestScoped // Use @javax.faces.bean.RequestScoped on outdated environments.
public class Bean {

    private String text;
    private String choice;
    private String result;

    public void submit() {
        result = "Submitted values: " + text + ", " + choice;
        System.out.println(result);
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getChoice() {
        return choice;
    }

    public void setChoice(String choice) {
        this.choice = choice;
    }

    public String getResult() {
        return result;
    }
}

That's all. Turning the regular form into an ajax form is a matter of nesting a <f:ajax> in the command component as below.

<h:commandButton value="submit" action="#{bean.submit}">
    <f:ajax execute="@form" render="@form" />
</h:commandButton>

You can find another example and valuable links at bottom of our JSF wiki page.

Do note that whatever you intend to do with the submitted values is beyond the responsibility of JSF. For example, manipulating it, passing into another class, saving it in database, etc. None of this all is related to JSF. It has as being a HTML form based framework already done its job of providing you the submitted values in flavor of usable Java variables. The remainder is up to you.

To investigate the next step, you should at this point just be doing as if you've already a bunch of prepared / hardcoded variables instead of a whole JSF based user interface. For example, in order save to the values in a database, people usually use a business service layer framework like EJB which in turn uses a persistence layer framework like JPA. Some people even use "plain vanilla" JDBC for that. For more links to concrete examples, start here: JSF Controller, Service and DAO.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文