Ciao a tutti sto studiando Struts 2 e vorrei caricare dei dati dentro una listbox al caricamento della pagina
Vi posto la pagina jsp dove ho la form
codice:<s:form action="Utente.action" method="POST" validate="true"> <tr> <td colspan="2">Please enter</td> </tr> <s:actionerror /> <s:textfield name="username" label="User Name"/> <s:password name="password" label="Password"/> <s:select label="Profilo" name="profilo" headerKey="0" headerValue="---Seleziona Profilo------" list="listaprofili" listKey="id" listValue="profilo" > </s:select> <s:submit action ="add" value="Save" align="left"/> <s:submit value="Visualizza" action="vis" align="rigth" /> </s:form>
Vi posto anche il file struts.xml
codice:<package name="default" namespace="/" extends="struts-default"> <action name="add" method="add" class="net.roseindia.Utente"> <result name="input">/insertData.jsp</result> <result name="SUCCESS" >/insertSucces.jsp</result> </action> <action name="vis" method="vis" class="net.roseindia.Utente" > <result name="input">/visualizza.jsp</result> <result name="SUCCESS">/visualizza.jsp</result> </action> </package>
Vi metto anche la classe java dove stavo tentando di scrivere qualcosa su metodo preparate perche ho letto che viene svolto all'apertura della pagina (utente.java)
codice:package net.roseindia; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import com.opensymphony.xwork2.ActionSupport; import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List; public class Utente extends ActionSupport { private String username; private String password; private ArrayList<String> myList ; private ResultSet myList2 ; private ArrayList<String> listaprofili ; public String execute(){ System.out.println("excutee!!!!!!!"); return "SUCCESS"; } public String add() throws Exception{ try{ Connessione con = new Connessione(); Connection c = null; Statement stmt= null; c = con.getCon(); stmt=c.createStatement(); if (username.equals("daniele")){ addActionError("errororororor"); return "input"; }else{ stmt.executeUpdate("INSERT INTO utenti (nome,password) VALUES('"+getUsername()+"','"+getPassword()+"')"); //this.myList2 = lista(stmt); return "SUCCESS"; } }catch(Exception e){ System.out.println("Eccezione : "+e.getMessage()); } return "SUCCESS"; } public String vis() throws Exception{ System.out.println("visualizza!!!!!!!"); Connessione con = new Connessione(); Connection c = null; Statement stmt= null; c = con.getCon(); stmt=c.createStatement(); this.myList2 = lista(stmt); return "SUCCESS"; } public void prepare(){ this.listaprofili = new ArrayList<String>(); listaprofili.add("ADMIN"); listaprofili.add("DIP"); } public String getUsername() { return username; } public void setUsername(String value) { username = value; } public String getPassword() { return password; } public void setPassword(String value) { password = value; } private ResultSet lista(Statement stmt) throws SQLException{ ResultSet rs = null; try{ rs = stmt.executeQuery("Select * from utenti "); }catch(Exception e){ System.out.println(e.getMessage()); } return rs; } public ArrayList<String> getMylist(){ return myList; } public ArrayList<String> getMylist2() throws SQLException{ ArrayList<String> lista = new ArrayList<String>(); while(this.myList2.next()){ lista.add(this.myList2.getString(2)); } return lista; } }

Rispondi quotando