如何发送表单输入值并调用 JSF bean 中的方法 [英] How to send form input values and invoke a method in JSF bean

查看:22
本文介绍了如何发送表单输入值并调用 JSF bean 中的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

现在我必须执行插入语句,但我不知道如何读取像 这样的 JSF 输入组件的 value 并发送它给我执行插入的 bean.

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.

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

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

推荐答案

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

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.

鉴于此 JSF 表单示例,其中包含一个输入字段和一个选择字段:

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>

以下 bean 将提交的值打印到标准输出,证明 JSF 早在您在 action 方法中访问它之前就已经设置了这些值.

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;
    }
}

仅此而已.将常规表单转换为 ajax 表单是在命令组件中嵌套一个 的问题,如下所示.

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>

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

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

请注意,无论您打算对提交的值做什么,都超出了 JSF 的责任.例如,操作它,传递给另一个类,将它保存在数据库中等等.这些都与JSF无关.它作为一个基于 HTML 表单的框架已经完成了它的工作,以可用 Java 变量的形式为您提供提交的值.剩下的由你决定.

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.

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

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.

这篇关于如何发送表单输入值并调用 JSF bean 中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