Seguinte foi esse.
Tenho essa classe
public class ClienteController implements Controller {
private transient final Log log = LogFactory.getLog(ClienteController.class);
private ClienteManager mgr = null;
public void setClienteManager(ClienteManager clienteManager) {
this.mgr = clienteManager;
}
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response)
throws Exception {
if (log.isDebugEnabled()) {
log.debug("entering 'handleRequest' method...");
}
return new ModelAndView("clienteList", Constants.CLIENTE_LIST, mgr.getGrupos(""));
}
public List ListaGrupos() {
return mgr.getGrupos("");
}
}
Até esse momento beleza, pois os testes trazem os grupos que é um atributo da classe cliente lá do model.
Porém estou precisando montar um combo box no JSP com todos os grupos dos cllientes.
Ai vem essa ClasseTAG.
package br.com.modere.webapp.taglib;
import java.io.IOException;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import org.displaytag.tags.el.ExpressionEvaluator;
import br.com.modere.model.LabelValue;
/**
-
Tag for creating multiple <select> options for displaying a list of
-
Grupo names.
-
-
NOTE - This tag requires a Java2 (JDK 1.2 or later) platform.
-
@author Luciano Brunes
-
@version 1.0 $ $Date: 11/10/2006 11:51:00 $
-
@jsp.tag name=“grupo” bodycontent=“empty” <em>/ public class GrupoTag extends TagSupport { private static final long serialVersionUID = 3905528206810167095L; private String name; private String prompt; private String scope; private String selected; /</em>*
- @param name The name to set.
-
@jsp.attribute required=“false” rtexprvalue=“true”
*/
public void setName(String name) {
this.name = name;
}
/**
- @param prompt The prompt to set.
-
@jsp.attribute required=“false” rtexprvalue=“true”
*/
public void setPrompt(String prompt) {
this.prompt = prompt;
}
/**
- @param selected The selected option.
-
@jsp.attribute required=“false” rtexprvalue=“true”
*/
public void setDefault(String selected) {
this.selected = selected;
}
/**
- Property used to simply stuff the list of ClasseSocials into a
- specified scope.
- @param scope
-
@jsp.attribute required=“false” rtexprvalue=“true”
*/
public void setToScope(String scope) {
this.scope = scope;
}
/**
-
Process the start of this tag.
-
@return int status
-
@exception JspException if a JSP exception has occurred
-
@see javax.servlet.jsp.tagext.Tag#doStartTag() */ public int doStartTag() throws JspException { ExpressionEvaluator eval = new ExpressionEvaluator(this, pageContext);
if (selected != null) { selected = eval.evalString(“default”, selected); }
Locale userLocale =
pageContext.getRequest().getLocale();
List Grupos =this.buildGruposList(userLocale);if (scope != null) { if (scope.equals(“page”)) { pageContext.setAttribute(name, Grupos); } else if (scope.equals(“request”)) { pageContext.getRequest().setAttribute(name, Grupos); } else if (scope.equals(“session”)) { pageContext.getSession().setAttribute(name, Grupos); } else if (scope.equals(“application”)) { pageContext.getServletContext().setAttribute(name, Grupos); } else { throw new JspException(“Attribute ‘scope’ must be: page, request, session or application”); } } else { StringBuffer sb = new StringBuffer(); sb.append("<select name="" + name + “” id="" + name + “” class=“select”>\n");
if (prompt != null) { sb.append(" <option value=\"\" selected=\"selected\">"); sb.append(eval.evalString("prompt", prompt) + "</option>\n"); } for (Iterator i = Grupos.iterator(); i.hasNext();) { LabelValue ClasseSocial = (LabelValue) i.next(); sb.append(" <option value=\"" + ClasseSocial.getValue() + "\""); if ((selected != null) && selected.equals(ClasseSocial.getValue())) { sb.append(" selected=\"selected\""); } sb.append(">" + ClasseSocial.getLabel() + "</option>\n"); } sb.append("</select>"); try { pageContext.getOut().write(sb.toString()); } catch (IOException io) { throw new JspException(io); }
}
return
super.doStartTag();
}
/**
- Release aquired resources to enable tag reusage.
-
@see javax.servlet.jsp.tagext.Tag#release()
*/
public void release() {
super.release();
}
/**
-
Build a List of LabelValues for all the available countries. Uses
-
the two letter uppercase ISO name of the ClasseSocial as the value and the
-
localized ClasseSocial name as the label.
-
@param locale The Locale used to localize the ClasseSocial names.
-
@return List of LabelValues for all available countries. */ protected List buildGruposList(Locale locale) { final String EMPTY = “”; List grupos = new ArrayList(); grupos.add(“CLINICA”);
List grupos2 =
new ArrayList();for (Iterator it=grupos.iterator(); it.hasNext(); ) { final String iso = it.next().toString(); final String name = iso;
if (!EMPTY.equals(name)) { LabelValue Grupo = new LabelValue(name, iso); if (!grupos2.contains(Grupo)) { grupos2.add(new LabelValue(name, iso)); } }
}
Collections.sort(grupos2,);new LabelValueComparator(locale)System.out.println("GruposTag: " + grupos2);return grupos2;
}
/**
-
Class to compare LabelValues using their labels with
-
locale-sensitive behaviour. */ public class LabelValueComparator implements Comparator { private Comparator c;
/**
- Creates a new LabelValueComparator object.
-
@param locale The Locale used for localized String comparison.
*/
public LabelValueComparator(Locale locale) {
c = Collator.getInstance(locale);
}
/**
-
Compares the localized labels of two LabelValues.
-
@param o1 The first LabelValue to compare.
-
@param o2 The second LabelValue to compare.
-
@return The value returned by comparing the localized labels. */ public final int compare(Object o1, Object o2) { LabelValue lhs = (LabelValue) o1; LabelValue rhs = (LabelValue) o2;
return c.compare(lhs.getLabel(), rhs.getLabel()); } } }
No buildGruposList desta classe fiz um objeto direto receber o valor para testar o JSP. Funciona.
Mas como faço para buscar a minha lista de grupos lá da classe ClienteController ?
Até.