带支撑梁的 JSF 复合组件

发布于 2024-10-27 03:57:55 字数 3599 浏览 2 评论 0原文

我正在尝试让复合组件与它自己的支持 bean 一起工作, 使用 Core JSF 3 书中 p375 ​​上的示例,但只得到 NPE。问题似乎出在encodeBegin()开始处,Date date = (Date) getValue()返回null。 老实说,我真的不明白该组件的价值应该在哪里 被存储时,我使用 cc:attribute type= 将其指定为 java.util.Date,但我 不太明白这是怎么回事: public Object getSubscribedValue() { return this; }- 它将返回一个 InputDateBean 类的实例 - 结果是一个日期。我总体上很好,但对它应该如何工作感到困惑。

与书中的示例不同,我尝试使用支持组件进行临时存储, 因此,当输入日期时,我尝试将其存储在#{cc.day}中,在书中,由于某种原因,他们使用应用程序范围的bean。

感谢您的任何帮助。我正在使用 Mojarra 2.1。

输入日期.xhtml

<cc:interface componentType="uk.co.myco.jsfbeans.sqcc.InputDateBean">
    <cc:attribute name="value" type="java.util.Date"/>
</cc:interface>

<cc:implementation>
    <h:panelGrid columns="3">
        <h:inputText id="day" value="#{cc.day}"
                     converter="javax.faces.Integer"/>
        <h:inputText id="month" value="#{cc.month}"
                     converter="javax.faces.Integer"/>
        <h:inputText id="year" value="#{cc.year}"
                     converter="javax.faces.Integer"/>
    </h:panelGrid>
</cc:implementation>

输入日期Bean.java

package uk.co.myco.jsfbeans.sqcc;


import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import javax.faces.component.FacesComponent;
import java.util.GregorianCalendar;
import javax.faces.application.FacesMessage;
import javax.faces.component.NamingContainer;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.convert.ConverterException;
import uk.co.myco.general.SQLog;
import uk.co.myco.jsfbeans.helper.Messages;

@FacesComponent(value = "uk.co.myco.jsfbeans.sqcc.InputDateBean")
public class InputDateBean extends UIInput implements NamingContainer {

    private int day = 0, month  = 0, year = 0;

    public InputDateBean() {
    }

    @Override
    public String getFamily() {
        return "javax.faces.NamingContainer";
    }

    @Override
    public void encodeBegin(FacesContext context) throws IOException {
        Date date = (Date) getValue();
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        UIInput dayComponent = (UIInput) findComponent("day");
        UIInput monthComponent = (UIInput) findComponent("month");
        UIInput yearComponent = (UIInput) findComponent("year");
        dayComponent.setValue(cal.get(Calendar.DATE));
        monthComponent.setValue(cal.get(Calendar.MONTH) + 1);
        yearComponent.setValue(cal.get(Calendar.YEAR));
        super.encodeBegin(context);
    }

    @Override
    public Object getSubmittedValue() {
        return this;
    }

    @Override
    protected Object getConvertedValue(FacesContext context, Object newSubmittedValue)
            throws ConverterException {
        UIInput dayComponent = (UIInput) findComponent("day");
        UIInput monthComponent = (UIInput) findComponent("month");
        UIInput yearComponent = (UIInput) findComponent("year");
        int lday = (Integer) dayComponent.getValue();
        int lmonth = (Integer) monthComponent.getValue();
        int lyear = (Integer) yearComponent.getValue();
        if (isValidDate(lday, lmonth, lyear)) {
            return new GregorianCalendar(lyear, lmonth - 1, lday).getTime();
        } else {
            FacesMessage message = Messages.getMessage("util.messages", "invalidDate", null);
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ConverterException(message);
        }
    }
    // getters & setters & isValidDate() removed
}

I'm trying to get a composite component working with it's own backing bean,
using the example on p375 from the Core JSF 3 book, but just get an NPE. The problem seems to be at the start of encodeBegin(), Date date = (Date) getValue() returns null.
If I'm honest I don't really understand where the value of the component is supposed to
be getting stored, I specify it as a java.util.Date using cc:attribute type=, but I
don't really understand how this: public Object getSubmittedValue() { return this; } -
which is going to return an instance of an InputDateBean class - results in a Date. I am generally good and confused by how this is supposed to work.

