You are on page 1of 8

Tiempo: ?

h
escuela técnica superior
de ingeniería informática

Última revisión: Autor (mes 200?); descripción cambios.


Struts Básico

Versión original: Autor (mes 200?)


Departamento de
Lenguajes y Sistemas Informá
Informáticos

Grupo de Ingenierí
Ingeniería del Software
Febrero 2007

[Ángel US V7] Diseño: Amador Durán Toro (2003-2006)

Struts Básico
[Ángel US V7] Diseño: Amador Durán Toro (2003-2006)

• Introducció
Introducción
Escuela Técnica Superior – Proyecto Jakarta
de Ingeniería Informática

– Estándar “de facto”


Departamento de Lenguajes
y Sistemas Informáticos

1. Introducció
Introducción
2. Estructuras – Aplicaciones J2EE web Æ Contenedor Web
2.1 Struts y MVC2
2.2 Proyecto J2EE – MVC2 Æ Vista y Controlador
2.3Integrar struts
3. Elementos de Struts – Servlets + JSP y XML
3.1 struts-
struts-config.xml
3.2 Actions
3.3 ActionMappings
3.4 ActionForms
4. Librerí
Librerías de
Etiquetas
4.1 html y logic
4.2 bean y nested
5. Internacionalizació
Internacionalización
5.1 i18n en JSP
• 2 i18n en Actions
• Bibliografí
Bibliografía

Sevilla, abril de 2007


Grupo de Ingeniería del Software

1
[Ángel US V7] Diseño: Amador Durán Toro (2003-2006) Struts Básico

• Struts y MVC2
Escuela Técnica Superior
de Ingeniería Informática
Departamento de Lenguajes
y Sistemas Informáticos

1. Introducció
Introducción
2. Estructuras
2.1 Struts y MVC2
2.2 Proyecto J2EE
2.3Integrar struts
3. Elementos de Struts
3.1 struts-
struts-config.xml
3.2 Actions
3.3 ActionMappings
3.4 ActionForms
4. Librerí
Librerías de
Etiquetas
4.1 html y logic
4.2 bean y nested
5. Internacionalizació
Internacionalización
5.1 i18n en JSP
• 2 i18n en Actions
• Bibliografí
Bibliografía

Sevilla, abril de 2007


Grupo de Ingeniería del Software

Struts Básico
[Ángel US V7] Diseño: Amador Durán Toro (2003-2006)

• Estructura de un proyecto
Escuela Técnica Superior
de Ingeniería Informática
Web J2EE
Departamento de Lenguajes
y Sistemas Informáticos

1. Introducció
Introducción
2. Estructuras
2.1 Struts y MVC2
2.2 Proyecto J2EE
2.3Integrar struts
3. Elementos de Struts
3.1 struts-
struts-config.xml
3.2 Actions
3.3 ActionMappings
3.4 ActionForms
4. Librerí
Librerías de
Etiquetas
4.1 html y logic
4.2 bean y nested
5. Internacionalizació
Internacionalización
5.1 i18n en JSP
• 2 i18n en Actions
• Bibliografí
Bibliografía

Sevilla, abril de 2007


Grupo de Ingeniería del Software

2
[Ángel US V7] Diseño: Amador Durán Toro (2003-2006) Struts Básico

• Integrar Struts
Escuela Técnica Superior – Añadir a Web.xml
de Ingeniería Informática
Departamento de Lenguajes
y Sistemas Informáticos

1. Introducció
Introducción <<servlet>
servlet>
servlet>
2. Estructuras
<<servlet-name>action</servlet-name>
servlet-
servlet-name>
name>action</
action</servlet
servlet--name>
name>
2.1 Struts y MVC2
2.2 Proyecto J2EE <<servlet-class>
servlet-
servlet-class>
class>
2.3Integrar struts org.apache.struts.action.ActionServlet
org.apache.struts.action.ActionServlet
3. Elementos de Struts
3.1 struts-
struts-config.xml
</servlet
</ servlet--class>
class>
</servlet-class>
3.2 Actions <<init-param>
init-
init-param>
param>
3.3 ActionMappings
3.4 ActionForms
<<param-name>config</param-name>
param-
param-name>
name>config</
config</param
param--name>
name>
4. Librerí
Librerías de <<param-value>
param-
param-value>
value>
Etiquetas
4.1 html y logic
/WEB-
/WEB INF/struts--config.xml
-INF/struts
/WEB-INF/struts-config.xml
4.2 bean y nested </param
</ param--value>
value>
</param-value>
5. Internacionalizació
Internacionalización
5.1 i18n en JSP
</init
</ init--param>
param>
</init-param>


