在Struts2,<sx:autocompleter>標記是一個下拉式方塊,當在使用者輸入文字框會自動提示下拉的提示選單。
這裡建立一個Web工程:strut2autocompleter,來演示在多個核取方塊如何設定的預設值,整個專案的結構如下圖所示:
例如,
<%@ taglib prefix="sx" uri="/struts-dojo-tags" %> <html> <head> <sx:head /> </head> <body> <sx:autocompleter label="What's your lucky number?" name="yourLuckyNumber" autoComplete="false" list="{'1','12','13','14'}" />
產生下面的HTML
<html> <head> <script language="JavaScript" type="text/javascript"> // Dojo configuration djConfig = { isDebug: false, bindEncoding: "UTF-8" ,baseRelativePath: "/Struts2Example/struts/dojo/" ,baseScriptUri: "/Struts2Example/struts/dojo/" ,parseWidgets : false }; </script> <script language="JavaScript" type="text/javascript" src="/Struts2Example/struts/dojo/struts_dojo.js"></script> <script language="JavaScript" type="text/javascript" src="/Struts2Example/struts/ajax/dojoRequire.js"></script> <link rel="stylesheet" href="/Struts2Example/struts/xhtml/styles.css" type="text/css"/> <script language="JavaScript" src="/Struts2Example/struts/utils.js" type="text/javascript"></script> <script language="JavaScript" src="/Struts2Example/struts/xhtml/validation.js" type="text/javascript"></script> <script language="JavaScript" src="/Struts2Example/struts/css_xhtml/validation.js" type="text/javascript"></script> </head> ... <tr> <td class="tdLabel"> <label for="resultAction_yourLuckyNumber" class="label"> What's your lucky number?:</label></td> <td> <select dojoType="struts:ComboBox" id="resultAction_yourLuckyNumber" autoComplete="false" name="yourLuckyNumber" keyName="yourLuckyNumberKey" visibleDownArrow="true" > <option value="1">1</option> <option value="12">12</option> <option value="13">13</option> <option value="14">14</option> </select> </td> </tr> <script language="JavaScript" type="text/javascript"> djConfig.searchIds.push("resultAction_yourLuckyNumber");</script>
在一個<s:autocompleter>標籤的完整的例子,產生下拉的提示選單,同時在相應的文字框的使用者輸入。
Action類生成的Web框架的選項列表的「autocompleter」組成部分。
AutoCompleterAction.java
package com.tw511.common.action; import java.util.ArrayList; import java.util.List; import com.opensymphony.xwork2.ActionSupport; public class AutoCompleterAction extends ActionSupport{ private List<String> webframeworks = new ArrayList<String>(); private String yourFavWebFramework; private String yourLuckyNumber; public AutoCompleterAction(){ webframeworks.add("Spring MVC"); webframeworks.add("Struts 1.x"); webframeworks.add("Struts 2.x"); webframeworks.add("JavaServer Faces (JSF)"); webframeworks.add("Google Web Toolkit (GWT)"); webframeworks.add("Apache Wicket"); webframeworks.add("Apache Click"); webframeworks.add("Apache Cocoon"); webframeworks.add("JBoss Seam"); webframeworks.add("Stripes"); webframeworks.add("Apache Tapestry"); webframeworks.add("Others"); } public String getYourLuckyNumber() { return yourLuckyNumber; } public void setYourLuckyNumber(String yourLuckyNumber) { this.yourLuckyNumber = yourLuckyNumber; } public String getYourFavWebFramework() { return yourFavWebFramework; } public void setYourFavWebFramework(String yourFavWebFramework) { this.yourFavWebFramework = yourFavWebFramework; } public List<String> getWebframeworks() { return webframeworks; } public void setWebframeworks(List<String> webframeworks) { this.webframeworks = webframeworks; } public String display() { return NONE; } }
通過「<s:autocompleter>」渲染「autocompleter」元件標籤,並產生通過Java列表和OGNL自動下拉提示選單。
autocompleter.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <%@ taglib prefix="sx" uri="/struts-dojo-tags" %> <html> <head> <sx:head /> </head> <body> <h1>Struts 2 autocompleter example</h1> <s:form action="resultAction" namespace="/" method="POST" > <sx:autocompleter label="What's your lucky number?" name="yourLuckyNumber" autoComplete="false" list="{'1','12','13','14','21','22','23','24', '31','32','33','34','41','42','43','44'}" /> <sx:autocompleter label="What's your favorite web framework?" list="webframeworks" name="yourFavWebFramework" /> <s:submit value="submit" name="submit" /> </s:form> </body> </html>
result.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <body> <h1>Struts 2 autocompleter example</h1> <h2> Lucky Number : <s:property value="yourLuckyNumber"/> </h2> <h2> Web Appication Frameworks : <s:property value="yourFavWebFramework"/> </h2> </body> </html>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="autoCompleterAction" class="com.tw511.common.action.AutoCompleterAction" method="display"> <result name="none">/pages/autocompleter.jsp</result> </action> <action name="resultAction" class="com.tw511.common.action.AutoCompleterAction" > <result name="success">/pages/result.jsp</result> </action> </package> </struts>
注意:如果靜態檔案未找到,或是不能打日期選擇,請下載程式碼並參考其中的web.xml組態。
<filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/struts/*</url-pattern> </filter-mapping>
http://localhost:8080/Struts2Example/autoCompleterAction.action