如何在运行时不使用@PostContruct的情况下使用数据库中的值初始化inputtextfield? [英] How to initialize inputtextfield with a value from database on runtime without the use of @PostContruct?

查看:103
本文介绍了如何在运行时不使用@PostContruct的情况下使用数据库中的值初始化inputtextfield?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很清楚

Any scoped managed bean method annotated with @PostConstruct will be called
after the managed bean is instantiated, but before the bean is placed in scope.

考虑

<h:inputText binding="#{bean.input}" >
</h:inputText>

托管Bean所在的

public class Bean {
    private HtmlInputText input; 
    public PreInitializeBean(){
        input = new HtmlInputText();
        input.setMaxlength(15);
        input.setStyle("background: pink;");
        input.setValue(fetchValueFromDatabase());
    }

    private Object fetchValueFromDatabase() {
        String resultValue = null;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection con = DriverManager.getConnection(
                    "jdbc:oracle:thin:@localhost:1521:xe", "system", "system");


            System.out.println("Connection Object: "+con);
            // retieving data from RESULT table
            PreparedStatement ps = con
                    .prepareStatement("select * from RESULT",
                            ResultSet.TYPE_SCROLL_SENSITIVE,
                            ResultSet.CONCUR_UPDATABLE);

            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                System.out.print("<br>" + rs.getInt(1) + " " + rs.getString(2) + " "
                        + rs.getString(3) + " " + rs.getString(4));
                resultValue = rs.getString(2);
            }

            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultValue;
    }

    public HtmlInputText getInput() {
        return input;
    }

    public void setInput(HtmlInputText input) {
        this.input = input;
    }

}

当我在构造函数中执行初始化工作时,在输入文本字段中什么也没有得到,但是如果我正在执行操作,则会将其放置在标有@PostContruct的方法中,得到期望的值(在输入文本框中输入一个值). br> 将构造函数方法替换为:

I get nothing in the inputtext field when i do the initialisation stuff inside the Contructor, but i get the expected(a value in the inputtext box) if what I am doing I place it in a method marked with @PostContruct.
Replace the constructor method with:

    @PostConstruct
    public void init() {
        input = new HtmlInputText();
        input.setMaxlength(15);
        input.setStyle("background: pink;");
        input.setValue(fetchValueFromDatabase());
    }

@Luiggi似乎在此处提供了一些帮助对我发表的评论.

@Luiggi seems to offer some help here in response to a comment I made.

注意:这也很好.

private String input;

public Bean(){
    this.input= fetchValueFromDatabase();
}

推荐答案

实际上,我无法重现您的问题.这对我来说可以.我使用Mojarra 2.2.8和Apache Tomcat 7.0.47进行了测试.您是否在数据库代码中看到任何错误?是否应用了背景样式?

Actually I couldn't reproduce your issue. It works fine for me. I tested with Mojarra 2.2.8 and Apache Tomcat 7.0.47. Did you see any Errors?`maybe in your Database code? Was the background style applied?

但是我不确定您是否真的需要绑定吗?您也可以尝试以下方法.

However I'm not sure whether you really need the binding? You can also try the following approach.

private String input= fetchValueFromDatabase();

public PreInitializeBean(){
}

private String fetchValueFromDatabase() {
    String resultValue = "preSetValue";
    return resultValue;
}

public String getInput() {
    return input;
}

public void setInput(String input) {
    this.input = input;
}

和xml:

<h:inputText value="#{data.input}" maxlength="15" style="background: pink;">
</h:inputText>

我认为这是比较常规的.

I think this is more conventional.

这篇关于如何在运行时不使用@PostContruct的情况下使用数据库中的值初始化inputtextfield?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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