|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Member
Iscritto dal: Dec 2006
Messaggi: 201
|
STRUTS: forms e beans
Qual'è la differenza tra i due ? Sono alle prime armi e sono un po confuso!
I forms servono per incapsulare e validare i dati contenuti nella richiesta http e sono descritti tramite gli elementi <form-bean>. I beans? |
![]() |
![]() |
![]() |
#2 |
Member
Iscritto dal: Apr 2004
Messaggi: 252
|
Allora i form sono scritti in html e servono per permettere all'utente di inserire informazioni da mandare al server.
i form-beans sono i veicoli attraverso i quali le informazioni arrivano al server. Per esempio se faccio un form nome cognome gli allego un form-beans che ha fra le sue variabili nome cognome Il form-beans sarà popolato con i dati inseriti nel form. |
![]() |
![]() |
![]() |
#3 |
Member
Iscritto dal: Dec 2006
Messaggi: 201
|
Ma i form non estendono la classe ActionForm e sono scritti in linguaggio java ?
![]() |
![]() |
![]() |
![]() |
#4 |
Member
Iscritto dal: Apr 2004
Messaggi: 252
|
Perdonami ho letto male il post...e ho capito che non capivi il rapporto fra il form e gli ActionForm..
ma da quanto so un beans in java non è altro che una classe Java che ha le variabili d'istanza private a cui si puo' accedere utilizzando solo i metodi get e set implementanti per cui Codice:
class SampleBean{ private Integer counter; public void setCounter(Integer i){ counter = i; } public Integer getCounter(){ return counter; } |
![]() |
![]() |
![]() |
#5 |
Member
Iscritto dal: Dec 2006
Messaggi: 201
|
Un ActionForm sono JavaBeans con la differenza però che estendono la classe Actionform. Entrambi hanno attributi private e metodi pubblici set e get.
Quando devo usare uno e quando l'altro ? |
![]() |
![]() |
![]() |
#6 |
Bannato
Iscritto dal: Jan 2003
Città:
Messaggi: 4421
|
...ciao...
...ogni form di una pagina web dovrebbe essere in grado di trasferire dati inseriti dall'utente all'action che si prende carico di eseguire xxx operazioni in base ai dati ricevuti...tali dati verranno passati alla action sfruttando una classe contenitore formata da beans e metodi di reset e validate...nel file di configurazione di struts dovrai specificare l'esistenza della action e del relativo form bean e nel form... ...action di login... Codice:
public class LoginAction extends Action { private final String FORWARD_TRUE = "true"; private final String FORWARD_FALSE = "false"; public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { super.execute(mapping, form, request, response); long startMilliseconds = System.currentTimeMillis(); String returnForward = "wrongForward"; ActionMessages messages = new ActionMessages(); LoginForm user = (LoginForm)form; String var = request.getParameter("req"); if (var==null) var=""; try { if (var.equals("out")) { request.getSession().setAttribute("user",null); returnForward = FORWARD_FALSE; } else if("login".equalsIgnoreCase(user.getOperazione())) { if(user.getUser().equals("utente")&&user.getPassword().equals("password")) { request.getSession().setAttribute("user","ok"); returnForward = FORWARD_TRUE; } else { messages.add("",new ActionMessage("label.error.nouserlogged")); returnForward = FORWARD_FALSE; } } }catch(Exception e) { exceptionManager(request, e); System.out.println(e); throw e; } saveMessages(request, messages); return (mapping.findForward(returnForward)); } } Codice:
public class LoginForm extends ActionForm { private String user; private String password; /** * @return Returns the password. */ public String getPassword() { return password; } /** * @param password The password to set. */ public void setPassword(String password) { this.password = password; } /** * @return Returns the user. */ public String getUser() { return user; } /** * @param user The user to set. */ public void setUser(String user) { this.user = user; } } Codice:
<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"> <struts-config> <data-sources> </data-sources> <form-beans> <form-bean name="LoginForm" type="it.form.LoginForm"/> </form-beans> <global-forwards> </global-forwards> <action-mappings> <action input="/login.jsp" name="LoginForm" path="/login" scope="request" type="it.action.LoginAction"> <forward name="true" path="/page/index.jsp"/> <forward name="false" path="/page/login.jsp"/> </action> </action-mappings> <controller> </controller> <message-resources parameter="FileResources"/> </struts-config> Codice:
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> <html:html> <title> Login </title> <link rel="stylesheet" type="text/css" href="general.css"> <script language="Javascript"> function validate(form) { var message=''; if(form.user.value=='') message = 'User richiesto'; if(form.password.value=='') message += '\r\nPassword richiesta'; if(message.length>0) { alert(message); return false; } form.submit(); } function reset(form) { form.dsCognome.value = ''; form.password.value = ''; form.dsCognome.focus(); return false; } </script> <link rel="stylesheet" href="/page/style.css" type="text/css"> <body> <html:form action="login" method="post"> <html:hidden property="operazione" value="login"></html:hidden> <p align="center" valign="middle"> <table height="100%"> <tr> <td> <table width="100%"> <tr> <td align="center" colspan="3"> <font class="error_message" color="red"><html:messages id="message" message="true"><bean:write name="message"/></html:messages></font> </td> </tr> </table> <table border="2" bordercolor="#ACDFEE" width="700px"> <tr> <td> <table align="center"> <td align="left" class="wpsLabelText"><b>User</b></td> <td> </td> <td><html:text property="user" maxlength="15"></html:text></td> </tr> <tr> <td align="left" class="wpsLabelText"><b>Password</b></td> <td> </td> <td><html:password property="password" maxlength="15"></html:password></td> </table> <table align="center"> <tr> <td align="center" > <input type="button" value="Annulla" onClick="reset(document.LoginForm); return false;" style="background-color:#79AEC1;color:#FFFFFF;" /> <input type="button" value="Entra" onClick="validate(document.LoginForm); return false;" style="background-color:#79AEC1;color:#FFFFFF;" /> </td> </tr> </table> </td> </tr> </table> </td> </tr> </table> </html:form> </body> </html:html> ...ciao... |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 07:26.