JSF多选列表框

算法 ww9992019com 发表于 1 周前

www.hj6666.com开户请拨15894452155模特核电站不雅照西安莲湖区dJSg4y<div style="width:100%;float:left;" class="article-content">

                <input type="hidden" name="article_id" id="article_id" value="10147">
                <input type="hidden" name="catid" id="catid" value="0">

                <p>以下代码显示如何创建多选择选择框。<code>&lt;h:selectManyListbox&gt;</code>标签呈现一个类型为“<code>select</code>”的HTML输入元素,并指定其大小和多项目。</p>

<p>以下JSF代码 - </p>

<pre><code class="lang-html">&lt;h:selectManyListbox value="#{userData.data}"&gt;

&lt;f:selectItem itemValue="1" itemLabel="Item 1" /&gt;

&lt;f:selectItem itemValue="2" itemLabel="Item 2" /&gt;

&lt;/h:selectOneListbox&gt;

</code></pre>

<p>被渲染成以下HTML代码 - </p>

<pre><code class="lang-html">&lt;select name="j_idt6:j_idt8" size="2" multiple="multiple"&gt;

&lt;option value="1"&gt;Item 1&lt;/option&gt;

&lt;option value="2"&gt;Item 2&lt;/option&gt;

&lt;/select&gt;

</code></pre>

<h2 id="h2-u5B9Eu4F8B"><a name="%E5%AE%9E%E4%BE%8B" class="reference-link"></a><span class="header-link octicon octicon-link"></span>实例</h2><p>以下是文件: <em>index.xhtml</em> 中的代码 - </p>

<pre><code class="lang-html">&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

&lt;html xmlns="http://www.w3.org/1999/xhtml"

  xmlns:h="http://java.sun.com/jsf/html"

  xmlns:f="http://java.sun.com/jsf/core"

  &gt;

&lt;h:body&gt;

  &lt;h:form&gt;

  Hard-coded with "f:selectItem" :

   &lt;h:selectManyListbox value="#{user.item}"&gt;

     &lt;f:selectItem itemValue="A" itemLabel="Item A" /&gt;

     &lt;f:selectItem itemValue="B" itemLabel="Item B" /&gt;

     &lt;f:selectItem itemValue="C" itemLabel="Item C" /&gt;

   &lt;/h:selectManyListbox&gt;

&lt;br /&gt;&lt;br /&gt;

  &lt;h:commandButton value="Submit" action="result" /&gt;

&lt;h:commandButton value="Reset" type="reset" /&gt;

  &lt;/h:form&gt;

&lt;/h:body&gt;

&lt;/html&gt;

</code></pre>

<p>以下是文件: <em>result.xhtml</em> 中的代码 - </p>

<pre><code class="lang-html">&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

&lt;html xmlns="http://www.w3.org/1999/xhtml"

  xmlns:h="http://java.sun.com/jsf/html"

  xmlns:f="http://java.sun.com/jsf/core"

  xmlns:c="http://java.sun.com/jsp/jstl/core"&gt;

&lt;h:body&gt;

Selected: #{user.itemString}

&lt;/h:body&gt;

&lt;/html&gt;

</code></pre>

<p>以下是文件: <em>UserBean.java</em> 中的代码 - </p>

<pre><code class="lang-java">package com.yiibai;

import java.io.Serializable;

import java.util.Arrays;

import java.util.LinkedHashMap;

import java.util.Map;

import javax.faces.bean.ManagedBean;

import javax.faces.bean.SessionScoped;

@ManagedBean (name="user")

@SessionScoped

public class UserBean implements Serializable{

public String[] item = {"A", "B"};

public String[] getItem() {

return item;

}

public void setItem(String[] i) {

this.item = i;

}

public String getItemString() {

return Arrays.toString(item);

}

}

</code></pre>

<h2 id="h2-selectmanylistbox-"><a name="SelectManyListBox%E5%AE%9E%E4%BE%8B" class="reference-link"></a><span class="header-link octicon octicon-link"></span>SelectManyListBox实例</h2><p>以下是文件: <em>UserBean.java</em> 中的代码 - </p>

<pre><code class="lang-java">package com.yiibai;

import java.io.Serializable;

import java.util.Arrays;

import java.util.LinkedHashMap;

import java.util.Map;

import javax.faces.bean.ManagedBean;

import javax.faces.bean.SessionScoped;

@ManagedBean (name="user")

@SessionScoped

public class UserBean implements Serializable{

public String[] item = {"A", "B"};

public String[] getItem() {

return item;

}

public void setItem(String[] i) {

this.item = i;

}

public String getItemString() {

return Arrays.toString(item);

}

//Generated by Object array

public static class Item{

public String label;

public String value;



public Item(String l, String v){

  this.label = l;

  this.value = v;

}



public String getLabel(){

  return label;

}



public String getValue(){

  return value;

}

}

public Item[] itemList;

public Item[] getItemValue() {

itemList = new Item[3];

itemList[0] = new Item("Item A", "A");

itemList[1] = new Item("Item B", "B");

itemList[2] = new Item("Item C", "C");



return itemList;

}

}

</code></pre>

<p>以下是文件: <em>index.xhtml</em> 中的代码 - </p>

<pre><code class="lang-html">&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

&lt;html xmlns="http://www.w3.org/1999/xhtml"

  xmlns:h="http://java.sun.com/jsf/html"

  xmlns:f="http://java.sun.com/jsf/core"

  &gt;

