栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

通过使用Java Server Faces将多个输入字段绑定到支持bean属性?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

通过使用Java Server Faces将多个输入字段绑定到支持bean属性?

三种方式:

  1. 通过
    java.util.Calendar
    三个getter和三个setter来返回或居中。
  2. 利用,
    Converter
    但是这会有点麻烦。
  3. 利用第3方组件,例如
    rich:calendar

编辑: 根据评论,这是选项2的样子。

page.jsp

<h:form>    <h:selectoneMenu value="#{myBean.date}">        <f:converter converterId="datePartConverter" />        <f:attribute name="part" value="day" />        <f:selectItems value="#{myBean.days}" />    </h:selectOneMenu>    <h:selectoneMenu value="#{myBean.date}">        <f:converter converterId="datePartConverter" />        <f:attribute name="part" value="month" />        <f:selectItems value="#{myBean.months}" />    </h:selectOneMenu>    <h:selectoneMenu value="#{myBean.date}">        <f:converter converterId="datePartConverter" />        <f:attribute name="part" value="year" />        <f:selectItems value="#{myBean.years}" />    </h:selectOneMenu>    <h:commandButton value="submit" action="#{myBean.submit}"/>    <h:messages /></h:form>

mypackage.MyBean

package mypackage;import java.util.ArrayList;import java.util.Date;import java.util.List;import javax.faces.model.SelectItem;public class MyBean {    private static List<SelectItem> days = new ArrayList<SelectItem>();    private static List<SelectItem> months = new ArrayList<SelectItem>();    private static List<SelectItem> years = new ArrayList<SelectItem>();    static {        // Just do your thing to fill them. only ensure that those are Strings,        // else you'll need to change the type in Converter accordingly.        for (int i = 1; i <= 31; i++) days.add(new SelectItem(String.valueOf(i)));        for (int i = 1; i <= 12; i++) months.add(new SelectItem(String.valueOf(i)));        for (int i = 2000; i <= 2020; i++) years.add(new SelectItem(String.valueOf(i)));    }    private Date date;    public void submit() {        // Print submitted date to stdout.        System.out.println("Submitted date: " + date);    }    public List<SelectItem> getDays() {        return days;    }    public List<SelectItem> getMonths() {        return months;    }    public List<SelectItem> getYears() {        return years;    }    public Date getDate() {        return date;    }    public void setDate(Date date) {        this.date = date;    }}

mypackage.DatePartConverter

package mypackage;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Map;import javax.faces.application.FacesMessage;import javax.faces.component.UIComponent;import javax.faces.component.UIInput;import javax.faces.context.FacesContext;import javax.faces.convert.Converter;import javax.faces.convert.ConverterException;public class DatePartConverter implements Converter {    public Object getAsObject(FacesContext context, UIComponent component, String value) {        String part = (String) component.getAttributes().get("part");        Date date = null;        if (context.getRenderResponse()) { // Convert any default/selected date for display. Date selectedDate = (Date) ((UIInput) component).getValue(); if (selectedDate != null) {     if (("day".equals(part) && new SimpleDateFormat("d").format(selectedDate).equals(value))         || ("month".equals(part) && new SimpleDateFormat("M").format(selectedDate).equals(value))         || ("year".equals(part) && new SimpleDateFormat("yyyy").format(selectedDate).equals(value)))     {         date = selectedDate;     } }        } else { // Convert submitted date after submit. Map<String, Object> map = context.getExternalContext().getRequestMap(); if ("day".equals(part)) {     map.put("DatePartConverter.day", value); // Save until we have all parts. } else if ("month".equals(part)) {     map.put("DatePartConverter.month", value); // Save until we have all parts. } else if ("year".equals(part)) {     String day = (String) map.get("DatePartConverter.day");     String month = (String) map.get("DatePartConverter.month");     String dateString = String.format("%s-%s-%s", day, month, value);     try {         date = new SimpleDateFormat("d-M-yyyy").parse(dateString);     } catch (ParseException e) {         throw new ConverterException(new FacesMessage(e.getMessage()), e);     } }        }        return date;    }}    public String getAsString(FacesContext context, UIComponent component, Object value) {        // Not relevant here. Just return SelectItem's value.        return (String) value;    }

faces-config.xml

<converter>    <converter-id>datePartConverter</converter-id>    <converter-class>mypackage.DatePartConverter</converter-class></converter><managed-bean>    <managed-bean-name>myBean</managed-bean-name>    <managed-bean-class>mypackage.MyBean</managed-bean-class>    <managed-bean-scope>request</managed-bean-scope></managed-bean>

请注意,没有,

Validator
并且
SimpleDateFormat
默认情况下是
lenient
。因此,例如选择11月31日将产生12月1日。
DatePartValidator
如果要警告自己,则可能需要自己实施。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/454023.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号