ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
Creaci¨®n aplicaci¨®n base Struts2 Clase 11- 12
Crear Aplicaci¨®n Empresarial Click New ?  Enterprise Application Project
Seleccionar servidor de aplicaciones New Target Runtime  ?  Jboss  ?  Jboss 5.0
Seleccionar servidor de aplicaciones Buscar carpeta base del JBOSS instalado
Seleccionar servidor de aplicaciones
Ingresar nombre del proyecto
Agregar M¨®dulos EJB y Web Click en NEW MODULE  y Seleccionar EJB Module y Web Module
Instalaci¨®n de aplicaci¨®n Web del Profe 1.-Copiar del proyecto Web Enviado las carpetas SRC y WebContent a la carpeta Base del proytecto creado que se ubicar¨¢ en : C:serslt;Usuario>orkspaceplicacionEmpresarialWeb y reemplazar Todo
Instalaci¨®n de aplicaci¨®n Web del Profe 2.- Actualizar el proyecto creado para que aparezcan los archivos copiados
Agregar las librer¨ªas Struts2 para la ejecuci¨®n 1.- Click derecho sobre la aplicaci¨®n Web 2.- Propiedades  ?  Java Build Path  ?  Libraries  ?  Add JARs
Agregar las librer¨ªas Struts2 para la ejecuci¨®n 3.- Seleccionar JAR de la carpeta LIB ubicada en WebContent ?  WEB-INF del proyecto creado
Ejecutar la aplicaci¨®n creada 1.- Click derecho sobre aplicaci¨®n Web  ?  RUN AS  ? Run on Server
Crear una aplicaci¨®n Struts2 Base 1.- Crear Formuario JSP : 1.1 Crear carpeta  ingreso  donde se coloquen los JSP a crear
Crear Formulario JSP 1.-Click derecho sobre carpeta ingreso  ?  JSP File 2.- Crear index.jsp , formulario.jsp, resultado.jsp
index.jsp <%@ page language= &quot;java&quot; contentType=&quot;text/html; charset=ISO-8859-1&quot; pageEncoding= &quot;ISO-8859-1&quot;%> <%@ taglib prefix= &quot;s&quot; uri=&quot;/struts-tags&quot; %> <!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;> <html> <head> <meta http-equiv= &quot;Content-Type&quot; content=&quot;text/html; charset=ISO-8859-1&quot;> <title>Struts 2 Form Tags - Welcome</title> </head> <body> <h1> Pagina de edicion</h1> <p><a href= '<s:url action=&quot;edit&quot; />' > Editar Informaci¨®n</a></p> </body> </html>
Formulario.jsp <%@ page language= &quot;java&quot; contentType=&quot;text/html; charset=ISO-8859-1&quot; pageEncoding= &quot;ISO-8859-1&quot;%> <%@ taglib prefix= &quot;s&quot; uri=&quot;/struts-tags&quot; %> <!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;> <html> <head> <s:head /> <meta http-equiv= &quot;Content-Type&quot; content=&quot;text/html; charset=ISO-8859-1&quot;> <title> Editar Persona</title> </head> <body> <h1> Actualizar</h1> <s:form action= &quot;save&quot; method=&quot;post&quot;> <s:textfield key= &quot;personBean.firstName&quot; />  <s:textfield key= &quot;personBean.lastName&quot; />  <s:select key= &quot;personBean.sport&quot; list=&quot;sports&quot; /> <s:radio key= &quot;personBean.gender&quot; list=&quot;genders&quot; /> <s:select key= &quot;personBean.residency&quot; list=&quot;states&quot; listKey=&quot;stateAbbr&quot; listValue=&quot;stateName&quot; /> <s:checkbox key= &quot;personBean.over21&quot; /> <s:checkboxlist key= &quot;personBean.carModels&quot; list=&quot;carModelsAvailable&quot; /> <s:submit key= &quot;submit&quot; /> </s:form> </body> </html>
Resultado.jsp <?xml version= &quot;1.0&quot; encoding=&quot;ISO-8859-1&quot; ?> <%@ taglib prefix= &quot;s&quot; uri=&quot;/struts-tags&quot; %> <%@ page language= &quot;java&quot; contentType=&quot;text/html; charset=ISO-8859-1&quot; pageEncoding= &quot;ISO-8859-1&quot;%> <!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;> <html xmlns= &quot;http://www.w3.org/1999/xhtml&quot;> <head> <meta http-equiv= &quot;Content-Type&quot; content=&quot;text/html; charset=ISO-8859-1&quot; /> <title>Update Successful</title> </head> <body> <h1>Updated Information</h1> <p> Infomacion Ingresada <s:property value= &quot;personBean&quot; /> </p> <p><a href= &quot;<s:url action='index' />&quot; > Regresar</a>.</p> </body> </html>
Crear Helper EditPersonHelper.java Crear Package :  com.aplicacion.presentacion.helper package com.aplicacion.presentacion.helper; import  com.aplicacion.presentacion.model.Person; /** * Implement Services needed to edit and save * a Person object's state.  In this implementation * the Person object's state is stored in memory *  @author  brucephillips * */ public class EditPersonHelper  { private static  Person  person ; private static String []  carModels = {&quot;Ford&quot;,&quot;Nissan&quot;};
EditPersonHelper.java static { person =  new Person(); person.setFirstName(&quot;Bruce&quot;); person.setLastName(&quot;Phillips&quot;); person.setSport(&quot;basketball&quot;); person.setGender(&quot;not sure&quot;); person.setResidency(&quot;KS&quot;); person.setOver21( true); person.setCarModels( carModels); } public  Person getPerson() {
EditPersonHelper.java return  EditServiceInMemory.person; } public void savePerson( Person personBean) { EditServiceInMemory.person.setFirstName(personBean.getFirstName() ); EditServiceInMemory.person.setLastName(personBean.getLastName() ); EditServiceInMemory.person.setSport(personBean.getSport() ); EditServiceInMemory.person.setGender( personBean.getGender() ); EditServiceInMemory.person.setResidency( personBean.getResidency() ); EditServiceInMemory.person.setOver21( personBean.isOver21() ); EditServiceInMemory.person.setCarModels(personBean.getCarModels() ); } }
Person.java Crear Package  com.aplicacion.presentacion.model package com.aplicacion.presentacion.model; import java.util.Arrays; public class Person { private String firstName; private String lastName; private String sport; private String gender; private String residency; private boolean over21; private String [] carModels;
Person.java public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; }
Person.java public void setSport(String sport) { this.sport = sport; } public void setGender(String gender) { this.gender = gender; } public String getGender() { return gender; } public String getSport() { return sport; } public void setResidency(String residency) { this.residency = residency; } public String getResidency() { return residency; } public void setOver21(boolean over21) { this.over21 = over21; }
Person.java public boolean isOver21() { return over21; } public void setCarModels(String [] carModels) { this.carModels = carModels; } public String [] getCarModels() { return carModels; } public String toString() { return &quot;First Name: &quot; + getFirstName() + &quot; | &quot; + &quot; Last Name:  &quot; + getLastName() + &quot; | &quot; + &quot; Favorite Sport: &quot; + getSport() + &quot; | &quot; + &quot; Gender: &quot; + getGender() + &quot; | &quot; +  &quot; Residency: &quot; + getResidency() + &quot; | &quot; + &quot; Over 21: &quot; + isOver21()  + &quot; | &quot; + &quot; Car models: &quot; + Arrays. asList( getCarModels() ) ; } }
State.java package com.aplicacion.presentacion.model; public class State { private String stateAbbr; private String stateName; public State (String stateAbbr, String stateName) { this.stateAbbr = stateAbbr; this.stateName = stateName; } public void setStateAbbr(String stateAbbr) { this.stateAbbr = stateAbbr; } public String getStateAbbr() { return stateAbbr; } public void setStateName(String stateName) { this.stateName = stateName; } public String getStateName() { return stateName; } public String toString() { return getStateAbbr() ; } }
Crear EditAction.java package com.aplicacion.presentacion.action; import com.opensymphony.xwork2.ActionSupport; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import com.aplicacion.presentacion.model.Person; import com.aplicacion.presentacion.model.State; import com.aplicacion.presentacion.helper.*; import  com.opensymphony.xwork2.ActionSupport;
public class EditAction extends ActionSupport { private static final long  serialVersionUID = 1L; private EditPersonHelper editService = new EditPersonHelper(); private Person personBean; private String [] sports = {&quot;football&quot;, &quot;baseball&quot;, &quot;basketball&quot; }; private String [] genders = {&quot;male&quot;, &quot;female&quot;, &quot;not sure&quot; }; private List<State> states ; private String [] carModelsAvailable = {&quot;Ford&quot;,&quot;Chrysler&quot;,&quot;Toyota&quot;,&quot;Nissan&quot;}; public String execute() throws Exception { editService.savePerson( getPersonBean() ); return  SUCCESS; }
public String input() throws Exception { setPersonBean( editService.getPerson() ); return  INPUT; } public Person getPersonBean() { return personBean; } public void setPersonBean(Person person) { personBean = person; }
public List<String> getSports() { return Arrays. asList(sports); } public List<String> getGenders() { return Arrays. asList(genders); } public List<State> getStates() { states =  new ArrayList<State>(); states.add(  new State(&quot;AZ&quot;, &quot;Arizona&quot;) ); states.add(  new State(&quot;CA&quot;, &quot;California&quot;) ); states.add(  new State(&quot;FL&quot;, &quot;Florida&quot;) ); states.add(  new State(&quot;KS&quot;, &quot;Kansas&quot;) ); states.add(  new State(&quot;NY&quot;, &quot;New York&quot;) ); return states; } public String [] getCarModelsAvailable() { return carModelsAvailable; } }
Crear edit.xml para registrar Action <?xml version= &quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <!DOCTYPE struts PUBLIC &quot;-//Apache Software Foundation//DTD Struts Configuration 2.0//EN¡±  &quot;http://struts.apache.org/dtds/struts-2.0.dtd&quot;> <struts> <constant name= &quot;struts.devMode&quot; value=&quot;false&quot; /> <package name= &quot;editar&quot; extends=&quot;struts-default&quot;  namespace=&quot;/ingreso&quot;> <!-- If no class attribute is specified the framework will assume success and  render the result index.jsp --> <!-- If no name value for the result node is specified the success value is the default --> <action name= &quot;index&quot;> <result>/index.jsp</result> </action> <action name= &quot;edit&quot; class=&quot;com.aplicacion.presentacion.action.EditAction&quot; method=&quot;input&quot;> <result name= &quot;input&quot;>/ ingreso/formulario.jsp</result> </action> <action name= &quot;save&quot; class=&quot;com.aplicacion.presentacion.action.EditAction&quot; method=&quot;execute&quot;> <result name= &quot;success&quot;>/ ingreso/resultado.jsp</result> </action> </package> </struts>
Registrarlo en Struts.xml <include file= &quot;edit.xml&quot;/>

