autocompleteはlist属性に候補となる文字列のリストを入れれば、入力時に候補としてでてくる。しかし、日本語入力に弱くバックスペースなどを押さないと候補がでてこない。以下ソース。struts2-dojo-pluginをクラスパスに入れることを忘れずに
<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s" %> <%@taglib uri="/struts-dojo-tags" prefix="sx" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Struts2 Test</title> </head> <body> <s:form action="action1.action" method="POST"> <s:label name="text" id="text" /> <sx:head/> <sx:autocompleter list="{'田中', '田辺', '田所'}" name="autocomplete" /> <s:submit /> </s:form> </body> </html>
次にアクションクラス、Page1.java
import java.util.Date; public class Page1 { private String text; private String autocomplete; public void setText(String text){ this.text = text; } public String getText(){ return this.text; } public void setAutocomplete(String autocomplete){ this.autocomplete = autocomplete; } public String getAutocomplete(){ return this.autocomplete; } public String execute() throws Exception{ this.setText(this.getAutocomplete() + "が選択されました"); return "page1"; } }
<?xml version="1.0"?> <!DOCTYPE struts PUBLIC "-//APache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="autocomplete" namespace="/" extends="struts-default"> <action name="action1" class="Page1"> <result name="page1">/page1.jsp</result> </action> </package> </struts>
フィルタマッピングは省略