Java 包含 inputText 的数据表:是否可以使用 JSF 自定义组件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/355614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataTable containing inputText: is it possible with JSF Custom Component
提问by
i'm new to JSF and for the application i'm woriking on i've to do a multiple field input form.
我是 JSF 的新手,对于我正在研究的应用程序,我必须做一个多字段输入表单。
In few words the user should have an inputText when he inputs his data and an "add" button which add a new input text in order to input another data. When the user is done, he pushes another submit button. I looked for something already done, but i haven't found anything, so i decided to create my own custom jsf component
简而言之,用户在输入数据时应该有一个 inputText 和一个“添加”按钮,用于添加新的输入文本以输入另一个数据。当用户完成后,他按下另一个提交按钮。我寻找已经完成的东西,但我什么也没找到,所以我决定创建自己的自定义 jsf 组件
The idea was to create a component combining a dataTable containing an inputText for each row in datatable, plus a button that add a row to the collection binded to the datatable.
这个想法是创建一个组件,它组合了一个包含数据表中每一行的 inputText 的数据表,以及一个按钮,该按钮将一行添加到绑定到数据表的集合中。
I'm looking through the jsf documentation and books, but i'm a bit confused and i'm not sure if is it possible to create such a component... Can somebody help me? TIA
我正在浏览 jsf 文档和书籍,但我有点困惑,我不确定是否可以创建这样的组件......有人可以帮助我吗?TIA
回答by McDowell
It is possible to do everything you want.
有可能做你想做的一切。
You can build something close to what you want using existing controls. A good way to get the most out of JSF is to tailor the model for the view. For example, this view displays edit options that allow you to add values to a data table.
您可以使用现有控件构建接近您想要的内容。充分利用 JSF 的一个好方法是为视图定制模型。例如,此视图显示允许您向数据表添加值的编辑选项。
<f:view>
<h:form>
<h:dataTable value="#{people.list}" var="row">
<h:column>
<f:facet name="header">
<h:outputText value="#" />
</f:facet>
<h:selectBooleanCheckbox value="#{row.selected}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="First Name" />
</f:facet>
<h:inputText value="#{row.firstname}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Last Name" />
</f:facet>
<h:inputText value="#{row.lastname}" />
</h:column>
<f:facet name="footer">
<h:panelGroup>
<h:commandButton value="Add Row" action="#{people.addPerson}" />
<h:commandButton value="Delete Selected"
action="#{people.deleteSelected}" />
<h:commandButton value="Finish" action="#{people.finish}" />
</h:panelGroup>
</f:facet>
</h:dataTable>
</h:form>
</f:view>
People bean:
人豆:
public class People implements Serializable {
private static final long serialVersionUID = 1L;
private List<Person> people = new ArrayList<Person>();
public People() {
// initialise with one entry
people.add(new Person());
}
public List<Person> getList() {
return people;
}
public String addPerson() {
people.add(new Person());
return null;
}
public String deleteSelected() {
Iterator<Person> entries = people.iterator();
while (entries.hasNext()) {
Person person = entries.next();
if (person.isSelected()) {
entries.remove();
}
}
return null;
}
public String finish() {
System.out.println(people);
return "someNavigationRule";
}
}
Person bean:
人豆:
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
private String firstname;
private String lastname;
private transient boolean selected = false;
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
faces-config.xml:
面孔-config.xml:
<managed-bean>
<managed-bean-name>people</managed-bean-name>
<managed-bean-class>addmultiple.People</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
This approach is quite flexible and with some CSS, can look quite good. With a few changes to the view and model, you can have only the last row editable, for example.
这种方法非常灵活,加上一些 CSS,看起来很不错。例如,通过对视图和模型进行一些更改,您可以只编辑最后一行。
If the resultant experience isn't rich enough or if you need something you can reuse, you can create a custom control. It is difficult to get into the specifics without knowing exactly what you want, but you probably would need:
如果由此产生的体验不够丰富,或者如果您需要可以重用的东西,您可以创建自定义控件。在不确切知道您想要什么的情况下很难深入了解细节,但您可能需要:
- A new Rendererfor emitting HTML and decoding form requests.
- (Maybe) a new component, probably extending UIData, and a concrete formfor exposing renderkit-specific (e.g. HTML) attributes.
- A new JSP tag classfor allowing the control to be used in JSPs.
- Definitions for all of the above in a faces-config.xml(see spec).