More Related Content

Creaci¨®n aplicaci¨®n Web base struts2

  • 1. Creaci¨®n aplicaci¨®n base Struts2 Clase 11- 12
  • 2. Crear Aplicaci¨®n Empresarial Click New ? Enterprise Application Project
  • 3. Seleccionar servidor de aplicaciones New Target Runtime ? Jboss ? Jboss 5.0
  • 4. Seleccionar servidor de aplicaciones Buscar carpeta base del JBOSS instalado
  • 5. Seleccionar servidor de aplicaciones
  • 7. Agregar M¨®dulos EJB y Web Click en NEW MODULE y Seleccionar EJB Module y Web Module
  • 8. Instalaci¨®n de aplicaci¨®n Web del Profe 1.-Copiar del proyecto Web Enviado las carpetas SRC y WebContent a la carpeta Base del proytecto creado que se ubicar¨¢ en : C:serslt;Usuario>orkspaceplicacionEmpresarialWeb y reemplazar Todo
  • 9. Instalaci¨®n de aplicaci¨®n Web del Profe 2.- Actualizar el proyecto creado para que aparezcan los archivos copiados
  • 10. Agregar las librer¨ªas Struts2 para la ejecuci¨®n 1.- Click derecho sobre la aplicaci¨®n Web 2.- Propiedades ? Java Build Path ? Libraries ? Add JARs
  • 11. Agregar las librer¨ªas Struts2 para la ejecuci¨®n 3.- Seleccionar JAR de la carpeta LIB ubicada en WebContent ? WEB-INF del proyecto creado
  • 12. Ejecutar la aplicaci¨®n creada 1.- Click derecho sobre aplicaci¨®n Web ? RUN AS ? Run on Server
  • 13. Crear una aplicaci¨®n Struts2 Base 1.- Crear Formuario JSP : 1.1 Crear carpeta ingreso donde se coloquen los JSP a crear
  • 14. Crear Formulario JSP 1.-Click derecho sobre carpeta ingreso ? JSP File 2.- Crear index.jsp , formulario.jsp, resultado.jsp
  • 15. index.jsp <%@ page language= &quot;java&quot; contentType=&quot;text/html; charset=ISO-8859-1&quot; pageEncoding= &quot;ISO-8859-1&quot;%> <%@ taglib prefix= &quot;s&quot; uri=&quot;/struts-tags&quot; %> <!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;> <html> <head> <meta http-equiv= &quot;Content-Type&quot; content=&quot;text/html; charset=ISO-8859-1&quot;> <title>Struts 2 Form Tags - Welcome</title> </head> <body> <h1> Pagina de edicion</h1> <p><a href= '<s:url action=&quot;edit&quot; />' > Editar Informaci¨®n</a></p> </body> </html>
  • 16. Formulario.jsp <%@ page language= &quot;java&quot; contentType=&quot;text/html; charset=ISO-8859-1&quot; pageEncoding= &quot;ISO-8859-1&quot;%> <%@ taglib prefix= &quot;s&quot; uri=&quot;/struts-tags&quot; %> <!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;> <html> <head> <s:head /> <meta http-equiv= &quot;Content-Type&quot; content=&quot;text/html; charset=ISO-8859-1&quot;> <title> Editar Persona</title> </head> <body> <h1> Actualizar</h1> <s:form action= &quot;save&quot; method=&quot;post&quot;> <s:textfield key= &quot;personBean.firstName&quot; /> <s:textfield key= &quot;personBean.lastName&quot; /> <s:select key= &quot;personBean.sport&quot; list=&quot;sports&quot; /> <s:radio key= &quot;personBean.gender&quot; list=&quot;genders&quot; /> <s:select key= &quot;personBean.residency&quot; list=&quot;states&quot; listKey=&quot;stateAbbr&quot; listValue=&quot;stateName&quot; /> <s:checkbox key= &quot;personBean.over21&quot; /> <s:checkboxlist key= &quot;personBean.carModels&quot; list=&quot;carModelsAvailable&quot; /> <s:submit key= &quot;submit&quot; /> </s:form> </body> </html>
  • 17. Resultado.jsp <?xml version= &quot;1.0&quot; encoding=&quot;ISO-8859-1&quot; ?> <%@ taglib prefix= &quot;s&quot; uri=&quot;/struts-tags&quot; %> <%@ page language= &quot;java&quot; contentType=&quot;text/html; charset=ISO-8859-1&quot; pageEncoding= &quot;ISO-8859-1&quot;%> <!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;> <html xmlns= &quot;http://www.w3.org/1999/xhtml&quot;> <head> <meta http-equiv= &quot;Content-Type&quot; content=&quot;text/html; charset=ISO-8859-1&quot; /> <title>Update Successful</title> </head> <body> <h1>Updated Information</h1> <p> Infomacion Ingresada <s:property value= &quot;personBean&quot; /> </p> <p><a href= &quot;<s:url action='index' />&quot; > Regresar</a>.</p> </body> </html>
  • 18. Crear Helper EditPersonHelper.java Crear Package : com.aplicacion.presentacion.helper package com.aplicacion.presentacion.helper; import com.aplicacion.presentacion.model.Person; /** * Implement Services needed to edit and save * a Person object's state. In this implementation * the Person object's state is stored in memory * @author brucephillips * */ public class EditPersonHelper { private static Person person ; private static String [] carModels = {&quot;Ford&quot;,&quot;Nissan&quot;};
  • 19. EditPersonHelper.java static { person = new Person(); person.setFirstName(&quot;Bruce&quot;); person.setLastName(&quot;Phillips&quot;); person.setSport(&quot;basketball&quot;); person.setGender(&quot;not sure&quot;); person.setResidency(&quot;KS&quot;); person.setOver21( true); person.setCarModels( carModels); } public Person getPerson() {
  • 20. EditPersonHelper.java return EditServiceInMemory.person; } public void savePerson( Person personBean) { EditServiceInMemory.person.setFirstName(personBean.getFirstName() ); EditServiceInMemory.person.setLastName(personBean.getLastName() ); EditServiceInMemory.person.setSport(personBean.getSport() ); EditServiceInMemory.person.setGender( personBean.getGender() ); EditServiceInMemory.person.setResidency( personBean.getResidency() ); EditServiceInMemory.person.setOver21( personBean.isOver21() ); EditServiceInMemory.person.setCarModels(personBean.getCarModels() ); } }
  • 21. Person.java Crear Package com.aplicacion.presentacion.model package com.aplicacion.presentacion.model; import java.util.Arrays; public class Person { private String firstName; private String lastName; private String sport; private String gender; private String residency; private boolean over21; private String [] carModels;
  • 22. Person.java public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; }
  • 23. Person.java public void setSport(String sport) { this.sport = sport; } public void setGender(String gender) { this.gender = gender; } public String getGender() { return gender; } public String getSport() { return sport; } public void setResidency(String residency) { this.residency = residency; } public String getResidency() { return residency; } public void setOver21(boolean over21) { this.over21 = over21; }
  • 24. Person.java public boolean isOver21() { return over21; } public void setCarModels(String [] carModels) { this.carModels = carModels; } public String [] getCarModels() { return carModels; } public String toString() { return &quot;First Name: &quot; + getFirstName() + &quot; | &quot; + &quot; Last Name: &quot; + getLastName() + &quot; | &quot; + &quot; Favorite Sport: &quot; + getSport() + &quot; | &quot; + &quot; Gender: &quot; + getGender() + &quot; | &quot; + &quot; Residency: &quot; + getResidency() + &quot; | &quot; + &quot; Over 21: &quot; + isOver21() + &quot; | &quot; + &quot; Car models: &quot; + Arrays. asList( getCarModels() ) ; } }
  • 25. State.java package com.aplicacion.presentacion.model; public class State { private String stateAbbr; private String stateName; public State (String stateAbbr, String stateName) { this.stateAbbr = stateAbbr; this.stateName = stateName; } public void setStateAbbr(String stateAbbr) { this.stateAbbr = stateAbbr; } public String getStateAbbr() { return stateAbbr; } public void setStateName(String stateName) { this.stateName = stateName; } public String getStateName() { return stateName; } public String toString() { return getStateAbbr() ; } }
  • 26. Crear EditAction.java package com.aplicacion.presentacion.action; import com.opensymphony.xwork2.ActionSupport; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import com.aplicacion.presentacion.model.Person; import com.aplicacion.presentacion.model.State; import com.aplicacion.presentacion.helper.*; import com.opensymphony.xwork2.ActionSupport;
  • 27. public class EditAction extends ActionSupport { private static final long serialVersionUID = 1L; private EditPersonHelper editService = new EditPersonHelper(); private Person personBean; private String [] sports = {&quot;football&quot;, &quot;baseball&quot;, &quot;basketball&quot; }; private String [] genders = {&quot;male&quot;, &quot;female&quot;, &quot;not sure&quot; }; private List<State> states ; private String [] carModelsAvailable = {&quot;Ford&quot;,&quot;Chrysler&quot;,&quot;Toyota&quot;,&quot;Nissan&quot;}; public String execute() throws Exception { editService.savePerson( getPersonBean() ); return SUCCESS; }
  • 28. public String input() throws Exception { setPersonBean( editService.getPerson() ); return INPUT; } public Person getPersonBean() { return personBean; } public void setPersonBean(Person person) { personBean = person; }
  • 29. public List<String> getSports() { return Arrays. asList(sports); } public List<String> getGenders() { return Arrays. asList(genders); } public List<State> getStates() { states = new ArrayList<State>(); states.add( new State(&quot;AZ&quot;, &quot;Arizona&quot;) ); states.add( new State(&quot;CA&quot;, &quot;California&quot;) ); states.add( new State(&quot;FL&quot;, &quot;Florida&quot;) ); states.add( new State(&quot;KS&quot;, &quot;Kansas&quot;) ); states.add( new State(&quot;NY&quot;, &quot;New York&quot;) ); return states; } public String [] getCarModelsAvailable() { return carModelsAvailable; } }
  • 30. Crear edit.xml para registrar Action <?xml version= &quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <!DOCTYPE struts PUBLIC &quot;-//Apache Software Foundation//DTD Struts Configuration 2.0//EN¡± &quot;http://struts.apache.org/dtds/struts-2.0.dtd&quot;> <struts> <constant name= &quot;struts.devMode&quot; value=&quot;false&quot; /> <package name= &quot;editar&quot; extends=&quot;struts-default&quot; namespace=&quot;/ingreso&quot;> <!-- If no class attribute is specified the framework will assume success and render the result index.jsp --> <!-- If no name value for the result node is specified the success value is the default --> <action name= &quot;index&quot;> <result>/index.jsp</result> </action> <action name= &quot;edit&quot; class=&quot;com.aplicacion.presentacion.action.EditAction&quot; method=&quot;input&quot;> <result name= &quot;input&quot;>/ ingreso/formulario.jsp</result> </action> <action name= &quot;save&quot; class=&quot;com.aplicacion.presentacion.action.EditAction&quot; method=&quot;execute&quot;> <result name= &quot;success&quot;>/ ingreso/resultado.jsp</result> </action> </package> </struts>
  • 31. Registrarlo en Struts.xml <include file= &quot;edit.xml&quot;/>