Label S: Select struts2 jdbc

1

Hi, I'm learning Struts2 , I'm doing a registration form but I have a question about how I can pull the records that are in a table to the s:select tag (I practically want to make a > combobox pulling data from a field in a sql table).

This is my method of consulting the Database :

private String sql6= "select distinct RazonSocial  from titular;";

    public List<Propietario> consultarPr()throws SQLException{ 
    List<Propietario> listadopr = new ArrayList<Propietario>();
    Connection con=Coneccion.getConnection();
    PreparedStatement ps=con.prepareStatement(sql6);
    ResultSet res = ps.executeQuery();

    while(res.next()){
        Propietario p = new Propietario();
      p.setNombres(res.getString("RazonSocial"));

        listadopr.add(p);
    }
    ps.close();
    res.close();
    con.close();


    return listadopr;

}

My action:

private List<Propietario> listadopr = new ArrayList<Propietario>();
public String consultarPr() throws SQLException{
    ModelPropie ad = new ModelPropie();
    listadopr = ad.consultarPr();
    return SUCCESS;
}

My jsp:

<div class="form-group">
                <label>Titular</label>
                <s:iterator value="listadopr"> 
                <s:select class="form-control select2" name="p.UEA" style="width: 100%;" list="value" listvalue="RazonSocial" headerKey="">

                </s:select>
                </s:iterator>
              </div>

Where it says name p.UEA is where the field is registered and I save it to the table of my Database .

    
asked by DrGun Gun 30.05.2017 в 23:53
source

1 answer

0

You do not have to use a s:iterator in this case. The tag select , in its attribute list , already expects an iterable (map or list, normally). So in your case you should have in your action class a method like

  public Map<String,String> getListaRazonSocial() {
      Map<String,String> = new LinkedHashMap<String,String>();

      ... llenar el map
  }

(It's just an example, and it does not have to be Map<String,String> , it could be a map of other types, and it could also return a list of objects and use listKey '' and listValue to tell you how to extract the key and the value of each object).

The map would return as key the value that will remain in each option of the select, and the associated value will correspond to the text that is displayed.

And in your jsp you would simply write

  <s:select list="listaRazonSocial" name="p.UEA" style="..." />

Of course there are other options, but this is the simplest one

    
answered by 31.05.2017 / 01:52
source