2 i18n en Actions
Bibliografí
Bibliografía
</servlet
</ servlet>
</servlet> >
<<servlet-mapping>
servlet-
servlet-mapping>
mapping>
<<servlet-name>action</servlet-name>
servlet-
servlet-name>
name>action</
action</servlet
servlet--name>
name>
<<url-pattern>*.do</url-pattern>
url-
url-pattern>*.do</
pattern>*.do</url url--pattern>
pattern>
</servlet
</ servlet--mapping>
mapping>
</servlet-mapping>

Sevilla, abril de 2007


Grupo de Ingeniería del Software

Struts Básico
[Ángel US V7] Diseño: Amador Durán Toro (2003-2006)

• Struts-
Struts-config.xml
Escuela Técnica Superior
de Ingeniería Informática
Departamento de Lenguajes
y Sistemas Informáticos

1. Introducció
Introducción <?xml
<? xml version="1.0"
<?xml version ="1.0" encoding="UTF
version="1.0" encoding ="UTF--8"?>
encoding="UTF-8"?>
2. Estructuras
2.1 Struts y MVC2
<!DOCTYPE
<!DOCTYPE struts-config PUBLIC"-
struts-
struts-config PUBLIC -//Apache Software
""-//Apache Software
2.2 Proyecto J2EE Foundation//DTD
Foundation//DTD Struts Configuration1.2//EN"
Foundation //DTD Struts Configuration 1.2//EN"
2.3Integrar struts
3. Elementos de Struts
""http://struts.apache.org/dtds/struts-config_1_2.dtd">
http://
http://struts.apache.org
struts.apache.org//dtds/
dtds/struts-
struts-config_1_2.
config_1_2.dtd
dtd">
">
3.1 struts-
struts-config.xml
3.2 Actions
<<struts-config>
struts-
struts-config>
config>
3.3 ActionMappings
3.4 ActionForms
4. Librerí
Librerías de
Etiquetas
<data-
<data -sources />
<data-sources />
4.1 html y logic <<form-beans
form-
form-beans />/>
4.2 bean y nested
5. Internacionalizació
Internacionalización
<global-
<global -exceptions />
<global-exceptions />
5.1 i18n en JSP <global-
<global -forwards />
<global-forwards />


2 i18n en Actions
Bibliografí
Bibliografía <<action-mappings
action-
action-mappings /> />

</struts
</struts--config>
config>
</struts-config>

Sevilla, abril de 2007


Grupo de Ingeniería del Software

3
[Ángel US V7] Diseño: Amador Durán Toro (2003-2006) Struts Básico

• Actions
Escuela Técnica Superior – Extender la clase Action
de Ingeniería Informática

– Implementar el método execute()


Departamento de Lenguajes
y Sistemas Informáticos

