Código:
Desde la web: http://struts.apache.org/2.1.6/docs/combobox.html<s:bean name="struts.util.Counter" var="year"> <s:param name="first" value="text('firstBirthYear')"/> <s:param name="last" value="2000"/> <s:combobox label="Birth year" size="6" maxlength="4" name="birthYear" list="#year"/> </s:bean>
Mi código es el siguiente, tengo dos clases:
package.Customer.java
Código:
package.Combo.javapublic class Customer { private String name; private int age; private String email; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
Código:
Mi config en struts.xmlimport com.opensymphony.xwork2.ActionSupport; import java.util.ArrayList; import java.util.List; import java.util.*; import package.Customer; public class Combo extends ActionSupport implements java.util.Iterator, java.io.Serializable { @Override public boolean hasNext() { return false; //To change body of implemented methods use File | Settings | File Templates. } @Override public Object next() { return null; //To change body of implemented methods use File | Settings | File Templates. } @Override public void remove() { //To change body of implemented methods use File | Settings | File Templates. } List<Customer> customers; public String execute() { customers = new ArrayList<Customer>(); tags.dto.Customer c = new Customer(); c.setName("George Joseph"); c.setAge(45); c.setEmail("[email protected]"); customers.add(c); System.out.println("Crea Customer c:" + c.getName()); c = new Customer(); c.setName("Mary Philip"); c.setAge(22); c.setEmail("[email protected]"); customers.add(c); System.out.println("Crea Customer c:" + c.getName()); customers.iterator(); return SUCCESS; } public List<Customer> getCustomers() { return customers; } public void setCustomers(List<Customer> customers) { this.customers = customers; } }
Código:
Mi pagina.jsp:<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="tags" extends="struts-default"> <action name="IterateDemo" class="package.Combo"> <param name="customers"/> <result>/tags/iteratedemo.jsp</result> </action> </package> </struts>
Código:
Archivo web.xml<%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> <title>COMBOBOX con Struts 2</title> </head> <body> <s:form> <s:action name="IterateDemo" var="customers" > Comienza Combo <s:bean name="tags.iterate.action.Combo" var="customers"> <s:param name="customers" value="text{'name'}"/> <s:combobox label="Lenguajes" size="6" maxlength="4" name="name" list="#customers"/> </s:bean> Fin Combo </s:action> </s:form> </body> </html>
Código:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
Ese es el proyecto completo, adjunté todos los archivos para alguien que le pueda servir, el único problema es que no me muestra los valores de "customers" sino que sólo muestra un combo vacío.
Ayuda por favor!! He seguido esta web: http://struts.apache.org/2.1.6/docs/combobox.html
(La guía de apache, pero aún así no me resulta! Qué hago mal??)
Ayudaaaa!!!!