KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > improve > struts > taglib > layout > field > ajax > select > SelectOptionAction


1 package fr.improve.struts.taglib.layout.field.ajax.select;
2
3 import javax.servlet.http.HttpServletRequest JavaDoc;
4 import javax.servlet.http.HttpServletResponse JavaDoc;
5
6 import org.apache.struts.action.Action;
7 import org.apache.struts.action.ActionForm;
8 import org.apache.struts.action.ActionForward;
9 import org.apache.struts.action.ActionMapping;
10
11 /**
12  * The base select Action which creates a double-combo linked list with Ajax.
13  *
14  * @author nsoule
15  *
16  */

17 public abstract class SelectOptionAction extends Action {
18     
19     public ActionForward execute(ActionMapping in_mapping,
20                                  ActionForm in_form,
21                                  HttpServletRequest JavaDoc in_request,
22                                  HttpServletResponse JavaDoc in_response)
23          throws Exception JavaDoc {
24         
25         in_response.addHeader("Content-Type", "text/xml");
26         
27         // r�cup�re la liste des param�tres de la requ�te
28
String JavaDoc valueSelected = in_request.getParameter("valueSelected");
29         String JavaDoc formName = in_request.getParameter("formName");
30         String JavaDoc elementName = in_request.getParameter("elementName");
31                 
32         String JavaDoc suggestions = getXMLOptionList(in_request, valueSelected,
33                                               formName, elementName,
34                                               "ISO-8859-1");
35         
36         in_response.getOutputStream().print(suggestions);
37         
38         return null;
39     }
40     
41     /**
42      * Return an XML document corresponding to a list of options.
43      * @param in_request the HTTP request we are processing
44      * @param in_value the value of a selected item from the first selection list
45      * @param in_formName the name of the form that whe need to update
46      * @param elementName the name of the second selection list to be filled
47      * @param in_encoding the XML document encoding.
48      */

49     protected String JavaDoc getXMLOptionList(HttpServletRequest JavaDoc in_request,
50                                       String JavaDoc in_value,
51                                       String JavaDoc in_formName,
52                                       String JavaDoc elementName,
53                                       String JavaDoc in_encoding) {
54         
55         // get options list by the selected item and current HTTP request
56
Option[] options = getOptionList(in_request, in_value);
57         
58         StringBuffer JavaDoc res = new StringBuffer JavaDoc("<?xml version=\"1.0\" encoding=\"")
59                                        .append(in_encoding).append("\" ?>\n");
60                         
61         res.append("<selectChoice>");
62             res.append("<selectElement>");
63             res.append("<formName>").append(in_formName).append("</formName>");
64             res.append("<formElem>").append(elementName).append("</formElem>");
65         res.append("</selectElement>");
66         
67         if (options!=null) {
68             for (int i=0; i<options.length; i++) {
69                 Option op = options[i];
70                 res.append("<entry>");
71                 res.append("<optionText>").append(op.getLabel()).append("</optionText>");
72                 res.append("<optionValue>").append(op.getValue()).append("</optionValue>");
73                 res.append("</entry>");
74             }
75         }
76         
77         res.append("</selectChoice>");
78         
79         return res.toString();
80     }
81         
82     /**
83      * Return an array of <code>fr.improve.struts.taglib.layout.field.ajax.select.Option</code>
84      * according to a selected item.
85      * @param in_request the HTTP request we are processing
86      * @param in_item a selected item of the first selection list.
87      */

88     public abstract Option[] getOptionList(HttpServletRequest JavaDoc in_request, String JavaDoc in_item);
89
90 }
91
Popular Tags