Struts2 <sx:autocompleter>自动提示例子
精华
小牛编辑
158浏览
2023-03-14
在Struts2,<sx:autocompleter>标记是一个组合框,当在用户输入文本框会自动提示下拉的提示菜单。
这里创建一个Web工程:strut2autocompleter,来演示在多个复选框如何设置的默认值,整个项目的结构如下图所示:
这个功能是通过Dojo库实现的,所以,一定要包含“struts2-dojo-plugin.jar”这个依赖库,把“struts-dojo-tags”标签上的页面,并通过 <sx:head />输出其标题信息前。
例如,
<%@ 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>
Struts2 <s:autocompleter> 示例
在一个<s:autocompleter>标签的完整的例子,产生下拉的提示菜单,同时在相应的文本框的用户输入。
2. 动作类
Action类生成的Web框架的选项列表的“autocompleter”组成部分。
AutoCompleterAction.java
package com.yiibai.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; } }
3. 结果页面
通过“<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>
3. struts.xml
<?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.yiibai.common.action.AutoCompleterAction" method="display"> <result name="none">/pages/autocompleter.jsp</result> </action> <action name="resultAction" class="com.yiibai.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>
4. 示例
http://localhost:8080/Struts2Example/autoCompleterAction.action
下面是另一个例子,说明使用JSON数据,提供选择选项列表中的autocompleter组件 –
Struts2 autocompleter + JSON 示例
参考
- Struts 2 autocompleter文档
- Struts 2 ajax and javascript recipes
- Struts2 combobox示例