autocompleter 标签会生成一个带下拉按钮的单行文本输入框,当用户单击下拉按钮时,将看到一系列的选项,单击某个选项可以将该选项填入单行文本框
web.xml
<?
xml version="1.0" encoding="GBK"
?>
< web-app version ="2.4" xmlns ="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
< filter >
< filter-name > struts2 </ filter-name >
< filter-class > org.apache.struts2.dispatcher.FilterDispatcher </ filter-class >
</ filter >
< filter-mapping >
< filter-name > struts2 </ filter-name >
< url-pattern > /* </ url-pattern >
</ filter-mapping >
</ web-app >
< web-app version ="2.4" xmlns ="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
< filter >
< filter-name > struts2 </ filter-name >
< filter-class > org.apache.struts2.dispatcher.FilterDispatcher </ filter-class >
</ filter >
< filter-mapping >
< filter-name > struts2 </ filter-name >
< url-pattern > /* </ url-pattern >
</ filter-mapping >
</ web-app >
struts.xml
<!
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.custom.i18n.resources" value ="messageResource" />
< constant name ="struts.i18n.encoding" value ="GBK" />
< package name ="ajax" extends ="struts-default" >
< action name ="books" >
< result > /data </ result >
</ action >
</ package >
</ struts >
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd" >
< struts >
< constant name ="struts.custom.i18n.resources" value ="messageResource" />
< constant name ="struts.i18n.encoding" value ="GBK" />
< package name ="ajax" extends ="struts-default" >
< action name ="books" >
< result > /data </ result >
</ action >
</ package >
</ struts >
JSON数据文件
[
["Spring2.0宝典"],
["轻量级J2EE企业实战"],
["基于J2EE的Ajax宝典"]
]
auto.jsp
<%
...
@ page contentType="text/html;charset=GBK" language="java"
%>
<% ... @ taglib prefix="s" uri="/struts-tags" %>
< html >
< head >
< title > 自动完成 </ title >
< s:head theme ="ajax" debug ="true" />
</ head >
将两个关联起来
< br />
< body >
< form id ="selectForm" >
请选择您喜欢的作者: < br >
< s:autocompleter theme ="simple" name ="author"
list ="{'李刚','Rod Johnson' , 'David Flanagan'}"
value ="李刚" notifyTopics ="/book"
forceValidOption ="true"
id ="sel" />
</ form >
请选择您喜欢的图书: < br >
< s:url id ="getBook" value ="/getBook.action" />
< s:autocompleter theme ="ajax" href ="${getBook}" cssStyle ="width: 240px;"
autoComplete ="false" formId ="selectForm" listenTopics ="/book" forceValidOption ="true" id ="ops" />
</ body >
</ html >
<% ... @ taglib prefix="s" uri="/struts-tags" %>
< html >
< head >
< title > 自动完成 </ title >
< s:head theme ="ajax" debug ="true" />
</ head >
将两个关联起来
< br />
< body >
< form id ="selectForm" >
请选择您喜欢的作者: < br >
< s:autocompleter theme ="simple" name ="author"
list ="{'李刚','Rod Johnson' , 'David Flanagan'}"
value ="李刚" notifyTopics ="/book"
forceValidOption ="true"
id ="sel" />
</ form >
请选择您喜欢的图书: < br >
< s:url id ="getBook" value ="/getBook.action" />
< s:autocompleter theme ="ajax" href ="${getBook}" cssStyle ="width: 240px;"
autoComplete ="false" formId ="selectForm" listenTopics ="/book" forceValidOption ="true" id ="ops" />
</ body >
</ html >
我们也可以为autocompleter做联动效果
首先编写action
package
lee;
import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
public class GetBookAction extends ActionSupport
... {
private String author;
private List<String> books = new ArrayList<String>();
public String getAuthor()
...{
return author;
}
public void setAuthor(String author)
...{
this.author = author;
}
public List<String> getBooks()
...{
return books;
}
public String execute() throws Exception
...{
System.out.println(author);
if (author.equals("李刚"))
...{
books.clear();
books.add("Spring2.0宝典");
books.add("轻量级J2EE企业应用实战");
books.add("基于J2EE的Ajax宝典");
}
else if (author.equals("Rod Johnson"))
...{
books.clear();
books.add("Expert One-on-One J2EE Design and Development");
}
else if (author.equals("David Flanagan"))
...{
books.clear();
books.add("JavaScript权威指南");
}
return SUCCESS;
}
}
import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
public class GetBookAction extends ActionSupport
... {
private String author;
private List<String> books = new ArrayList<String>();
public String getAuthor()
...{
return author;
}
public void setAuthor(String author)
...{
this.author = author;
}
public List<String> getBooks()
...{
return books;
}
public String execute() throws Exception
...{
System.out.println(author);
if (author.equals("李刚"))
...{
books.clear();
books.add("Spring2.0宝典");
books.add("轻量级J2EE企业应用实战");
books.add("基于J2EE的Ajax宝典");
}
else if (author.equals("Rod Johnson"))
...{
books.clear();
books.add("Expert One-on-One J2EE Design and Development");
}
else if (author.equals("David Flanagan"))
...{
books.clear();
books.add("JavaScript权威指南");
}
return SUCCESS;
}
}
这里判断中文作者名时候使用的并不是gb2312的名字,而是unicode的,因为所填struts2使用的是dojo,而dojo使用的是unicode
更新struts.xml
<!
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.custom.i18n.resources" value ="messageResource" />
< constant name ="struts.i18n.encoding" value ="GBK" />
< package name ="ajax" extends ="struts-default" >
< action name ="getBook" class ="lee.GetBookAction" >
< result > /books.jsp </ result >
</ action >
</ package >
</ struts >
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd" >
< struts >
< constant name ="struts.custom.i18n.resources" value ="messageResource" />
< constant name ="struts.i18n.encoding" value ="GBK" />
< package name ="ajax" extends ="struts-default" >
< action name ="getBook" class ="lee.GetBookAction" >
< result > /books.jsp </ result >
</ action >
</ package >
</ struts >
测试jsp
<%
...
@ page contentType="text/html;charset=GBK" language="java"
%>
<% ... @ taglib prefix="s" uri="/struts-tags" %>
< html >
< head >
< title > 自动完成 </ title >
< s:head theme ="ajax" debug ="true" />
</ head >
将两个关联起来
< br />
< body >
< form id ="selectForm" >
请选择您喜欢的作者: < br >
< s:autocompleter theme ="simple" name ="author"
list ="{'李刚','Rod Johnson' , 'David Flanagan'}"
value ="李刚" notifyTopics ="/book"
forceValidOption ="true"
id ="sel" />
</ form >
请选择您喜欢的图书: < br >
< s:url id ="getBook" value ="/getBook.action" />
< s:autocompleter theme ="ajax" href ="${getBook}" cssStyle ="width: 240px;"
autoComplete ="false" formId ="selectForm" listenTopics ="/book" forceValidOption ="true" id ="ops" />
</ body >
</ html >
<% ... @ taglib prefix="s" uri="/struts-tags" %>
< html >
< head >
< title > 自动完成 </ title >
< s:head theme ="ajax" debug ="true" />
</ head >
将两个关联起来
< br />
< body >
< form id ="selectForm" >
请选择您喜欢的作者: < br >
< s:autocompleter theme ="simple" name ="author"
list ="{'李刚','Rod Johnson' , 'David Flanagan'}"
value ="李刚" notifyTopics ="/book"
forceValidOption ="true"
id ="sel" />
</ form >
请选择您喜欢的图书: < br >
< s:url id ="getBook" value ="/getBook.action" />
< s:autocompleter theme ="ajax" href ="${getBook}" cssStyle ="width: 240px;"
autoComplete ="false" formId ="selectForm" listenTopics ="/book" forceValidOption ="true" id ="ops" />
</ body >
</ html >
book.jsp (用来动态生成联动输入框数据)
<%
...
@ page contentType="text/html;charset=GBK" language="java"
%>
<% ... @ taglib prefix="s" uri="/struts-tags" %>
[
< s:iterator value ="books" >
[" < s:property /> "],
</ s:iterator >
]
<% ... @ taglib prefix="s" uri="/struts-tags" %>
[
< s:iterator value ="books" >
[" < s:property /> "],
</ s:iterator >
]