Unlike the book example I am trying to the use backing component for temporary storage,
so when the day is input I try to store it in #{cc.day}, in the book they use an application scoped bean for some reason.

Thanks for any help. I am using Mojarra 2.1.

inputDate.xhtml

<cc:interface componentType="uk.co.myco.jsfbeans.sqcc.InputDateBean">
    <cc:attribute name="value" type="java.util.Date"/>
</cc:interface>

<cc:implementation>
    <h:panelGrid columns="3">
        <h:inputText id="day" value="#{cc.day}"
                     converter="javax.faces.Integer"/>
        <h:inputText id="month" value="#{cc.month}"
                     converter="javax.faces.Integer"/>
        <h:inputText id="year" value="#{cc.year}"
                     converter="javax.faces.Integer"/>
    </h:panelGrid>
</cc:implementation>

InputDateBean.java

package uk.co.myco.jsfbeans.sqcc;


import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import javax.faces.component.FacesComponent;
import java.util.GregorianCalendar;
import javax.faces.application.FacesMessage;
import javax.faces.component.NamingContainer;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.convert.ConverterException;
import uk.co.myco.general.SQLog;
import uk.co.myco.jsfbeans.helper.Messages;

@FacesComponent(value = "uk.co.myco.jsfbeans.sqcc.InputDateBean")
public class InputDateBean extends UIInput implements NamingContainer {

    private int day = 0, month  = 0, year = 0;

    public InputDateBean() {
    }

    @Override
    public String getFamily() {
        return "javax.faces.NamingContainer";
    }

    @Override
    public void encodeBegin(FacesContext context) throws IOException {
        Date date = (Date) getValue();
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        UIInput dayComponent = (UIInput) findComponent("day");
        UIInput monthComponent = (UIInput) findComponent("month");
        UIInput yearComponent = (UIInput) findComponent("year");
        dayComponent.setValue(cal.get(Calendar.DATE));
        monthComponent.setValue(cal.get(Calendar.MONTH) + 1);
        yearComponent.setValue(cal.get(Calendar.YEAR));
        super.encodeBegin(context);
    }

    @Override
    public Object getSubmittedValue() {
        return this;
    }

    @Override
    protected Object getConvertedValue(FacesContext context, Object newSubmittedValue)
            throws ConverterException {
        UIInput dayComponent = (UIInput) findComponent("day");
        UIInput monthComponent = (UIInput) findComponent("month");
        UIInput yearComponent = (UIInput) findComponent("year");
        int lday = (Integer) dayComponent.getValue();
        int lmonth = (Integer) monthComponent.getValue();
        int lyear = (Integer) yearComponent.getValue();
        if (isValidDate(lday, lmonth, lyear)) {
            return new GregorianCalendar(lyear, lmonth - 1, lday).getTime();
        } else {
            FacesMessage message = Messages.getMessage("util.messages", "invalidDate", null);
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ConverterException(message);
        }
    }
    // getters & setters & isValidDate() removed
}

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

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

发布评论

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

评论(1

萌吟 2024-11-03 03:57:55

我现在看到了我的错误。问题是复合组件必须是
使用 Date 对象调用,即 作为
代码代表日期需要实例化,但事实并非如此。越坚固
这样做的方法是在encodeBegin()中执行一个新的Date(),如果
getValue() 为空。这然后工作相同 ah:inputText/f:convertDateTime
这不需要实例化该值。

I now see my mistake. The problem was that the composite component has to be
called with a Date object, i.e. <cclib:inputDate value="#{bean.date}"/>. As the
code stands the date needs to be instantiated, but it wasn't. The more robust
way of doing this is to do a new Date() in encodeBegin() in the event that
getValue() is null. This then works the same a h:inputText/f:convertDateTime
which does not require that the value is instantiated.

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