将javascript创建的动态文本字段绑定到bean

将javascript创建的动态文本字段绑定到bean,javascript,jsf-2,Javascript,Jsf 2,我有下面的代码,这创建了我在textField中输入的textField的数量 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/19

我有下面的代码,这创建了我在textField中输入的textField的数量

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"    
      xmlns:h="http://java.sun.com/jsf/html">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Dashboard | BlueWhale Admin</title>
    <link rel="stylesheet" type="text/css" href="../css/reset.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="../css/text.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="../css/grid.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="../css/layout.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="../css/nav.css" media="screen" />

<script type="text/javascript">
//<![CDATA[
            function BuildFormFields($amount)
            {
                var
                    $container = document.getElementById('FormFields'),
                    $item, $field, $i;

                $container.innerHTML = '';
                for ($i = 0; $i < $amount; $i++) {
                    $item = document.createElement('div');
                    $item.style.margin = '3px';

                    $field = document.createElement('span');
                    $field.innerHTML = '<hr>'+'News '+ ($i+1) +             "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                    $field.style.marginRight = '10px';
                    $item.appendChild($field);

                    $field = document.createElement('input');
                    $field.name = 'Design[' + $i + ']';
                    $field.id = 'Design[' + $i + ']';
                    $field.type = 'text';
                    $field.className = 'error';
                    $field.style.width = '500px';
                    $item.appendChild($field);


                    $container.appendChild($item);
                }
            }

        function saveNews()  
        {  
            var value=document.getElementById("noOfNews").value;

            if(value==="")  
            {  
               alert("Please Enter Some Data");
            } 
            else
            {
                var $no = parseInt(value),$i;
                for($i=0;$i < $no; $i++ )
                {
                    var value1=document.getElementById( 'Design[' + $i + ']' ).value;


                    if(value1==="")
                    {
                    alert("Please enter Data");
                    break;
                    }
                }
                if($no===$i)
                {
                    alert("Data Saved !!!");
                }
        }
    }  
        //]]>
        </script>
        </head>
<body>
Number Of News Item To be Displayed <input id="noOfNews" type="text" onkeyup="BuildFormFields(parseInt(this.value, 10));" />

            <div id="FormFields" style="margin: 20px 0px;"></div>
<button class="btn btn-blue" onclick="saveNews()">Save</button>
</body>
</html> 

仪表板|蓝鲸管理
//
要显示的新闻项目数
拯救
我正在使用JSF2.0,希望将输入的数据保存到数据库中,有什么想法吗

谢谢,
Abhinay

与使用Javascript相比,使用支持bean可以更好地实现这一点。使用ajax,您不需要任何页面刷新。大致如下:

HTML

<h:form>
    <p>
        <h:inputText value="#{bean.noOfFields}" />
        <h:commandButton value="Create fields">
            <f:ajax execute="@form" render="@form" />
        </h:commandButton>
    </p>

    <hr />

    <p>
        <c:forEach items=#{bean.values} varStatus="counter">
            Field no. #{counter.index} <h:inputText value="#{bean.values[counter.index}" /><br />
        </c:forEach>

        <h:commandButton action="#{bean.submit}" value="Save" />
    </p>
</h:form>
注意


如果您想坚持使用keyup事件,您也可以轻松地将其绑定到
。尽管我建议不要这样做,因为每次击键都会调用另一个ajax调用。

您在服务器端bean中尝试了哪些数据?如果您不知道如何实现这一点,请考虑一个<代码> JSF Hello World 示例。我试图给用户一个选项,以尽可能多地添加新闻。一旦他写完新闻,我想把它们存储在数据库中,然后在主页上取回并显示出来。这是管理页面的一部分嗨,很抱歉耽搁了,但我试着用你的方法,它不断给出以下错误。javax.servlet.ServletException:无法创建托管bean新闻。发现以下问题:-找不到托管Bean新闻的Bean或属性类字符串[]。能否在PasteBin(Bean和相关HTML)上共享代码?我来看看;)您需要将
替换为
。当需要Ajax时,这将包括
javax.faces
库中的
jsf.js
但是通过设置
noOfFields=“1”,您将值
“1”
保留在您的文本字段中。我建议保持这些不变,即使只是为了用户的简单性。我想问题在于faces-congig.xml,其中值String[]String[]或java.lang.String都不适用于“values”托管属性
@ManagedBean
@ViewScoped
public class Bean {
    private String noOfFields = 1;
    private String[] values = new String[1];

    public void submit() {
        // save values in database
    }

    public String getNoOfFields() {
        return noOfFields;
    }

    public void setNoOfFields(String noOfFields) {
        try {
            values = new String[Integer.valueOf(noOfFields)];
            this.noOfFields = noOfFields;
        catch(NumberFormatException ex) {
            values = new String[1];
            noOfFields = "1";
        }
    }

    public String[] getValues() {
        return values;
    }
}