Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Struts B Á Sico
Struts B Á Sico
Struts Bsico
Struts Bsico
Introduccin Introducci
Escuela Tcnica Superior de Ingeniera Informtica Departamento de Lenguajes y Sistemas Informticos
Proyecto Jakarta Estndar de facto Aplicaciones J2EE web MVC2 Servlets + JSP y XML Contenedor Web Vista y Controlador
1. Introduccin Introducci 2. Estructuras 2.1 Struts y MVC2 2.2 Proyecto J2EE 2.3Integrar struts 3. Elementos de Struts 3.1 struts-config.xml struts3.2 Actions 3.3 ActionMappings 3.4 ActionForms 4. Libreras de Librer Etiquetas 4.1 html y logic 4.2 bean y nested 5. Internacionalizacin Internacionalizaci 5.1 i18n en JSP 2 i18n en Actions Bibliografa Bibliograf
Versin original: Autor (mes 200?) ltima revisin: Autor (mes 200?); descripcin cambios.
Tiempo: ?h
Struts Bsico
Struts y MVC2
Escuela Tcnica Superior de Ingeniera Informtica Departamento de Lenguajes y Sistemas Informticos
1. Introduccin Introducci 2. Estructuras 2.1 Struts y MVC2 2.2 Proyecto J2EE 2.3Integrar struts 3. Elementos de Struts 3.1 struts-config.xml struts3.2 Actions 3.3 ActionMappings 3.4 ActionForms 4. Libreras de Librer Etiquetas 4.1 html y logic 4.2 bean y nested 5. Internacionalizacin Internacionalizaci 5.1 i18n en JSP 2 i18n en Actions Bibliografa Bibliograf
Struts Bsico
Estructura de un proyecto
Escuela Tcnica Superior de Ingeniera Informtica Departamento de Lenguajes y Sistemas Informticos
Web J2EE
1. Introduccin Introducci 2. Estructuras 2.1 Struts y MVC2 2.2 Proyecto J2EE 2.3Integrar struts 3. Elementos de Struts 3.1 struts-config.xml struts3.2 Actions 3.3 ActionMappings 3.4 ActionForms 4. Libreras de Librer Etiquetas 4.1 html y logic 4.2 bean y nested 5. Internacionalizacin Internacionalizaci 5.1 i18n en JSP 2 i18n en Actions Bibliografa Bibliograf
Struts Bsico
Integrar Struts
Escuela Tcnica Superior de Ingeniera Informtica Departamento de Lenguajes y Sistemas Informticos
Aadir a Web.xml <servlet> servlet> <servlet> <servlet-name>action</servlet-name> servlet- name> action</servlet- name> <servlet-name>action</servlet-name> <servlet-class> servlet- class> <servlet-class> org.apache.struts.action.ActionServlet org.apache.struts.action.ActionServlet </servlet-class> </servlet-class> </servlet-class> <init-param> init- param> <init-param> <param-name>config</param-name> param- name> config</param- name> <param-name>config</param-name> <param-value> param- value> <param-value> /WEB-INF/struts-config.xml /WEB- INF/struts/WEB-INF/struts-config.xml </param-value> </param-value> </param-value> </init-param> </init-param> </init-param> </servlet> </servlet> </servlet> <servlet-mapping> servlet- mapping> <servlet-mapping> <servlet-name>action</servlet-name> servlet- name> action</servlet- name> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> url- pattern>*.do</url- pattern> <url-pattern>*.do</url-pattern> </servlet-mapping> </servlet-mapping> </servlet-mapping>
1. Introduccin Introducci 2. Estructuras 2.1 Struts y MVC2 2.2 Proyecto J2EE 2.3Integrar struts 3. Elementos de Struts 3.1 struts-config.xml struts3.2 Actions 3.3 ActionMappings 3.4 ActionForms 4. Libreras de Librer Etiquetas 4.1 html y logic 4.2 bean y nested 5. Internacionalizacin Internacionalizaci 5.1 i18n en JSP 2 i18n en Actions Bibliografa Bibliograf
Struts Bsico
Struts-config.xml StrutsEscuela Tcnica Superior de Ingeniera Informtica Departamento de Lenguajes y Sistemas Informticos
1. Introduccin Introducci 2. Estructuras 2.1 Struts y MVC2 2.2 Proyecto J2EE 2.3Integrar struts 3. Elementos de Struts 3.1 struts-config.xml struts3.2 Actions 3.3 ActionMappings 3.4 ActionForms 4. Libreras de Librer Etiquetas 4.1 html y logic 4.2 bean y nested 5. Internacionalizacin Internacionalizaci 5.1 i18n en JSP 2 i18n en Actions Bibliografa Bibliograf
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software <!DOCTYPE strutsstruts-config PUBLIC ""-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" Foundation//DTD Struts Configuration 1.2//EN" Foundation//DTD ""http://struts.apache.org/dtds/struts-config_1_2.dtd"> http://struts.apache.org/dtds/struts-config_1_2.dtd"> http://struts.apache.org/ dtds/ struts- config_1_2.dtd"> <struts-config> struts- config> <struts-config> <data-sources /> <data<data-sources /> <form-beans /> form<form-beans /> <global-exceptions /> <global<global-exceptions /> <global-forwards /> <global<global-forwards /> <action-mappings /> action<action-mappings /> </struts-config> </struts-config> </struts-config>
Struts Bsico
Actions
Escuela Tcnica Superior de Ingeniera Informtica Departamento de Lenguajes y Sistemas Informticos
1. Introduccin Introducci 2. Estructuras 2.1 Struts y MVC2 2.2 Proyecto J2EE 2.3Integrar struts 3. Elementos de Struts 3.1 struts-config.xml struts3.2 Actions 3.3 ActionMappings 3.4 ActionForms 4. Libreras de Librer Etiquetas 4.1 html y logic 4.2 bean y nested 5. Internacionalizacin Internacionalizaci 5.1 i18n en JSP 2 i18n en Actions Bibliografa Bibliograf
public class LoginAction extends Action { public class LoginAction extends Action { public ActionForward execute( public ActionForward execute( execute( ActionMapping mapping, ActionMapping mapping, mapping, ActionForm form, ActionForm form, form, HttpServletRequest request, HttpServletRequest request, request, HttpServletResponse response) HttpServletResponse response) { { // lgica // l lgica } } } }
Struts Bsico
ActionMappings
Escuela Tcnica Superior de Ingeniera Informtica Departamento de Lenguajes y Sistemas Informticos
1. Introduccin Introducci 2. Estructuras 2.1 Struts y MVC2 2.2 Proyecto J2EE 2.3Integrar struts 3. Elementos de Struts 3.1 struts-config.xml struts3.2 Actions 3.3 ActionMappings 3.4 ActionForms 4. Libreras de Librer Etiquetas 4.1 html y logic 4.2 bean y nested 5. Internacionalizacin Internacionalizaci 5.1 i18n en JSP 2 i18n en Actions Bibliografa Bibliograf
<action <action input="/login.jsp input="/login.jsp input="/login.jsp validate=true validate= true validate=true name="loginForm" name="loginForm" name="loginForm" path="/login" path="/login" path="/login" scope="request scope="request scope="request roles=admin,usuario roles= admin,usuario roles=admin,usuario type="com.ejemplo.struts.action.LoginAction"> type="com.ejemplo.struts.action.LoginAction"> type="com.ejemplo.struts.action.LoginAction"> <forward name="incorrecto" path="/Mal.jsp" /> <forward name="incorrecto" path="/Mal.jsp" /> name="incorrecto" path="/Mal.jsp" <forward name="correcto" path="/Bien.jsp" /> <forward name="correcto" path="/Bien.jsp" /> name="correcto" path="/Bien.jsp" </action> </action> </action> return mapping.findForward(correcto"); mapping.findForward(correcto"); return mapping.findForward( correcto");
Struts Bsico
ActionForms
Escuela Tcnica Superior de Ingeniera Informtica Departamento de Lenguajes y Sistemas Informticos
<form-bean name="loginForm form<form-bean name="loginForm name="loginForm type="com.ejemplo.struts.form.LoginForm" /> type="com.ejemplo.struts.form.LoginForm" /> type="com.ejemplo.struts.form.LoginForm" public class LoginForm extends ActionForm { public class LoginForm extends ActionForm { private String passw; private String passw; passw; private String user; private String user; user; public ActionErrors validate ((ActionMappingmapping, public ActionErrors validate ActionMapping mapping, mapping, HttpServletRequest request) {return null;} HttpServletRequest request) {return null;} request) {return null;} public void reset (ActionMapping mapping, ActionMapping mapping, public void reset ((ActionMapping mapping, HttpServletRequest request) {} HttpServletRequest request) {} request) public String getPassw () {return passw;} public String getPassw () {return passw;} {return passw;} public void setPassw(String pass) {this.passw = pass;} public void setPassw(String pass) {this.passw = pass;} {this.passw public String getUser() {return user;} public String getUser() {return user;} getUser() {return user;} public void setUser(String user) {this.user = user;} public void setUser(String user) {this.user = user;} user) {this.user user;} } }
1. Introduccin Introducci 2. Estructuras 2.1 Struts y MVC2 2.2 Proyecto J2EE 2.3Integrar struts 3. Elementos de Struts 3.1 struts-config.xml struts3.2 Actions 3.3 ActionMappings 3.4 ActionForms 4. Libreras de Librer Etiquetas 4.1 html y logic 4.2 bean y nested 5. Internacionalizacin Internacionalizaci 5.1 i18n en JSP 2 i18n en Actions Bibliografa Bibliograf
Struts Bsico
Html: <html:form action="/login.do"> <html:form action="/login.do"> action="/login.do"> password ::<html:text property="password"/> password <html:text property="password"/> <html:text property="password"/> nombre :: <html:text property="user"/> nombre <html:text property="user"/> property="user"/> <html:submit/><html:cancel/> html:submit/><html:cancel/> <html:submit/><html:cancel/> </html:form> </html:form> </html:form> <html:link action="/login.do">Login</html:link> <html:link action="/login.do">Login</html:link> action="/login.do">Login</html:link> Logic: <logic:present scope=session name=cesta> <logic:present scope= session name= cesta scope=session name=cesta> <! Pintar cesta de la compra --> <! Pintar cesta de la compra --> <! --> </logic:present> </logic:present> </logic:present> <logic iterate id=lin name=cesta property=lineas <logic iterate id= lin name= cesta property= lineas id=lin name=cesta property=lineas indexId=linId scope=session> indexId= linId scope= session indexId=linId scope=session> <! Iterar --> <! Iterar --> <! --> </logic:iterate> </logic:iterate> </logic:iterate>
1. Introduccin Introducci 2. Estructuras 2.1 Struts y MVC2 2.2 Proyecto J2EE 2.3Integrar struts 3. Elementos de Struts 3.1 struts-config.xml struts3.2 Actions 3.3 ActionMappings 3.4 ActionForms 4. Libreras de Librer Etiquetas 4.1 html y logic 4.2 bean y nested 5. Internacionalizacin Internacionalizaci 5.1 i18n en JSP 2 i18n en Actions Bibliografa Bibliograf
Struts Bsico
Bean <bean:define id=prod name=lin property=producto> <bean:define id= prod name= lin property= producto id=prod name=lin property=producto> <bean:write name=prod property=titulo/> <bean:write name= prod property= titulo name=prod property=titulo/>
1. Introduccin Introducci 2. Estructuras 2.1 Struts y MVC2 2.2 Proyecto J2EE 2.3Integrar struts 3. Elementos de Struts 3.1 struts-config.xml struts3.2 Actions 3.3 ActionMappings 3.4 ActionForms 4. Libreras de Librer Etiquetas 4.1 html y logic 4.2 bean y nested 5. Internacionalizacin Internacionalizaci 5.1 i18n en JSP 2 i18n en Actions Bibliografa Bibliograf
Struts Bsico
Internacionalizacin Internacionalizaci
Escuela Tcnica Superior de Ingeniera Informtica Departamento de Lenguajes y Sistemas Informticos
1. Introduccin Introducci 2. Estructuras 2.1 Struts y MVC2 2.2 Proyecto J2EE 2.3Integrar struts 3. Elementos de Struts 3.1 struts-config.xml struts3.2 Actions 3.3 ActionMappings 3.4 ActionForms 4. Libreras de Librer Etiquetas 4.1 html y logic 4.2 bean y nested 5. Internacionalizacin Internacionalizaci 5.1 i18n en JSP 2 i18n en Actions Bibliografa Bibliograf
I18n
MVC2 -> textos en interfaz y mensajes generados en controlador
Struts Bsico
i18n en JSP
Escuela Tcnica Superior de Ingeniera Informtica Departamento de Lenguajes y Sistemas Informticos
Alias <message-resources parameter=com.ejemplo.mensajes message<message-resources parameter= com.ejemplo.mensajes parameter=com.ejemplo.mensajes key=gui> key= gui key=gui> Algunos usos <html:option key=error.nohay.articulo bundle=gui/> <html:option key= error.nohay.articulo bundle= gui key=error.nohay.articulo bundle=gui/> <html:img pageKey=error.nohay.articulo <html:img pageKey= error.nohay.articulo pageKey=error.nohay.articulo srcKey=error.nohay.articulo srcKey= error.nohay.articulo srcKey=error.nohay.articulo tittleKey=error.nohay.articulo tittleKey= error.nohay.articulo tittleKey=error.nohay.articulo altKey=error.nohay.articulo bundle=gui/> altKey= error.nohay.articulo bundle= gui altKey=error.nohay.articulo bundle=gui/> <bean:message key=error.nohay.articulo bundle=gui/> <bean:message key= error.nohay.articulo bundle= gui key=error.nohay.articulo bundle=gui/>
1. Introduccin Introducci 2. Estructuras 2.1 Struts y MVC2 2.2 Proyecto J2EE 2.3Integrar struts 3. Elementos de Struts 3.1 struts-config.xml struts3.2 Actions 3.3 ActionMappings 3.4 ActionForms 4. Libreras de Librer Etiquetas 4.1 html y logic 4.2 bean y nested 5. Internacionalizacin Internacionalizaci 5.1 i18n en JSP 2 i18n en Actions Bibliografa Bibliograf
Struts Bsico
1. Introduccin Introducci 2. Estructuras 2.1 Struts y MVC2 2.2 Proyecto J2EE 2.3Integrar struts 3. Elementos de Struts 3.1 struts-config.xml struts3.2 Actions 3.3 ActionMappings 3.4 ActionForms 4. Libreras de Librer Etiquetas 4.1 html y logic 4.2 bean y nested 5. Internacionalizacin Internacionalizaci 5.1 i18n en JSP 2 i18n en Actions Bibliografa Bibliograf
ActionMessages errors = new ActionMessage(); ActionMessages errors = new ActionMessage(); ActionMessage(); Try{ Try{ Try{ //invocar logica de negocio //invocar logica de negocio }catch(Exception ex){ }catch(Exception ex){ errors.add(error,new ActionMessage(error.nohay.arti)); errors.add( error errors.add(error,new ActionMessage( error.nohay.arti ActionMessage(error.nohay.arti)); } } saveErrors(request,errors); saveErrors(request,errors); saveErrors(request,errors);
Y en ActionForms ActionForm
public ActionErrors validate() public ActionErrors validate( validate() { { ActionErrors errors = new ActionErrors(); ActionErrors errors = new ActionErrors(); ActionErrors(); // comparaciones // comparaciones errors.add(user,new ActionMessage(error.falta.user)); errors.add( user errors.add(user,new ActionMessage( error.falta.user ActionMessage(error.falta.user)); return errors; return errors; errors; } }
<html:errors/> html:errors/>
Sevilla, abril de 2007 Grupo de Ingeniera del Software
Struts Bsico
Bibliografa Bibliograf
Escuela Tcnica Superior de Ingeniera Informtica Departamento de Lenguajes y Sistemas Informticos
1. Introduccin Introducci 2. Estructuras 2.1 Struts y MVC2 2.2 Proyecto J2EE 2.3Integrar struts 3. Elementos de Struts 3.1 struts-config.xml struts3.2 Actions 3.3 ActionMappings 3.4 ActionForms 4. Libreras de Librer Etiquetas 4.1 html y logic 4.2 bean y nested 5. Internacionalizacin Internacionalizaci 5.1 i18n en JSP 2 i18n en Actions Bibliografa Bibliograf