1. Introducció
Introducción
2. Estructuras
2.1 Struts y MVC2
2.2 Proyecto J2EE public
publicclass
classLoginAction
LoginActionextends
extendsAction
Action{{
2.3Integrar struts
3. Elementos de Struts
3.1 struts-
struts-config.xml public
publicActionForward
ActionForwardexecute(
execute
execute((
ActionMapping
ActionMappingmapping, ,
3.2 Actions
3.3 ActionMappings
mapping
mapping,
3.4 ActionForms ActionForm
ActionFormform,
form ,
form,
4. Librerí
Librerías de
Etiquetas HttpServletRequest
HttpServletRequestrequest,
request,
request,
4.1 html y logic HttpServletResponse response)
HttpServletResponse response)
4.2 bean y nested
5. Internacionalizació
Internacionalización
{{
5.1 i18n en JSP //
//ló
lógica
lógica
• 2 i18n en Actions
• Bibliografí
Bibliografía }}

}}

Sevilla, abril de 2007


Grupo de Ingeniería del Software

Struts Básico
[Ángel US V7] Diseño: Amador Durán Toro (2003-2006)

• ActionMappings
Escuela Técnica Superior <<action
action
de Ingeniería Informática
Departamento de Lenguajes
y Sistemas Informáticos
input="/
input ="/login.jsp
login.jsp““
input="/login.jsp“
1. Introducció
Introducción validate=
validate =“true”
true”
validate=“true”
2. Estructuras name="
name ="loginForm
loginForm""
name="loginForm"
2.1 Struts y MVC2
2.2 Proyecto J2EE
path="/
path ="/login
login""
path="/login"
2.3Integrar struts scope="
scope ="request
request““
scope="request“
3. Elementos de Struts
3.1 struts-
struts-config.xml
roles=“
roles= “admin,usuario”
admin,usuario”
roles=“admin,usuario”
3.2 Actions type="
type ="com.ejemplo.struts.action.LoginAction
com.ejemplo.struts.action.LoginAction">
type="com.ejemplo.struts.action.LoginAction"> ">
3.3 ActionMappings
<<forward
forward name="incorrecto"
name="incorrecto" path="/Mal.jsp"/>
name ="incorrecto" path="/
path="/Mal.jsp
Mal.jsp"" />
3.4 ActionForms
4. Librerí
Librerías de <<forward
forward name="correcto"
name ="correcto" path="/
name="correcto" path="/Bien.jsp
Bien.jsp"" />
path="/Bien.jsp" />
Etiquetas
4.1 html y logic
</action
</ action>
</action> >
4.2 bean y nested
5. Internacionalizació
Internacionalización
5.1 i18n en JSP
• 2 i18n en Actions return
returnmapping.findForward(“
mapping.findForward(“correcto");
correcto");
mapping.findForward(“correcto");
• Bibliografí
Bibliografía

<action
<actionforward="/login.jsp
forward="/login.jsp"" path="/login"
forward="/login.jsp" path="/login"/>
/>

Sevilla, abril de 2007


Grupo de Ingeniería del Software

4
[Ángel US V7] Diseño: Amador Durán Toro (2003-2006) Struts Básico
• ActionForms

Escuela Técnica Superior


<<form-bean
form-
form-bean name="
name ="loginForm
loginForm““
name="loginForm“
de Ingeniería Informática
Departamento de Lenguajes type="
type="com.ejemplo.struts.form.LoginForm
com.ejemplo.struts.form.LoginForm"" />
type="com.ejemplo.struts.form.LoginForm" />
y Sistemas Informáticos

1. Introducció
Introducción public
publicclass
classLoginForm
LoginFormextends
extendsActionForm
ActionForm{{
2. Estructuras
2.1 Struts y MVC2
private
privateString
Stringpassw;
passw
passw;;
2.2 Proyecto J2EE private
privateString
Stringuser;
user;
user;
2.3Integrar struts
3. Elementos de Struts
3.1 struts-
struts-config.xml public
publicActionErrors
ActionErrorsvalidate
validate((ActionMapping
ActionMapping mapping,
mapping,
mapping,
3.2 Actions
3.3 ActionMappings
HttpServletRequest
HttpServletRequestrequest)
request ) {return
request) return null;}
{{return null;}
null;}
3.4 ActionForms
4. Librerí
Librerías de
Etiquetas
public
publicvoid
voidreset
reset(ActionMapping
ActionMapping mapping,
((ActionMapping mapping,
mapping,
4.1 html y logic HttpServletRequest
HttpServletRequestrequest)
request) {}
request) {}
4.2 bean y nested
5. Internacionalizació
Internacionalización
5.1 i18n en JSP public
publicString
StringgetPassw
getPassw()
(){return
return passw;}
{{return passw;}
passw;}
• 2 i18n en Actions
• Bibliografí
Bibliografía

public
publicvoid
voidsetPassw(String
setPassw(Stringpass)
pass){this
this.passw
.passw ==pass;}
{{this.passw pass;}

public
publicString
StringgetUser()
getUser() {return
getUser() return user;}
{{return user;}
user;}

public
publicvoid
voidsetUser(String
setUser(Stringuser)
user) {this
user) this.user
.user ==user;}
{{this.user user;}
user;}
Sevilla, abril de 2007 }}
Grupo de Ingeniería del Software

Struts Básico
[Ángel US V7] Diseño: Amador Durán Toro (2003-2006)

• Librerí
Librerías de Etiquetas JSP de Struts (I)
Escuela Técnica Superior – Html:
de Ingeniería Informática
Departamento de Lenguajes
y Sistemas Informáticos
<<html:form
html:form action="/
action ="/login.do
login.do">
">
action="/login.do">
1. Introducció
Introducción
2. Estructuras
password : <html:text
< html:text property="
property ="password
password"/>
"/>
password : <html:text property="password"/>
2.1 Struts y MVC2 nombre : < html:text property="
property="user
user"/>
nombre : <html:text property="user"/> "/>
2.2 Proyecto J2EE
2.3Integrar struts
<<html:submit/><html:cancel/>
html:submit/><
html:submit/><html:cancel
html:cancel/>
/>
3. Elementos de Struts </html:form
</ html:form>
</html:form>>
3.1 struts-
struts-config.xml
3.2 Actions
3.3 ActionMappings <<html:link
html:link action="/
action="/login.do
login.do">
">Login
Login</
</html:link
html:link>
>
action="/login.do">Login</html:link>
3.4 ActionForms
4. Librerí
Librerías de
Etiquetas – Logic:
4.1 html y logic
4.2 bean y nested
5. Internacionalizació
Internacionalización <<logic:present
logic:present scope=
scope =“session”
session” name=
scope=“session” name =“cesta”
cesta”>
name=“cesta”>
5.1 i18n en JSP <!—
<!— Pintar cesta de la compra -->
--
<!— Pintar cesta de la compra --> >
• 2 i18n en Actions
• Bibliografí
Bibliografía </logic:present
</ logic:present>
</logic:present>>

<<logic
logic iterate
iterateid=
id=“lin”
lin” name=
id=“lin” name =“cesta”
cesta” property=
name=“cesta” property =“lineas”
lineas”
property=“lineas”
indexId=
indexId = “ linId”
linId” scope=
scope = “ session”
session
indexId=“linId” scope=“session”> ” >
<!—
<! — Iterar
<!— Iterar-->
-->
-->
</logic:iterate
</ logic:iterate>
</logic:iterate> >
Sevilla, abril de 2007
Grupo de Ingeniería del Software

5
[Ángel US V7] Diseño: Amador Durán Toro (2003-2006) Struts Básico

• Librerí
Librerías de Etiquetas JSP de Struts (II)
Escuela Técnica Superior – Bean
de Ingeniería Informática
Departamento de Lenguajes
y Sistemas Informáticos

1. Introducció
Introducción
2. Estructuras <<bean:define
bean:define id=
id=“prod”
prod” name=
id=“prod” name=“lin”
lin” property=
name=“lin” property=“producto”
producto”>
property=“producto”>
2.1 Struts y MVC2
2.2 Proyecto J2EE
2.3Integrar struts <<bean:write
bean:write name=
name=“prod”
prod” property=
name=“prod” property=“titulo”
titulo”/>
property=“titulo”/>
3. Elementos de Struts
3.1 struts-
struts-config.xml
3.2 Actions
3.3 ActionMappings
3.4 ActionForms – Nested: Para anidaciones no permitidas en JSP
4. Librerí
Librerías de
Etiquetas
4.1 html y logic
4.2 bean y nested
5. Internacionalizació
Internacionalización
5.1 i18n en JSP
• 2 i18n en Actions
• Bibliografí
Bibliografía

Sevilla, abril de 2007


Grupo de Ingeniería del Software

Struts Básico
[Ángel US V7] Diseño: Amador Durán Toro (2003-2006)

• Internacionalizació
Internacionalización
Escuela Técnica Superior – Diferentes idiomas
de Ingeniería Informática

– No duplicación páginas
Departamento de Lenguajes
y Sistemas Informáticos

1. Introducció
Introducción
2. Estructuras – Mantenimiento
2.1 Struts y MVC2
2.2 Proyecto J2EE – Resource Bundle
2.3Integrar struts
3. Elementos de Struts • Fichero properties
3.1 struts-
struts-config.xml
3.2 Actions
• Locale Æ sufijo idioma_pais p.e. mensajes_es_ES
3.3 ActionMappings
• Dentro líneas con asignaciones
3.4 ActionForms
4. Librerí
Librerías de • error.nohay.articulo = No nos queda ese articulo
Etiquetas
4.1 html y logic • Colocar en cualquier paquete com.ejemplo.resources
4.2 bean y nested
5. Internacionalizació
Internacionalización – I18n
5.1 i18n en JSP
• 2 i18n en Actions • MVC2 -> textos en interfaz y mensajes generados en
• Bibliografí
Bibliografía
controlador

Sevilla, abril de 2007


Grupo de Ingeniería del Software

6
[Ángel US V7] Diseño: Amador Durán Toro (2003-2006) Struts Básico

• i18n en JSP
Escuela Técnica Superior – Alias
de Ingeniería Informática
Departamento de Lenguajes
y Sistemas Informáticos
<<message-resources
message-
message-resources parameter=
parameter=“com.ejemplo.mensajes”
com.ejemplo.mensajes”
parameter=“com.ejemplo.mensajes”
1. Introducció
Introducción
2. Estructuras
key=
key=“gui”
gui”>
key=“gui”>
2.1 Struts y MVC2
2.2 Proyecto J2EE – Algunos usos
2.3Integrar struts
3. Elementos de Struts <<html:option
html:option key=
key=“error.nohay.articulo”
error.nohay.articulo” bundle=
key=“error.nohay.articulo” bundle=“gui”
gui”/>
bundle=“gui”/>
3.1 struts-
struts-config.xml
3.2 Actions
3.3 ActionMappings <<html:img
html:img pageKey=
pageKey =“error.nohay.articulo”
error.nohay.articulo”
pageKey=“error.nohay.articulo”
3.4 ActionForms
4. Librerí
Librerías de
srcKey=
srcKey =“error.nohay.articulo”
error.nohay.articulo”
srcKey=“error.nohay.articulo”
Etiquetas
4.1 html y logic
tittleKey=
tittleKey =“error.nohay.articulo”
error.nohay.articulo”
tittleKey=“error.nohay.articulo”
4.2 bean y nested altKey=
altKey = “ error.nohay.articulo”
error.nohay.articulo
altKey=“error.nohay.articulo” ” bundle=
bundle=“gui”
gui”/>
bundle=“gui”/>
5. Internacionalizació
Internacionalización
5.1 i18n en JSP


2 i18n en Actions
Bibliografí
Bibliografía <<bean:message
bean:message key=
key=“error.nohay.articulo”
error.nohay.articulo” bundle=
key=“error.nohay.articulo” bundle=“gui”
gui”/>
bundle=“gui”/>

Sevilla, abril de 2007


Grupo de Ingeniería del Software

Struts Básico
[Ángel US V7] Diseño: Amador Durán Toro (2003-2006)

• i18n en Action’
Action’s
Escuela Técnica Superior
de Ingeniería Informática ActionMessages
ActionMessageserrors
errors==new
newActionMessage();
ActionMessage ();
ActionMessage();
Departamento de Lenguajes
y Sistemas Informáticos Try{
Try {
Try{
1. Introducció
Introducción //invocar
//invocarlogica
logicade
denegocio
negocio
2. Estructuras
2.1 Struts y MVC2
}}catch(Exception
catch(Exception ex){
ex){
2.2 Proyecto J2EE errors.add(
errors.add (“error”
error”,new ActionMessage(
errors.add(“error”,new ActionMessage (“error.nohay.arti”
error.nohay.arti”));
ActionMessage(“error.nohay.arti”));
2.3Integrar struts
3. Elementos de Struts
}}
3.1 struts-
struts-config.xml saveErrors(request,errors);
saveErrors(request,errors
saveErrors(request,errors););
3.2 Actions
3.3 ActionMappings
3.4 ActionForms
• Y en ActionForm’
ActionForm’s
4. Librerí
Librerías de
Etiquetas public
publicActionErrors
ActionErrorsvalidate(
validate (…)
validate(…)
4.1 html y logic {{
4.2 bean y nested
5. Internacionalizació
Internacionalización
ActionErrors
ActionErrorserrors
errors==new
newActionErrors();
ActionErrors();
ActionErrors();
5.1 i18n en JSP // comparaciones
// comparaciones
• 2 i18n en Actions
• Bibliografí
Bibliografía errors.add(
errors.add (“user”
user”,new ActionMessage(
errors.add(“user”,new ActionMessage (“error.falta.user”
error.falta.user”));
ActionMessage(“error.falta.user”));
return
returnerrors;
errors ;
errors;
}}

• <html:errors/>
html:errors/>
Sevilla, abril de 2007
Grupo de Ingeniería del Software

7
[Ángel US V7] Diseño: Amador Durán Toro (2003-2006) Struts Básico

• Bibliografí
Bibliografía
Escuela Técnica Superior – Programming Jakarta Struts
de Ingeniería Informática

O’reilly
Departamento de Lenguajes
y Sistemas Informáticos

1. Introducció
Introducción
2. Estructuras
2.1 Struts y MVC2
2.2 Proyecto J2EE
2.3Integrar struts
3. Elementos de Struts
3.1 struts-
struts-config.xml
3.2 Actions – Struts in Action
3.3 ActionMappings
3.4 ActionForms Manning
4. Librerí
Librerías de
Etiquetas
4.1 html y logic
4.2 bean y nested
5. Internacionalizació
Internacionalización
5.1 i18n en JSP
• 2 i18n en Actions
• Bibliografí
Bibliografía
– Struts Recipes
Manning

Sevilla, abril de 2007


Grupo de Ingeniería del Software

You might also like