In this example, you will see the use of autocomplete tag of struts2.2.1. It is a combo box that will a display drop down suggestion lists while user enter on the text box.
1- index.jsp
<%@taglib uri="/struts-tags" prefix="s" %>ADS_TO_REPLACE_1 <html> <head> <title>AutoComplete_TagExample</title> </head> <body>ADS_TO_REPLACE_2 <h2>AutoComplete_Tag_Example</h2><hr> <s:a href="autoCompleteAction.action">AutoComplete_TagExample</s:a> </body>ADS_TO_REPLACE_3 </html> |
2_ ListOfMonth.jsp
<%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%>ADS_TO_REPLACE_4 <%@ taglib prefix="sx" uri="/struts-dojo-tags"%> <html> <head><title>AutoComplete_TagExample</title><sx:head /></head>ADS_TO_REPLACE_5 <body><h2>AutoComplete_Tag_Example</h2><hr> <s:form action="resultAction.action" > <sx:autocompleter size="1" label="Month" ADS_TO_REPLACE_6 list="nameMonths" name="month"></sx:autocompleter> </action> <s:submit align="left"> </s:submit>ADS_TO_REPLACE_7 </s:form></body> </html> |
3_ AutoCompleteAction.java
package roseindia.action; import java.sql.*; import java.util.ArrayList; import java.util.List; import com.opensymphony.xwork2.ActionSupport; public class AutoCompleteAction extends ActionSupport { private String password = "root"; private String user = "root"; private String url = "jdbc:mysql://192.168.10.13/autocompleteexample"; Connection con = null; Statement stmt = null; ResultSet rs = null; private List<String> nameMonths = new ArrayList<String>(); private String month; public AutoCompleteAction() throws SQLException { try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection(url, user, password); stmt = con.createStatement(); String sql = "select nameMonths from monthsName"; rs = stmt.executeQuery(sql); while (rs.next()) { nameMonths.add(rs.getString(1)); } } catch (ClassNotFoundException e) { e.printStackTrace(); } } public String getMonth() { return month; } public void setMonth(String month) { this.month = month; } public List<String> getNameMonths() { return nameMonths; } public void setNameMonths(List<String> nameMonths) { this.nameMonths = nameMonths; } public String execute() throws Exception { return SUCCESS; } public String display() { return NONE; } } |
4_ OutListOfMonth.jsp
<struts> <constant name="struts.devMode" value="true" />ADS_TO_REPLACE_8 <package name="roseindia" extends="struts-default"> <action name="autoCompleteAction" class="roseindia.action.AutoCompleteAction" method="display"> <result name="none">jspPages/ListOfMonth.jsp</result>ADS_TO_REPLACE_9 </action> <action name="resultAction" class="roseindia.action.AutoCompleteAction"> <result name="success">jspPages/OutListOfMonth.jsp</result>ADS_TO_REPLACE_10 </action> </package> </struts> |
4_ OutListOfMonth.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" ADS_TO_REPLACE_11 pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html>ADS_TO_REPLACE_12 <head><title>AutoComplete_TagExample</title></head> <body> <h2>AutoComplete_Tag_Example</h2><hr>ADS_TO_REPLACE_13 Selected Month :<s:property value="month" /> </body> </html> |
index.gifADS_TO_REPLACE_14
AutocompleteBox.gif
ADS_TO_REPLACE_15
success.gif
ADS_TO_REPLACE_16
Advertisements
Ads
Ads