0% found this document useful (0 votes)
108 views6 pages

Public Class Private Private: Title Genre

The document describes how to create a Struts 2 application that: 1. Defines beans and an action class to display album details including the artist name, bio, album title, and songs. 2. Configures the action in struts.xml and sets the home page. 3. Creates a JSP to display the album details, using Struts tags to iterate over the songs. The document then discusses: 4. Grouping multiple methods in a single action class and invoking them dynamically based on parameters. Configuration involves mapping each method to a unique action in struts.xml. 5. Modifying struts.xml to invoke action methods dynamically based on parameters rather than explicitly

Uploaded by

Leo War
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views6 pages

Public Class Private Private: Title Genre

The document describes how to create a Struts 2 application that: 1. Defines beans and an action class to display album details including the artist name, bio, album title, and songs. 2. Configures the action in struts.xml and sets the home page. 3. Creates a JSP to display the album details, using Struts tags to iterate over the songs. The document then discusses: 4. Grouping multiple methods in a single action class and invoking them dynamically based on parameters. Configuration involves mapping each method to a unique action in struts.xml. 5. Modifying struts.xml to invoke action methods dynamically based on parameters rather than explicitly

Uploaded by

Leo War
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Struts 2: Utilizando tags de control (de bucle y condicional) 1.

Crear una clase de tipo action con nombre AlbumInfoAction que permita inicializar y mostrar datos sobre un cantante y alguna de sus canciones. Crear los siguientes beans:

public class Song { private String title; private String genre; public Song(String title, String genre) { this.title = title; this.genre = genre; }

Y
public class Artist { private String name; private String bio; public Artist(String name, String bio) { this.name = name; this.bio = bio; }

No olvidar generar sus mtodos get y set. Crear la clase action:


import java.util.ArrayList; import java.util.List; public class AlbumInfoAction{ private String title; private Artist artist; private static List<Song> songs = new ArrayList<Song>(); static { songs.add(new Song("Thriller","Disco")); songs.add(new Song("Beat It","Rock")); songs.add(new Song("Billie Jean","Pop")); } public String populate() { title = "Thriller"; artist = new Artist("Michael Jackson","King of pop"); return "populate"; }

public String execute() { return "success"; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public Artist getArtist() { return artist; } public void setArtist(Artist artist) { this.artist = artist; } public List<Song> getSongs() { return songs; } }

2. E en el archivo struts.xml configuramos:

<struts> <package name="default" extends="struts-default"> <action name="*AlbumAction" method="{1}" class="actions.AlbumInfoAction"> <result name="populate">/albumDetails.jsp</result> </action> </package> </struts>

3. En el web.xml configuramos la pgina de inicio

<welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> El jsp tendr:

<html> <head> <META HTTP-EQUIV="Refresh" CONTENT="0;URL=populateAlbumAction.action"> </head> <body> </body> </html>

4. Crear albumDetails.jsp que muestre todos los datos del artista. Para mostrar las canciones hacer uso del tag iterator.

<%@taglib uri="/struts-tags" prefix="s"%> <html> <head> <s:head /> <style type="text/css"> @import url(style.css); </style> <meta http-equiv="Content-Type" content="text/html; charset=ISO-88591"> <title>Album Details</title> </head> <body> <div class="content"><b>Album Title :</b> <s:property value="title" /> <br> <b>Artist Name :</b> <s:property value="artist.name" /> <br> <b>Artist Bio :</b> <s:property value="artist.bio" /> <br> Song Details <table class="songTable" border="1"> <tr > <td><b>Title</b></td> <td><b>Genre</b></td> </tr> <s:iterator value="songs"> <tr> <td><s:property value="title" /></td> <td><s:property value="genre" /></td> </tr> </s:iterator> </table> </div> </body> </html>

5. Las filas impares de la tabla de canciones mostrarlas de un color diferente al de las pares. Hacer uso del tag if.

<s:iterator value="songs" status="songStatus"> <tr class='<s:if test="#songStatus.odd==true">odd</s:if><s:else>even</s:else>'> <td><s:property value="title" /></td> <td><s:property value="genre" /></td> </tr> </s:iterator>

Nota: propiedades del objeto IteratorStatus

Struts 2: Utilizar un solo action que agrupe varias funciones. 1. Crear el siguiente action:
public class UserAction extends ActionSupport{ private String message; public String execute() { message = "Dentro del mtodo execute "; return SUCCESS; } public String add() { message = " Dentro del mtodo add "; return SUCCESS; } public String update() { message = " Dentro del mtodo update "; return SUCCESS; } public String delete() { message = "Dentro del mtodo delete "; return SUCCESS; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }

2. En el archivo struts.xml:
<struts> <package name="default" extends="struts-default"> <action name="User" class="actions.UserAction"> <result name="success">/success.jsp</result> </action> <action name="addUser" method="add" class=" actions.UserAction"> <result name="success">/success.jsp</result> </action> <action name="updateUser" method="update" class=" actions.UserAction"> <result name="success">/success.jsp</result> </action> <action name="deleteUser" method="delete" class=" actions.UserAction"> <result name="success">/success.jsp</result> </action> </package> </struts>

3. En el web.xml configuramos la pgina de inicio

<welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> El jsp tendr:

<%@taglib uri="/struts-tags" prefix="s" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-88591"> <title>Insert title here</title> </head> <body> <s:form action="User" > <s:submit /> <s:submit action="addUser" value="Agregar" /> <s:submit action="updateUser" value="Actualizar" /> <s:submit action="deleteUser" value="Eliminar" /> </s:form> </body> </html>

4. Crear success.jsp
<html> <head> <title>Insert title here</title> </head> <body> ${message}

</body> </html>

Struts 2: Invocando mtodos dinmicamente 1. Modificar el archivo struts.xml de la siguiente forma:


<struts> <package name="default" extends="struts-default"> <action name="*User" method="{1}" class="actions.UserAction"> <result name="success">/success.jsp</result> </action> </package> </struts>

You might also like