&lt;h:body&gt;

  &lt;h:form&gt;

  Generated by Object array and iterate with var :

   &lt;h:selectManyListbox value="#{user.item}"&gt;

     &lt;f:selectItems value="#{user.itemValue}" var="f"

     itemLabel="#{f.label}" itemValue="#{f.value}" /&gt;

   &lt;/h:selectManyListbox&gt;



&lt;br /&gt;&lt;br /&gt;

  &lt;h:commandButton value="Submit" action="result" /&gt;

&lt;h:commandButton value="Reset" type="reset" /&gt;

  &lt;/h:form&gt;

&lt;/h:body&gt;

&lt;/html&gt;

</code></pre>

<p>以下是文件: <em>result.xhtml</em> 中的代码 - </p>

<pre><code class="lang-html">&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

&lt;html xmlns="http://www.w3.org/1999/xhtml"

  xmlns:h="http://java.sun.com/jsf/html"

  xmlns:f="http://java.sun.com/jsf/core"

  xmlns:c="http://java.sun.com/jsp/jstl/core"&gt;

&lt;h:body&gt;

Selected: #{user.itemString}

&lt;/h:body&gt;

&lt;/html&gt;

</code></pre>

<h2 id="h2--selectmanylistbox-"><a name="%E9%80%9A%E8%BF%87%E6%98%A0%E5%B0%84%E7%9A%84SelectManyListBox%E5%AE%9E%E4%BE%8B" class="reference-link"></a><span class="header-link octicon octicon-link"></span>通过映射的SelectManyListBox实例</h2><p>以下是文件: <em>UserBean.java</em> 中的代码 - </p>

<pre><code class="lang-java">package com.yiibai;

import java.io.Serializable;

import java.util.Arrays;

import java.util.LinkedHashMap;

import java.util.Map;

import javax.faces.bean.ManagedBean;

import javax.faces.bean.SessionScoped;

@ManagedBean (name="user")

@SessionScoped

public class UserBean implements Serializable{

public String[] item = {"A", "B"};

public String[] getItem() {

return item;

}

public void setItem(String[] i) {

this.item = i;

}

public String getItemString() {

return Arrays.toString(item);

}

private static Map&lt;String,Object&gt; itemValue;

static{

itemValue = new LinkedHashMap&lt;String,Object&gt;();

itemValue.put("Item A", "A"); //label, value

itemValue.put("Item B", "B");

itemValue.put("Item C", "C");

}

public Map&lt;String,Object&gt; getItemValue() {

return itemValue;

}

}

</code></pre>

<p>以下是文件: <em>index.xhtml</em> 中的代码 - </p>

<pre><code class="lang-html">&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

&lt;html xmlns="http://www.w3.org/1999/xhtml"

  xmlns:h="http://java.sun.com/jsf/html"

  xmlns:f="http://java.sun.com/jsf/core"

  &gt;

&lt;h:body&gt;

  &lt;h:form&gt;

  Generated by Map :

   &lt;h:selectManyListbox value="#{user.item}"&gt;

     &lt;f:selectItems value="#{user.itemValue}" /&gt;

   &lt;/h:selectManyListbox&gt;

&lt;br /&gt;&lt;br /&gt;

  &lt;h:commandButton value="Submit" action="result" /&gt;

&lt;h:commandButton value="Reset" type="reset" /&gt;

  &lt;/h:form&gt;

&lt;/h:body&gt;

&lt;/html&gt;

</code></pre>

<p>以下是文件: <em>result.xhtml</em> 中的代码 - </p>

<pre><code class="lang-html">&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

&lt;html xmlns="http://www.w3.org/1999/xhtml"

  xmlns:h="http://java.sun.com/jsf/html"

  xmlns:f="http://java.sun.com/jsf/core"

  xmlns:c="http://java.sun.com/jsp/jstl/core"&gt;

&lt;h:body&gt;

Selected: #{user.itemString}

&lt;/h:body&gt;

&lt;/html&gt;

</code></pre>

                <br>
                <p style="text-align:center;float:left;width:100%;margin-top:24px;margin-bottom:18px;">
                        <button class="layui-btn layui-btn-danger" id="btn-reward"> ¥ 我要打赏 </button>  
                        <button class="layui-btn" id="article-perfect">
                            <i class="layui-icon layui-icon-edit"></i>教程纠错
                        </button>
                        <button class="layui-btn layui-btn-normal" id="action-collection">
                            <i class="layui-icon layui-icon-rate"></i>收藏
                        </button>
                </p>



                <div style="width:100%;margin-bottom: 16px; height: 32px;margin-top:18px;float:left;">

                    <span style="float:left;">上一篇:<a href="http://f2e.im/u/ww9992019com" title="JSF列表框"><i class="layui-icon"></i>
                            JSF列表框</a></span>
                                            <span style="float:right;">下一篇:<a href="http://f2e.im/u/ww9992019com" style="float:right;" title="下一篇:JSF输出格式化"><i class="layui-icon"></i>
                            JSF输出格式化</a></span>
                                            <div id="googlead" style="float:left;margin-top:8px;">
                        <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
                        <!-- yiibai下方 -->
                        <ins class="adsbygoogle" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-1090193214637198" data-ad-slot="6494738921"></ins>
                        <script>
                            (adsbygoogle = window.adsbygoogle || []).push({});
                        </script>
                    </div>
                </div>

                <div style="width:100%;margin-top:18px;float:left;">


            </div>
暂无回复,说出你的观点吧
登录后即可参与回复