- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 facelets 的新手,我已经使用 netbeans 生成了一个项目,但我在标签上苦苦挣扎。
我有
<h:selectOneMenu id="country" value="#{organisation.organisation.country}" title="Country" >
<f:selectItems value="#{country.countryItemsAvailableSelectOne}"/>
</h:selectOneMenu>
最佳答案
由于您在谈论 Facelets,我将假设 JSF 2.x。
首先,HTML 是一个完整的字符串。 JSF 生成 HTML。默认情况下,非 String
Java 对象来自 toString()
方法转换为他们的String
JSF 生成 HTML 时的表示。在这些 Java 对象和 String
之间正确转换, 你需要一个 Converter
.
我假设您的 Country
对象已经有 equals()
方法properly implemented ,否则验证将在稍后失败,并显示“验证错误:值无效”,因为所选对象不返回 true
在测试 equals()
对于任何可用的项目。
自 #{country}
起,我还将对命名进行一些更改。是一个令人困惑的托管 bean 名称,因为它显然不代表 Country
的实例。类(class)。我叫它Data
它应该保存应用程序范围的数据。
@ManagedBean
@ApplicaitionScoped
public class Data {
private static final List<Country> COUNTRIES = populateItSomehow();
public List<Country> getCountries() {
return COUNTRIES;
}
// ...
}
Country
类有两个属性
code
和
name
.我假设接收所选国家/地区的托管 bean 具有
private Country country
属性(property)。在您的
<f:selectItems>
,你需要循环
#{data.countries}
并将国家对象指定为项目值,将国家名称指定为项目标签。
<h:selectOneMenu value="#{bean.country}">
<f:selectItems value="#{data.countries}" var="country" itemValue="#{country}" itemLabel="#{country.name}" />
</h:selectOneMenu>
Converter
对于
Country
类(class)。我们将根据每个国家/地区唯一的国家/地区代码进行转换(对吗?)。在
getAsString()
您实现将 Java 对象转换为其唯一的字符串表示形式的代码,该表示形式将在 HTML 中使用。在
getAsObject()
您实现将唯一的 HTML 字符串表示形式转换回 Java 对象的代码。
@FacesConverter(forClass=Country.class)
public class CountryConverter implements Converter {
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
return (value instanceof Country) ? ((Country) value).getCode() : null;
}
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (value == null) {
return null;
}
Data data = context.getApplication().evaluateExpressionGet(context, "#{data}", Data.class);
for (Country country : data.getCountries()) {
if (country.getCode().equals(value)) {
return country;
}
}
throw new ConverterException(new FacesMessage(String.format("Cannot convert %s to Country", value)));
}
}
@FacesConverter
将在 JSF 中自动注册它,并且 JSF 将在遇到
Country
的值表达式时自动使用它。类型。最终,您最终将国家代码作为项目值,将国家名称作为项目标签。 JSF 会将提交的国家/地区代码转换回完整的
Country
表单提交时的对象。
h:selectOneMenu
.
关于jsf - F :selectItems showing the class not a value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7353171/
jsf中selectitem和selectitems标签有什么区别? 最佳答案 差异正是您所期望的。 selectitem 标签将单个项目添加到 HTML 列表,而 selectitems 标签添加多
我正在尝试根据查询将 asp:dropdownlist 绑定(bind)到一些站点核心项目。通过查询,我想返回继承模板“站点根”的项目。 当我使用: Sitecore.Context.Database
例如,在某些版本的JSF中,f:selectItems组件不支持title属性。 是否可以使用JSFC将JSF组件替换为纯HTML对应的组件,并执行类似的操作? 代替
我有一个数字列表(范围 500 - 5000,步长 500)。我想添加一个小数点 1000 -> 1.000; 2500 -> 2.500 等,但只是为了不将标签保存为值。我尝试了以下但没有用:
我有一个数字列表(范围 500 - 5000,步长 500)。我想添加一个小数点 1000 -> 1.000; 2500 -> 2.500 等,但只是为了不将标签保存为值。我尝试了以下但没有用:
如何从 List 中删除“abcd”的值. public class Sample { public static void main(String[] args) { List list=
我试图在我的 selectItem 中为每个默认添加一个选项,它绑定(bind)了一个数据源,之后我尝试为每个默认添加一个选项。我的问题是我总是看到数据源中的项目,但从来没有看到我的默认选项。这些是我
我试图让用户从 JSF 的下拉列表中选择一个集合的项目。 这是我正在使用的代码: 这是 MyBean 的代码: @ManagedBean
是否可以使用xpath语法进行基于日期的查询?我研究的一切都表明这是不可能的。我正在查询一组日历,只想取回一个月的数据-我可以使用哪些策略来实现这一目标? 2010年8月10日:编辑以获取更多信息 我
是否可以设置我周围我的链接文本是 itemLabel ? 我正在使用普通的太阳组件。 最佳答案 在 HTML 中不可能获得所需的结果。您需要为此添加一段 JavaScript。 哪里be
我有以下失败的 Facelet 代码: 支持 bean: public class StaticInfoBea
我是 SmartGWT 的新手,尝试以编程方式选择 SelectItem Widget 的项目。但没有找到解决方案。 这就是我想要的: 这是我当前的 SmartGWT 示例代码: SelectIte
我有一个乡村类(class): public class Country{ private Long id; private String name; } 以及具有两个 Count
我一直在研究复合组件,但遇到了一些我无法理解的问题。如果不使用复合组件,我会得到这样的东西: 我基本上将该片段变成了复合组件:
抱歉图片上有捷克语,但这并不重要。 在图片上,您可以在单击其下拉按钮后看到 SelectItem。在此下拉菜单的第一行,您可以看到额外的过滤输入,我需要将其删除。 我认为只有当您使用 DataSour
我正在尝试使用 selectOneMenu 制作过滤器。 我有两个类别,当一个类别被选中时,必须过滤显示的结果,还有第二个类别。 JSF 代码: Filter by topi
这个问题已经有答案了: How to populate options of h:selectOneMenu from database? (5 个回答) 已关闭 4 年前。 目标是正确获取 Sele
我有一个 SelectItem,它从数据库中获取数据。我需要向我选择的项目添加一个空字段。 我的代码看起来像这样,而且效果很好。 SelectItem editor = new SelectItem(
在 ContextMenu shell 扩展中创建文件后,如何在资源管理器中选择它? 我使用 IFileOperation 创建了文件API,并尝试使用 IShellView::SelectItem(
试图将所有演示 Material 保留在该项目的 xhtml 中,我需要将 selectItem 标记中的某些值格式化为具有 BigDecimal 值,并且需要使其看起来像货币。有没有办法申请在 里
我是一名优秀的程序员,十分优秀!