<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>rails y más cosas &#187; struts</title>
	<atom:link href="http://www.railsymas.com/category/struts/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.railsymas.com</link>
	<description>Nuevas Tecnologías</description>
	<lastBuildDate>Fri, 30 Jul 2010 22:45:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Integración Struts hibernate</title>
		<link>http://www.railsymas.com/2010/06/25/integracion-struts-hibernate/</link>
		<comments>http://www.railsymas.com/2010/06/25/integracion-struts-hibernate/#comments</comments>
		<pubDate>Fri, 25 Jun 2010 18:14:03 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[struts]]></category>

		<guid isPermaLink="false">http://www.railsymas.com/?p=1663</guid>
		<description><![CDATA[En este ejemplo se inserta un tupla en la base de datos creada, con los datos introducidos mediante un formulario rellenado por el usuario Tenemos la tabla libros de la base de datos libros_jdbc, tal como viene en este ejemplo base de datos. Tenemos una tabla libros pertenecientes a la base datos con tres campos [...]]]></description>
			<content:encoded><![CDATA[<p>   En este ejemplo se inserta un tupla en la base de datos creada, con los datos introducidos mediante un formulario rellenado por el usuario</p>
<p>  <span id="more-1663"></span></p>
<p>  Tenemos la tabla libros de la base de datos libros_jdbc, tal como viene en este ejemplo <a href="http://www.railsymas.com/2009/09/09/conexion-a-bases-de-datos-j2ee-jdbc/">base de datos</a>. Tenemos una tabla libros pertenecientes a la base datos con tres campos  el titulo, autor y la temática del libro.</p>
<p>Para este ejemplo necesitamos para un mejor entendimiento necesitamos conocer como se crea un plugin y aquí tenemos uno hecho <a href="http://www.railsymas.com/2010/05/23/creacion-de-un-plugin-con-struts-xml-dom/">creación de plugin</a></p>
<p> Formulario de recogida de datos <a href="http://www.railsymas.com/wp-content/uploads/2010/06/formulahiber.png"><img src="http://www.railsymas.com/wp-content/uploads/2010/06/formulahiber.png" alt="" title="formulahiber" width="580" height="280" class="alignnone size-full wp-image-1668" /></a></p>
<p>este formumario esta asociado a una acción recogida en el fichero de configuración de struts el struts-config.xml</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span></span>
&nbsp;
<span style="color: #009900;">          <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/formularioHibernate&quot;</span></span>
<span style="color: #009900;">          <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;acciones.AccionHibernateLibro&quot;</span></span>
<span style="color: #009900;">          <span style="color: #000066;">scope</span>=<span style="color: #ff0000;">&quot;request&quot;</span></span>
<span style="color: #009900;">          <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hibernatelibro&quot;</span></span>
<span style="color: #009900;">          <span style="color: #000066;">validate</span>=<span style="color: #ff0000;">&quot;true&quot;</span></span>
<span style="color: #009900;">          <span style="color: #000066;">input</span>=<span style="color: #ff0000;">&quot;/pages/formulariohibernate.jsp&quot;</span></span>
<span style="color: #009900;">         <span style="color: #000000; font-weight: bold;">&gt;</span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;set-property</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;cancellable&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span> 
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;forward</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;listado&quot;</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/pages/listadohibernate.jsp&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;forward</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;cancelada&quot;</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/pages/cancelada2.jsp&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/action<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Se ha explicado en otros post anteriores cada uno de los elementos que aparecen en  esta configuración, pero a modo de recordatorio, se enumeran los valores de los diferentes campos. Para el path tenemos el nombre de la acción que se  ejecuta en este caso (formulariohibernate), esta acción esta recogida en una clase (AccionHibernateLibro) que es la encargada de evaluar y procesar los datos, el name hace referencia al bean en este caso dinámico encargado de recoger los datos del formulario (hibernatelibro).</p>
<p>Código del bean dinámico encargado de recoger los datos del formulario, para poder ser tratados por la clase (AcciónHibernateLibro)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form-bean</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hibernatelibro&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;org.apache.struts.validator.DynaValidatorForm&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>    
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form-property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;titulo&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;java.lang.String&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form-property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;tematica&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;java.lang.String&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form-property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;autor&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;java.lang.String&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/form-bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Código del formulario formulariohibernate.jsp correspondiente a la imagen anterior</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;%</span>@ page language<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;java&quot;</span> contentType<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/html; charset=UTF-8&quot;</span>  pageEncoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;%</span>@taglib uri<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://struts.apache.org/tags-html&quot;</span> prefix<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;html&quot;</span> <span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;%</span>@taglib uri<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://struts.apache.org/tags-bean&quot;</span> prefix<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;bean&quot;</span> <span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;!</span>DOCTYPE html <span style="color: #000000; font-weight: bold;">PUBLIC</span> <span style="color: #0000ff;">&quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;</span> <span style="color: #0000ff;">&quot;http://www.w3.org/TR/html4/loose.dtd&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>html<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>base<span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>meta http<span style="color: #339933;">-</span>equiv<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Content-Type&quot;</span> content<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/html; charset=UTF-8&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>link rel<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;stylesheet&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;../css/estilo.css&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/css&quot;</span><span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;&lt;</span>bean<span style="color: #339933;">:</span>message key<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;titulo.hibernate&quot;</span><span style="color: #339933;">/&gt;&lt;/</span>title<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;contenedor&quot;</span><span style="color: #339933;">&gt;</span>
 <span style="color: #339933;">&lt;</span>h2<span style="color: #339933;">&gt;&lt;</span>bean<span style="color: #339933;">:</span>message key<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;titulo.hibernate&quot;</span><span style="color: #339933;">/&gt;&lt;/</span>h2<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;errores&quot;</span><span style="color: #339933;">&gt;</span>
 <span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>errors<span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;imagen&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>img srcKey<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;libro.imagen&quot;</span> titleKey<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;libro.imagen.alt&quot;</span><span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;contenido&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
   <span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>form method<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;post&quot;</span> action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;formularioHibernate&quot;</span> styleId<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;formulario&quot;</span> focus<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;titulo&quot;</span> <span style="color: #339933;">&gt;</span>
&nbsp;
   <span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;campo&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
   <span style="color: #339933;">&lt;</span>bean<span style="color: #339933;">:</span>message key<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;formulariodatasource.titulolibro&quot;</span><span style="color: #339933;">/&gt;</span>
&nbsp;
   <span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>text property<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;titulo&quot;</span><span style="color: #339933;">/&gt;</span>
&nbsp;
   <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
&nbsp;
   <span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;campo&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
   <span style="color: #339933;">&lt;</span>bean<span style="color: #339933;">:</span>message key<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;formulariodatasource.autor&quot;</span><span style="color: #339933;">/&gt;</span>
&nbsp;
   <span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>text property<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;autor&quot;</span><span style="color: #339933;">/&gt;</span>
&nbsp;
   <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
&nbsp;
   <span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;campo&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
   <span style="color: #339933;">&lt;</span>bean<span style="color: #339933;">:</span>message key<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;formulariodatasource.tematica&quot;</span><span style="color: #339933;">/&gt;</span>
&nbsp;
   <span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>text property<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;tematica&quot;</span><span style="color: #339933;">/&gt;</span>
&nbsp;
   <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
&nbsp;
   <span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>submit styleClass<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;boton&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;enviar&quot;</span><span style="color: #339933;">/&gt;</span>
   <span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>cancel styleClass<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;boton&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;cancelar&quot;</span><span style="color: #339933;">/&gt;</span>
   <span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>reset  styleClass<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;boton&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;borrar&quot;</span><span style="color: #339933;">/&gt;</span> 
&nbsp;
&nbsp;
   <span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">:</span>form<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;pie&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span> www.<span style="color: #006633;">railsymas</span>.<span style="color: #006633;">com</span> <span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">:</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>configuración del plugin en el fichero  struts-config.xml, creamos un plugin indicando la clase HibernatePlugin y la propiedad dirección con el valor de la ruta del fichero de configuración de hibernate</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"> <span style="color: #808080; font-style: italic;">&lt;!-- ========== Hibernate ======= --&gt;</span>
&nbsp;
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plug-in</span> <span style="color: #000066;">className</span>=<span style="color: #ff0000;">&quot;configuracion.plugin.HibernatePlugin&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;set-property</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;direccion&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;/hibernate.cfg.xml&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plug-in<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>para cargar el fichero de configuración de hibernate creamos un plugin, como repaso tenemos que extender de la clase Plugin y sobre escribimos los métodos destroy e init. Se crea por comodidad una variable estática de la clase llamada clave_hibernate que nos permitirá recoger la factoria de hibernate para poder insertar, borrar, editar etc..</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">configuracion.plugin</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.ServletContext</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.ServletException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.SessionFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.cfg.Configuration</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionServlet</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.PlugIn</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.config.ModuleConfig</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.HibernateException</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HibernatePlugin <span style="color: #000000; font-weight: bold;">implements</span> PlugIn
 <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> direccion<span style="color: #339933;">;</span>
&nbsp;
&nbsp;
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> clave_hibernate <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Hibernate&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> SessionFactory factoria <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getDireccion<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> direccion<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setDireccion<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> direccion<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">direccion</span> <span style="color: #339933;">=</span> direccion<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> destroy<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #000000; font-weight: bold;">try</span>
     <span style="color: #009900;">&#123;</span>
       factoria.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
     <span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span>HibernateException e<span style="color: #009900;">&#41;</span>
     <span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;No se puede cerrar la sesión: &quot;</span> <span style="color: #339933;">+</span> e.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> init<span style="color: #009900;">&#40;</span>ActionServlet servlet, ModuleConfig config<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ServletException
  <span style="color: #009900;">&#123;</span>
        Configuration configuration <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
        ServletContext contexto <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span>
&nbsp;
          contexto <span style="color: #339933;">=</span> servlet.<span style="color: #006633;">getServletContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          configuration <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Configuration<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">configure</span><span style="color: #009900;">&#40;</span>direccion<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          factoria <span style="color: #339933;">=</span> configuration.<span style="color: #006633;">buildSessionFactory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
          contexto.<span style="color: #006633;">setAttribute</span><span style="color: #009900;">&#40;</span>clave_hibernate, factoria<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span>HibernateException e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error de inicialización de hibernate: &quot;</span> <span style="color: #339933;">+</span> e.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
   <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>código de la clase encargada de recoger la acción, esta clase como solamente realiza una acción extendemos de action y editamos el método ejecute. Esta clase es la encargada de recoger los datos del formulario así como de la utilización de recuperar el manejador de la factoria.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">acciones</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.Action</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionMapping</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletRequest</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletResponse</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.ServletContext</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionForm</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.validator.DynaValidatorForm</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionForward</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">configuracion.plugin.HibernatePlugin</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.SessionFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.Session</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.Transaction</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.TransactionException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">modelos.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Iterator</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> AccionHibernateLibro <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">Action</span> <span style="color: #009900;">&#123;</span>
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> ActionForward reenvio <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> DynaValidatorForm formulario <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> Session sesion <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> SessionFactory  factory<span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> Transaction transaction <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> ActionForward execute <span style="color: #009900;">&#40;</span>ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span>
	<span style="color: #009900;">&#123;</span>   
&nbsp;
&nbsp;
		formulario <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>DynaValidatorForm<span style="color: #009900;">&#41;</span> form<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isCancelled<span style="color: #009900;">&#40;</span>request<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			reenvio <span style="color: #339933;">=</span> mapping.<span style="color: #006633;">findForward</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;cancelada&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">else</span>
		<span style="color: #009900;">&#123;</span>   
		    ServletContext context <span style="color: #339933;">=</span> request.<span style="color: #006633;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getServletContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		    factory <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>SessionFactory<span style="color: #009900;">&#41;</span> context.<span style="color: #006633;">getAttribute</span><span style="color: #009900;">&#40;</span>HibernatePlugin.<span style="color: #006633;">clave_hibernate</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			sesion <span style="color: #339933;">=</span>  factory.<span style="color: #006633;">openSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">try</span>
			<span style="color: #009900;">&#123;</span> 
				transaction <span style="color: #339933;">=</span> sesion.<span style="color: #006633;">beginTransaction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				Libros libro <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Libros<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #666666; font-style: italic;">//introducimos los valores para el libro nuevo</span>
				libro.<span style="color: #006633;">setTitulo</span><span style="color: #009900;">&#40;</span>formulario.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;titulo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				libro.<span style="color: #006633;">setAutor</span><span style="color: #009900;">&#40;</span>formulario.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;autor&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				libro.<span style="color: #006633;">setTematica</span><span style="color: #009900;">&#40;</span>formulario.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;tematica&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
				sesion.<span style="color: #006633;">save</span><span style="color: #009900;">&#40;</span>libro<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>						
				transaction.<span style="color: #006633;">commit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				transaction <span style="color: #339933;">=</span> sesion.<span style="color: #006633;">beginTransaction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #003399;">List</span> <span style="color: #339933;">&lt;</span>Libros<span style="color: #339933;">&gt;</span> libros <span style="color: #339933;">=</span>  sesion.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;from Libros&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">list</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #003399;">Iterator</span> <span style="color: #339933;">&lt;</span>Libros<span style="color: #339933;">&gt;</span> iterador <span style="color: #339933;">=</span> libros.<span style="color: #006633;">iterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span> iterador.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span>
				<span style="color: #009900;">&#123;</span>
					 libro <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Libros<span style="color: #009900;">&#41;</span> iterador.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
					<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;titulo:&quot;</span><span style="color: #339933;">+</span>libro.<span style="color: #006633;">getTitulo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;autor:&quot;</span><span style="color: #339933;">+</span>libro.<span style="color: #006633;">getAutor</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;tematica:&quot;</span><span style="color: #339933;">+</span>libro.<span style="color: #006633;">getTematica</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
&nbsp;
				<span style="color: #009900;">&#125;</span>
&nbsp;
				transaction.<span style="color: #006633;">commit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			    request.<span style="color: #006633;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;libros&quot;</span>, libros<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			    reenvio <span style="color: #339933;">=</span> mapping.<span style="color: #006633;">findForward</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;listado&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>TransactionException e  <span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span> 
&nbsp;
				<span style="color: #666666; font-style: italic;">//abortamos la transacción y restauramos los valores anteriores</span>
				transaction.<span style="color: #006633;">rollback</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			    e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   
&nbsp;
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000000; font-weight: bold;">finally</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #666666; font-style: italic;">//cerramos la sesión</span>
				sesion.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> reenvio<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>archivo de configuración hibernate.cfg,xml</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #00bbdd;">&lt;!DOCTYPE hibernate-configuration PUBLIC</span>
<span style="color: #00bbdd;">		&quot;-//Hibernate/Hibernate Configuration DTD 3.0//EN&quot;</span>
<span style="color: #00bbdd;">		&quot;http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd&quot;&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;hibernate-configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;session-factory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hibernate.connection.driver_class&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>com.mysql.jdbc.Driver<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hibernate.connection.username&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>root<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hibernate.connection.password&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>root<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hibernate.connection.url&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span> jdbc:mysql://localhost:3306/libros_jdbc<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hibernate.dialect&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>org.hibernate.dialect.MySQL5Dialect<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hibernate.hbm2ddl.auto&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>update<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;show_sql&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>true<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;format_sql&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>true<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mapping</span> <span style="color: #000066;">resource</span>=<span style="color: #ff0000;">&quot;modelos/Libros.hbm.xml&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>       
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/session-factory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/hibernate-configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>sobre los parámetros recogidos en el fichero hibernate.cfg.xml se comentaron en el post <a href="http://www.railsymas.com/2010/06/16/hibernate-tools-para-eclipse/">hibernate tools</a></p>
<p>Para el mapeo de las tablas tenemos que crear un archivo xml con extension hbm.xml. estos archivos los podemos poner en un package propio por en este ejemplo &#8220;modelo&#8221; haciendo referencia a las las clases y ficheros de configuración asociados a las tablas de la base de datos.</p>
<p>Al ejecutar la aplicación se nos crea automáticamente una clase bean con los propiedades indicadas en los ficheros hbm.xml, en este caso una clase Libro</p>
<p>Para crear un fichero hbm vamos con la vista hibernate de eclipse activada a new, hibernate xml mapping creamos un fichero libros.hbm.xml</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #00bbdd;">&lt;!DOCTYPE hibernate-mapping PUBLIC &quot;-//Hibernate/Hibernate Mapping DTD 3.0//EN&quot;</span>
<span style="color: #00bbdd;">&quot;http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd&quot;&gt;</span>
<span style="color: #808080; font-style: italic;">&lt;!-- Generated 26-ene-2010 1:33:06 by Hibernate Tools 3.2.4.GA --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;hibernate-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;class</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;modelos.Libros&quot;</span> <span style="color: #000066;">table</span>=<span style="color: #ff0000;">&quot;libros&quot;</span> <span style="color: #000066;">catalog</span>=<span style="color: #ff0000;">&quot;libros_jdbc&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;id&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;java.lang.Integer&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;column</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;id&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;generator</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;identity&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;titulo&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;string&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;column</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;titulo&quot;</span> <span style="color: #000066;">length</span>=<span style="color: #ff0000;">&quot;50&quot;</span> <span style="color: #000066;">not-null</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;autor&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;string&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;column</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;autor&quot;</span> <span style="color: #000066;">length</span>=<span style="color: #ff0000;">&quot;50&quot;</span> <span style="color: #000066;">not-null</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;tematica&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;string&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;column</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;tematica&quot;</span> <span style="color: #000066;">length</span>=<span style="color: #ff0000;">&quot;50&quot;</span> <span style="color: #000066;">not-null</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/hibernate-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Valores de los parámetros, table hace referencia a la tabla de la base de datos, catalog hace referencia a la base de datos, el primer parametro id indicamos que el campo es la clave primaria, los atributos property indicamos el nombre de la propiedad y a que campo de la tabla corresponde es decir podemos tener el campo titulo en la tabla y en la propiedad  titu_x por decir algo, entoces a la hora de usar por código el objeto de mapeo haríamos objeto.titu_x pero por comodidad le damos el mismo valor que la tabla así es más fácil acordorse de las propiedades. </p>
<p>Imagen del listado del libro una vez guardado los datos </p>
<p><a href="http://www.railsymas.com/wp-content/uploads/2010/06/listadohibernateli.png"><img src="http://www.railsymas.com/wp-content/uploads/2010/06/listadohibernateli.png" alt="" title="listadohibernateli" width="580" height="210" class="alignnone size-full wp-image-1690" /></a></p>
<p>Salida por pantalla para verificar la inserción como consecuencia de activar en el fichero hibernate.cfg.xml el parámetro show_sql y format_sql</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;">INFO: Tiles definition factory found for request processor ''.
Hibernate: 
    insert 
    into
        libros_jdbc.libros
        (titulo, autor, tematica) 
    values
        (?, ?, ?)
Hibernate: 
    select
        libros0_.id as id0_,
        libros0_.titulo as titulo0_,
        libros0_.autor as autor0_,
        libros0_.tematica as tematica0_ 
    from
        libros_jdbc.libros libros0_
titulo:titulo3
autor:autor3
tematica:tematica3</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.railsymas.com/2010/06/25/integracion-struts-hibernate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creación de un plugin con struts xml dom</title>
		<link>http://www.railsymas.com/2010/05/23/creacion-de-un-plugin-con-struts-xml-dom/</link>
		<comments>http://www.railsymas.com/2010/05/23/creacion-de-un-plugin-con-struts-xml-dom/#comments</comments>
		<pubDate>Sun, 23 May 2010 10:04:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[struts]]></category>

		<guid isPermaLink="false">http://www.railsymas.com/?p=1474</guid>
		<description><![CDATA[Tutorial de struts para la creación de un plugin en struts Struts nos permite la creación de plugins propios, en los diferentes post utilizabamos plugins standard como struts-validator y struts-tiles. Pero también se pueden crear plugins a medida de nuestras necesidades Tenemos un fichero xml, donde tenemos una estructura un elemento nodo raiz llamado plantilla [...]]]></description>
			<content:encoded><![CDATA[<p>Tutorial de struts para la creación de un plugin en struts<br />
<span id="more-1474"></span></p>
<p>Struts nos permite la creación de plugins propios, en los diferentes post utilizabamos plugins standard como struts-validator y struts-tiles. Pero también se pueden crear plugins a medida de nuestras necesidades</p>
<p>Tenemos un fichero xml, donde tenemos una estructura un elemento nodo raiz llamado plantilla y una serie de nodos hijos llamados obrero, cada elemento hijo consta de un atributo nombre y dos elementos subhijos cargo y edad</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plantilla<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>  
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;obrero</span> <span style="color: #000066;">nombre</span>=<span style="color: #ff0000;">&quot;pepe&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cargo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>peón<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cargo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;edad<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>25<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/edad<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/obrero<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;obrero</span> <span style="color: #000066;">nombre</span>=<span style="color: #ff0000;">&quot;juan&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cargo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>capataz<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cargo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;edad<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>46<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/edad<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/obrero<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;obrero</span> <span style="color: #000066;">nombre</span>=<span style="color: #ff0000;">&quot;maria&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cargo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>pintor<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cargo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;edad<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>38<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/edad<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/obrero<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plantilla<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Este fichero xml  lo denominamos plantillas.xml y será el fichero que lea el plugin de struts.</p>
<p>Para la creación del plugin debemos de declararlo en el fichero struts-config.xml</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!-- =============================================== Plug Ins Configuration --&gt;</span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plug-in</span> <span style="color: #000066;">className</span>=<span style="color: #ff0000;">&quot;acciones.PluginEjemplo&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span> 
&nbsp;
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;set-property</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;direccion&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;/WEB-INF/plantilla.xml&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plug-in<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Por un lado tenemos el nombre el de la clase del plugin,  en el valor del atributo className  y por otro el valor de la propiedad dirección en el que indicamos la ruta a la dirección del fichero xml </p>
<p>La clase que implementa la interface Plugin, debe implementar los métodos init y destroy, si se pasan parámetros en la declaración del plugin en este caso dirección con el valor de la ruta al fichero para poder asignar valor y recuperar desde la clase del plugin es necesario crear métodos getter y setter</p>
<p>El resto de los métodos son los encargados de leer el fichero xml</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">acciones</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.File</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.ServletContext</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.ServletException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.PlugIn</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionServlet</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.config.ModuleConfig</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.parsers.DocumentBuilderFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.w3c.dom.Document</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.w3c.dom.Element</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.w3c.dom.Node</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.w3c.dom.NodeList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.xml.sax.SAXException</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.parsers.DocumentBuilder</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.parsers.ParserConfigurationException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">vo.Obrero</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span>  <span style="color: #000000; font-weight: bold;">class</span> PluginEjemplo <span style="color: #000000; font-weight: bold;">implements</span> PlugIn
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> nombreclase <span style="color: #339933;">=</span> PluginEjemplo.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> direccion<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span>  <span style="color: #003399;">Document</span> documento<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> ArrayList<span style="color: #339933;">&lt;</span>Obrero<span style="color: #339933;">&gt;</span> listadoObreros <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Obrero<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getDireccion<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> direccion<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setDireccion<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> direccion<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">direccion</span> <span style="color: #339933;">=</span> direccion<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> init <span style="color: #009900;">&#40;</span>ActionServlet servlet, ModuleConfig config<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ServletException
	<span style="color: #009900;">&#123;</span>
&nbsp;
		ServletContext  contexto <span style="color: #339933;">=</span> servlet.<span style="color: #006633;">getServletContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
&nbsp;
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>nombreclase<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;direccion:&quot;</span><span style="color: #339933;">+</span>getDireccion<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		PluginEjemplo pluginejemplo <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PluginEjemplo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		leerXml <span style="color: #009900;">&#40;</span>getDireccion<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,servlet<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		contexto.<span style="color: #006633;">setAttribute</span><span style="color: #009900;">&#40;</span>nombreclase,pluginejemplo <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span>  <span style="color: #000066; font-weight: bold;">void</span> leerXml<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> direccion, ActionServlet servlet<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		DocumentBuilderFactory factoria <span style="color: #339933;">=</span> DocumentBuilderFactory.<span style="color: #006633;">newInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">try</span>
		<span style="color: #009900;">&#123;</span>
           DocumentBuilder constructor <span style="color: #339933;">=</span> factoria.<span style="color: #006633;">newDocumentBuilder</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
           ServletContext contexto <span style="color: #339933;">=</span> servlet.<span style="color: #006633;">getServletContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>           
           <span style="color: #003399;">String</span> lugar <span style="color: #339933;">=</span> contexto.<span style="color: #006633;">getRealPath</span><span style="color: #009900;">&#40;</span>direccion<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
           documento <span style="color: #339933;">=</span> constructor.<span style="color: #006633;">parse</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">File</span><span style="color: #009900;">&#40;</span>lugar<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
           documento.<span style="color: #006633;">getDocumentElement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">normalize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
           Node nodoraiz <span style="color: #339933;">=</span> documento.<span style="color: #006633;">getDocumentElement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>          
           <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Obrero<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
           <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>nodoraiz.<span style="color: #006633;">getNodeName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
           nodos<span style="color: #009900;">&#40;</span>documento<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>ParserConfigurationException ex<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;no se puede crear el Dom parser:&quot;</span><span style="color: #339933;">+</span>ex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>SAXException ex<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Generar Sax excepción: &quot;</span><span style="color: #339933;">+</span>ex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> ex<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
		  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Io excepción: &quot;</span><span style="color: #339933;">+</span>ex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
		<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span>  <span style="color: #000066; font-weight: bold;">void</span> nodos <span style="color: #009900;">&#40;</span><span style="color: #003399;">Document</span> documento<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
&nbsp;
        NodeList listadenodos <span style="color: #339933;">=</span> documento.<span style="color: #006633;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;obrero&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> listadenodos.<span style="color: #006633;">getLength</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
        	Node nodo <span style="color: #339933;">=</span> listadenodos.<span style="color: #006633;">item</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        	Obrero obrero <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Obrero<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>nodo.<span style="color: #006633;">getNodeType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> Node.<span style="color: #006633;">ELEMENT_NODE</span><span style="color: #009900;">&#41;</span>
        	<span style="color: #009900;">&#123;</span>
        		<span style="color: #003399;">Element</span> elemento <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Element</span><span style="color: #009900;">&#41;</span> nodo<span style="color: #339933;">;</span>
&nbsp;
        		obrero.<span style="color: #006633;">setNombre</span><span style="color: #009900;">&#40;</span>elemento.<span style="color: #006633;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;nombre&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        		obrero.<span style="color: #006633;">setCargo</span><span style="color: #009900;">&#40;</span>getValor<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;cargo&quot;</span>,elemento<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        		obrero.<span style="color: #006633;">setEdad</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span>.<span style="color: #006633;">parseInt</span><span style="color: #009900;">&#40;</span>getValor<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;edad&quot;</span>,elemento<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        	<span style="color: #009900;">&#125;</span>
&nbsp;
        	listadoObreros.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>obrero<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span>  <span style="color: #003399;">String</span> getValor <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> cadenatag, <span style="color: #003399;">Element</span> elemento<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
    	NodeList listado <span style="color: #339933;">=</span> elemento.<span style="color: #006633;">getElementsByTagName</span><span style="color: #009900;">&#40;</span>cadenatag<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getChildNodes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	Node nodo <span style="color: #339933;">=</span> listado.<span style="color: #006633;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    	<span style="color: #000000; font-weight: bold;">return</span> nodo.<span style="color: #006633;">getNodeValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> ArrayList<span style="color: #339933;">&lt;</span>Obrero<span style="color: #339933;">&gt;</span> getListado<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
    	<span style="color: #000000; font-weight: bold;">return</span> listadoObreros<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> destroy <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Fin del plugin&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Clase obrero</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">vo</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Obrero <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> nombre<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> edad<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> cargo<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getNombre<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> nombre<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setNombre<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> nombre<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">nombre</span> <span style="color: #339933;">=</span> nombre<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getEdad<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> edad<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setEdad<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> edad<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">edad</span> <span style="color: #339933;">=</span> edad<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getCargo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> cargo<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setCargo<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> cargo<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">cargo</span> <span style="color: #339933;">=</span> cargo<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Configuración del action en struts-config.xml</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>action
&nbsp;
     path<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/crearplugin&quot;</span>
     type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;acciones.AccionRecogerObreros&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
     <span style="color: #339933;">&lt;</span>forward  name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;satisfactorio&quot;</span> path<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/pages/listadoobreros.jsp&quot;</span> <span style="color: #339933;">/&gt;</span>
&nbsp;
&nbsp;
&nbsp;
    <span style="color: #339933;">&lt;/</span>action<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Resultado final</p>
<p><a href="http://www.railsymas.com/wp-content/uploads/2010/05/listadoxml.png"><img src="http://www.railsymas.com/wp-content/uploads/2010/05/listadoxml.png" alt="" title="listadoxml" width="580" height="300" class="alignnone size-full wp-image-1568" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.railsymas.com/2010/05/23/creacion-de-un-plugin-con-struts-xml-dom/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Desarrollo web comodines struts</title>
		<link>http://www.railsymas.com/2010/05/14/desarrollo-web-comodines-struts/</link>
		<comments>http://www.railsymas.com/2010/05/14/desarrollo-web-comodines-struts/#comments</comments>
		<pubDate>Fri, 14 May 2010 21:14:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[struts]]></category>

		<guid isPermaLink="false">http://www.railsymas.com/?p=1516</guid>
		<description><![CDATA[Seguimos con el tutorial de struts Página jsp con varios ejemplos de utilización de comodines (wildcard) Creamos un fichero de configuración struts específico para el ejemplo y lo integramos en el fichero descriptor de despliegue en struts-configWildcard.xml 1 2 3 4 5 6 7 8 9 10 &#60;!-- Standard Action Servlet Configuration --&#62; &#60;servlet&#62; &#60;servlet-name&#62;action&#60;/servlet-name&#62; [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.railsymas.com/wp-content/uploads/2010/05/wild1.png"><img src="http://www.railsymas.com/wp-content/uploads/2010/05/wild1.png" alt="" title="wild1" width="282" height="44" class="alignnone size-full wp-image-1529" /></a></p>
<p><span id="more-1516"></span></p>
<p>Seguimos con el tutorial de struts</p>
<p>Página jsp con varios ejemplos de utilización de comodines (wildcard)</p>
<p><a href="http://www.railsymas.com/wp-content/uploads/2010/05/wil2.png"><img src="http://www.railsymas.com/wp-content/uploads/2010/05/wil2.png" alt="" title="wil2" width="580" height="193" class="alignnone size-full wp-image-1530" /></a></p>
<p>Creamos un fichero de configuración struts específico para el ejemplo y lo integramos en el fichero descriptor de despliegue en struts-configWildcard.xml</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"> <span style="color: #339933;">&lt;!--</span> Standard <span style="color: #003399;">Action</span> Servlet Configuration <span style="color: #339933;">--&gt;</span>
  <span style="color: #339933;">&lt;</span>servlet<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>servlet<span style="color: #339933;">-</span>name<span style="color: #339933;">&gt;</span>action<span style="color: #339933;">&lt;/</span>servlet<span style="color: #339933;">-</span>name<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>servlet<span style="color: #339933;">-</span>class<span style="color: #339933;">&gt;</span>org.<span style="color: #006633;">apache</span>.<span style="color: #006633;">struts</span>.<span style="color: #006633;">action</span>.<span style="color: #006633;">ActionServlet</span><span style="color: #339933;">&lt;/</span>servlet<span style="color: #339933;">-</span>class<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>init<span style="color: #339933;">-</span>param<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>param<span style="color: #339933;">-</span>name<span style="color: #339933;">&gt;</span>config<span style="color: #339933;">&lt;/</span>param<span style="color: #339933;">-</span>name<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>param<span style="color: #339933;">-</span>value<span style="color: #339933;">&gt;/</span>WEB<span style="color: #339933;">-</span>INF<span style="color: #339933;">/</span>struts<span style="color: #339933;">-</span>config.<span style="color: #006633;">xml</span> , <span style="color: #339933;">/</span>WEB<span style="color: #339933;">-</span>INF<span style="color: #339933;">/</span>struts<span style="color: #339933;">-</span>config2.<span style="color: #006633;">xml</span> , <span style="color: #339933;">/</span>WEB<span style="color: #339933;">-</span>INF<span style="color: #339933;">/</span>struts<span style="color: #339933;">-</span>configWildcard.<span style="color: #006633;">xml</span> <span style="color: #339933;">&lt;/</span>param<span style="color: #339933;">-</span>value<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;/</span>init<span style="color: #339933;">-</span>param<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>load<span style="color: #339933;">-</span>on<span style="color: #339933;">-</span>startup<span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">&lt;/</span>load<span style="color: #339933;">-</span>on<span style="color: #339933;">-</span>startup<span style="color: #339933;">&gt;</span>
 <span style="color: #339933;">&lt;/</span>servlet<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Configuración del archivo struts-configWildcard.xml</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;">struts-config&gt;
&nbsp;
  <span style="color: #808080; font-style: italic;">&lt;!-- se puede aplicar a los  --&gt;</span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action-mappings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/enlaces&quot;</span> <span style="color: #000066;">forward</span>=<span style="color: #ff0000;">&quot;/pages/enlaces.jsp&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/Primero*&quot;</span> <span style="color: #000066;">forward</span>=<span style="color: #ff0000;">&quot;/pages/Wildcard/Paginap1.jsp&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span> 
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/*segundo&quot;</span> <span style="color: #000066;">forward</span>=<span style="color: #ff0000;">&quot;/pages/Wildcard/{1}.jsp&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/action-mappings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/struts-config<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>código de enlaces.jsp</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;%</span>@ page language<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;java&quot;</span> contentType<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/html; charset=UTF-8&quot;</span>    pageEncoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;%</span>@taglib uri<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://struts.apache.org/tags-html&quot;</span> prefix<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;html&quot;</span>  <span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;%</span>@taglib uri<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://struts.apache.org/tags-bean&quot;</span> prefix<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;bean&quot;</span> <span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;!</span>DOCTYPE html <span style="color: #000000; font-weight: bold;">PUBLIC</span> <span style="color: #0000ff;">&quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;</span> <span style="color: #0000ff;">&quot;http://www.w3.org/TR/html4/loose.dtd&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>meta http<span style="color: #339933;">-</span>equiv<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Content-Type&quot;</span> content<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/html; charset=UTF-8&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;&lt;</span>bean<span style="color: #339933;">:</span>message key<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;wildcard.enlace.titulo&quot;</span><span style="color: #339933;">/&gt;</span> <span style="color: #339933;">&lt;/</span>title<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>base<span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>link rel<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;stylesheet&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;../css/estilo.css&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/css&quot;</span><span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
 <span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;contenedor&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
 	  <span style="color: #339933;">&lt;</span>h2<span style="color: #339933;">&gt;</span> Enlaces <span style="color: #339933;">&lt;/</span>h2<span style="color: #339933;">&gt;</span>	
      <span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;contenido&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
      <span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;campo&quot;</span><span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>link action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;PrimeroPaginap1&quot;</span><span style="color: #339933;">&gt;</span> primero p<span style="color: #339933;">&amp;</span>aacute<span style="color: #339933;">;</span>gina p1<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">:</span>link<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
&nbsp;
      <span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;campo&quot;</span><span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>link action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Paginap1segundo&quot;</span><span style="color: #339933;">&gt;</span> p<span style="color: #339933;">&amp;</span>aacute<span style="color: #339933;">;</span>gina p1 segundo <span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">:</span>link<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
&nbsp;
      <span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;campo&quot;</span><span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>link action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Paginap2segundo&quot;</span><span style="color: #339933;">&gt;</span> primera p<span style="color: #339933;">&amp;</span>aacute<span style="color: #339933;">;</span>gina p2 <span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">:</span>link<span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
&nbsp;
    <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;pie&quot;</span><span style="color: #339933;">&gt;</span>
     <span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span> www.<span style="color: #006633;">railsymas</span>.<span style="color: #006633;">com</span> <span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
 <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Las páginas jsp son simples, no tienen ningun código especial</p>
<p>página 1 jsp</p>
<p><a href="http://www.railsymas.com/wp-content/uploads/2010/05/p1.png"><img src="http://www.railsymas.com/wp-content/uploads/2010/05/p1.png" alt="" title="p1" width="580" height="120" class="alignnone size-full wp-image-1542" /></a></p>
<p>página 2 jsp</p>
<p><a href="http://www.railsymas.com/wp-content/uploads/2010/05/p21.png"><img src="http://www.railsymas.com/wp-content/uploads/2010/05/p21.png" alt="" title="p2" width="580" height="120" class="alignnone size-full wp-image-1544" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.railsymas.com/2010/05/14/desarrollo-web-comodines-struts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Desarrollo web Múltiples archivos de configuración struts</title>
		<link>http://www.railsymas.com/2010/05/05/desarrollo-web-multiples-archivos-de-configuracion-struts/</link>
		<comments>http://www.railsymas.com/2010/05/05/desarrollo-web-multiples-archivos-de-configuracion-struts/#comments</comments>
		<pubDate>Wed, 05 May 2010 18:31:20 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[struts]]></category>

		<guid isPermaLink="false">http://www.railsymas.com/?p=1490</guid>
		<description><![CDATA[En este post veremos como crear diferentes archivos de configuración para el framework struts. Estamos con la versión 1.3.10 de struts y se llevan mostrados diferentes ejemplos sobre este framework. siempre a la hora de activar un componente de struts teníamos que ir al archivo de configuración, llega el momento en que este archivo de [...]]]></description>
			<content:encoded><![CDATA[<p>En este post veremos como crear diferentes archivos de configuración para el framework struts.</p>
<p><span id="more-1490"></span></p>
<p>Estamos con la versión 1.3.10 de struts y se llevan mostrados diferentes ejemplos sobre este framework. siempre a la hora de activar un componente de struts teníamos que ir al archivo de configuración, llega el momento en que este archivo de configuración esta sobrecargado de líneas de código.</p>
<p><a href="http://www.railsymas.com/wp-content/uploads/2010/05/ficherosconfiguración.png"><img src="http://www.railsymas.com/wp-content/uploads/2010/05/ficherosconfiguración.png" alt="" title="ficherosconfiguración" width="317" height="118" class="alignnone size-full wp-image-1494" /></a> </p>
<p>En este ejemplo intervienen los dos archivos de configuración, declaración de una acción en el archivo de configuración de struts-config.xml  denominada  &#8220;multiconfiguración&#8221; y se produce el reenvio a la página enlaceprivado.jsp</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span> </span>
<span style="color: #009900;">         <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/multiconfiguracion&quot;</span>  </span>
<span style="color: #009900;">         <span style="color: #000066;">forward</span>=<span style="color: #ff0000;">&quot;/pages/enlaceprivado.jsp&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></td></tr></table></div>

<p>Código de la página enlaceprivado</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;%</span>@ page language<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;java&quot;</span> contentType<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/html; charset=UTF-8&quot;</span>  pageEncoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;%</span>@taglib uri<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://struts.apache.org/tags-html&quot;</span> prefix<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;html&quot;</span> <span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;%</span>@taglib uri<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://struts.apache.org/tags-bean&quot;</span> prefix<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;bean&quot;</span> <span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;!</span>DOCTYPE html <span style="color: #000000; font-weight: bold;">PUBLIC</span> <span style="color: #0000ff;">&quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;</span> <span style="color: #0000ff;">&quot;http://www.w3.org/TR/html4/loose.dtd&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>html<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>base<span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>meta http<span style="color: #339933;">-</span>equiv<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Content-Type&quot;</span> content<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/html; charset=UTF-8&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;&lt;</span>bean<span style="color: #339933;">:</span>message key<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;enlaceprivado.titulo&quot;</span><span style="color: #339933;">/&gt;</span> <span style="color: #339933;">&lt;/</span>title<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>link rel<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;stylesheet&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/css&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;../css/estilo.css&quot;</span><span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;contenedor&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;contenido&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
  <span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;campo&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>link action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/config2&quot;</span><span style="color: #339933;">&gt;</span> Zona privada <span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">:</span>link<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;pie&quot;</span><span style="color: #339933;">&gt;</span>
 <span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span>www.<span style="color: #006633;">railsymas</span>.<span style="color: #006633;">com</span> <span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">:</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>En esa página jsp existe un enlace que activa la acción config2 que aparece en el fichero de configuración struts-config2.xml</p>
<p>Enlace imagen<br />
<a href="http://www.railsymas.com/wp-content/uploads/2010/05/enlace1.png"><img src="http://www.railsymas.com/wp-content/uploads/2010/05/enlace1.png" alt="" title="enlace" width="580" height="91" class="alignnone size-full wp-image-1500" /></a></p>
<p>Se declara en struts-config2.xml  un bean dinámico llamado formularioprivado  con dos campos de tipo string, y una acción privado, que va ser tratada desde struts-config2.xml</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form-beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form-bean</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;formularioprivado&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;org.apache.struts.validator.DynaValidatorForm&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>  
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form-property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;usuario&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;java.lang.String&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form-property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;clave&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;java.lang.String&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/form-bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/form-beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
&nbsp;
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action-mappings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span>      </span>
<span style="color: #009900;">     path =<span style="color: #ff0000;">&quot;/config2&quot;</span></span>
<span style="color: #009900;">     forward =<span style="color: #ff0000;">&quot;/pages/privado.jsp&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span> </span>
<span style="color: #009900;">      <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/privado&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;acciones.Privado&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;formularioprivado&quot;</span>      </span>
<span style="color: #009900;">      <span style="color: #000066;">input</span>=<span style="color: #ff0000;">&quot;/pages/privado.jsp&quot;</span>     </span>
<span style="color: #009900;">      <span style="color: #000066;">validate</span>=<span style="color: #ff0000;">&quot;true&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000000; font-weight: bold;">&gt;</span></span>            
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;forward</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;satisfactorio&quot;</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/pages/satisfactorio.jsp&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;forward</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;error&quot;</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/pages/cancelada2.jsp&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>      
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/action<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>visualización de la página privado donde se muestra un formulario de usuario, </p>
<p><a href="http://www.railsymas.com/wp-content/uploads/2010/05/formularioconf21.png"><img src="http://www.railsymas.com/wp-content/uploads/2010/05/formularioconf21.png" alt="" title="formularioconf2" width="580" height="300" class="alignnone size-full wp-image-1510" /></a></p>
<p>En el fichero web.xml del proyecto</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"> <span style="color: #808080; font-style: italic;">&lt;!-- Standard Action Servlet Configuration --&gt;</span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>action<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.apache.struts.action.ActionServlet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;init-param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>config<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/param-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param-value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/WEB-INF/struts-config.xml , /WEB-INF/struts-config2.xml <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/param-value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/init-param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;load-on-startup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/load-on-startup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.railsymas.com/2010/05/05/desarrollo-web-multiples-archivos-de-configuracion-struts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Desarrollo web uploadfile struts</title>
		<link>http://www.railsymas.com/2010/04/27/desarrollo-web-uploadfile-struts/</link>
		<comments>http://www.railsymas.com/2010/04/27/desarrollo-web-uploadfile-struts/#comments</comments>
		<pubDate>Tue, 27 Apr 2010 18:06:08 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[struts]]></category>

		<guid isPermaLink="false">http://www.railsymas.com/?p=1424</guid>
		<description><![CDATA[Ejemplo para subir un archivo zip mediante struts form bean, se declara el nombre del formulario bean como uploadForm en atributo type se declara la ruta de la clase bean 1 2 3 &#60;!-- el formbean solamente necesita el name y el type --&#62; &#60;form-bean name=&#34;uploadForm&#34; type=&#34;formularios.Formularioupload&#34;/&#62; En el archivo de configuración del struts-config.xml en [...]]]></description>
			<content:encoded><![CDATA[<p>Ejemplo para subir un archivo zip mediante struts</p>
<p><span id="more-1424"></span></p>
<p><br/></p>
<p><a href="http://www.railsymas.com/wp-content/uploads/2010/04/upload1.png"><img src="http://www.railsymas.com/wp-content/uploads/2010/04/upload1.png" alt="" title="upload" width="580" height="235" class="alignnone size-full wp-image-1442" /></a></p>
<p>form bean, se declara el nombre del formulario bean como uploadForm en atributo type se declara la ruta de la clase bean</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"> <span style="color: #808080; font-style: italic;">&lt;!-- el formbean solamente necesita el name y el type  --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form-bean</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;uploadForm&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;formularios.Formularioupload&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></td></tr></table></div>

<p>En el archivo de configuración del struts-config.xml en la seccion de actions mediante el atributo path indicamos que struts responda ante una acción uploadfile con la página ubicada en /pages/formularioloadfile.jsp en cargada de mostrar un formulario para subir el archivo</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;">&nbsp;
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/uploadfile&quot;</span></span>
<span style="color: #009900;">         <span style="color: #000066;">forward</span>=<span style="color: #ff0000;">&quot;/pages/formulariouploadfile.jsp&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/action<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span>                 </span>
<span style="color: #009900;">         <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/subirarchivo&quot;</span></span>
<span style="color: #009900;">         <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;acciones.SubirArchivo&quot;</span></span>
<span style="color: #009900;">         <span style="color: #000066;">scope</span>=<span style="color: #ff0000;">&quot;request&quot;</span></span>
<span style="color: #009900;">         <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;uploadForm&quot;</span></span>
<span style="color: #009900;">         <span style="color: #000066;">validate</span>=<span style="color: #ff0000;">&quot;true&quot;</span> </span>
<span style="color: #009900;">         <span style="color: #000066;">input</span>=<span style="color: #ff0000;">&quot;/pages/formulariouploadfile.jsp&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;forward</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;subido&quot;</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/pages/mostrarInformacion.jsp&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/action<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Si se subió el archivo web correctamente se realiza un reenvio a la página mostrarinformación para indicar un mensaje de confirmación al usuario, en caso contrario mediante el atributo input se indica que la propia página jsp de subida del archivo muestre por pantalla el mensaje correspondiente según el error cometido</p>
<p>Código de la clase subir archivo</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">acciones</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.Action</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletRequest</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletResponse</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionForm</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionForward</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionMapping</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.upload.FormFile</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">formularios.Formularioupload</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.FileOutputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.File</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SubirArchivo <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">Action</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> ActionForward envio<span style="color: #339933;">;</span>
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> ActionForward execute <span style="color: #009900;">&#40;</span>ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span>
	<span style="color: #009900;">&#123;</span>	
		Formularioupload formularioSubido <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Formularioupload<span style="color: #009900;">&#41;</span> form<span style="color: #339933;">;</span>
		<span style="color: #003399;">FileOutputStream</span> fileoutputstream <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">try</span>
		<span style="color: #009900;">&#123;</span>
			FormFile fichero <span style="color: #339933;">=</span> formularioSubido.<span style="color: #006633;">getFichero</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #003399;">String</span> ruta <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getServlet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getServletContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getRealPath</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/upload/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>fichero.<span style="color: #006633;">getFileName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			fileoutputstream <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileOutputStream</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">File</span><span style="color: #009900;">&#40;</span>ruta<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			fileoutputstream.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>fichero.<span style="color: #006633;">getFileData</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">finally</span>
		<span style="color: #009900;">&#123;</span>
		  <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>fileoutputstream <span style="color: #339933;">!=</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
		  <span style="color: #009900;">&#123;</span>
			  fileoutputstream.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		  <span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">return</span> mapping.<span style="color: #006633;">findForward</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;subido&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Código de la página jsp encargada de mostrar el formulario de subida de archivo</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;%</span>@ page language<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;java&quot;</span> contentType<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/html; charset=UTF-8&quot;</span>  pageEncoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;%</span>@taglib uri<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://struts.apache.org/tags-html&quot;</span> prefix<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;html&quot;</span><span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;%</span>@taglib uri<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://struts.apache.org/tags-bean&quot;</span> prefix<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;bean&quot;</span> <span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;!</span>DOCTYPE html <span style="color: #000000; font-weight: bold;">PUBLIC</span> <span style="color: #0000ff;">&quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;</span> <span style="color: #0000ff;">&quot;http://www.w3.org/TR/html4/loose.dtd&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>html<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>base<span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>meta http<span style="color: #339933;">-</span>equiv<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Content-Type&quot;</span> content<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/html; charset=UTF-8&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;&lt;</span>bean<span style="color: #339933;">:</span>message key<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;uploadfile.titulo&quot;</span><span style="color: #339933;">/&gt;&lt;/</span>title<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>link rel<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;stylesheet&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/css&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;../css/estilo.css&quot;</span> <span style="color: #339933;">/&gt;</span> 
<span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;contenedor&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
 <span style="color: #339933;">&lt;</span>h2<span style="color: #339933;">&gt;&lt;</span>bean<span style="color: #339933;">:</span>message key<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;uploadfile.titulo&quot;</span><span style="color: #339933;">/&gt;</span> <span style="color: #339933;">&lt;/</span>h2<span style="color: #339933;">&gt;</span>
&nbsp;
 <span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;errores&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>errors<span style="color: #339933;">/&gt;</span>
 <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
&nbsp;
 <span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;contenido&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
 <span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;imagen&quot;</span><span style="color: #339933;">&gt;</span>
 <span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>img srcKey<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;uploadfile.imagen&quot;</span> altKey<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;uploadfile.imagen.alt&quot;</span><span style="color: #339933;">/&gt;</span>
 <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
&nbsp;
 <span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>form method<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;post&quot;</span> action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/subirarchivo&quot;</span> enctype<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;multipart/form-data&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
 <span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;campo&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
 <span style="color: #339933;">&lt;</span>bean<span style="color: #339933;">:</span>message key<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;uploadfile.fichero&quot;</span><span style="color: #339933;">/&gt;</span>  
&nbsp;
&nbsp;
 <span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>file  property<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;fichero&quot;</span><span style="color: #339933;">/&gt;</span>
&nbsp;
 <span style="color: #339933;">&lt;</span>br<span style="color: #339933;">/&gt;</span>
&nbsp;
 <span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>submit styleClass<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;boton&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;enviar&quot;</span><span style="color: #339933;">/&gt;</span>
&nbsp;
 <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
&nbsp;
 <span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">:</span>form<span style="color: #339933;">&gt;</span>
 <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
 <span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;pie&quot;</span><span style="color: #339933;">&gt;</span>
 <span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span> www.<span style="color: #006633;">railsymas</span>.<span style="color: #006633;">com</span> <span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span> 
 <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
&nbsp;
 <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
 <span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">:</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>En el fichero de propiedades</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;">#uploadfile
uploadfile.titulo = formulario uploadfile
uploadfile.fichero = introduce el fichero a subir
uploadfile.imagen = ../imagenes/subirarchivo.png
uploadfile.imagen.alt = imagen archivo 
error.fichero.vacio = el fichero esta vac<span style="color: #ddbb00;">&amp;iacute;</span>o 
error.fichero.tipo =  el archivo no es de tipo zip
error.fichero.tamanio = error el tamaño excede lo permitido
uploadfile.resultado = formulario subido</pre></td></tr></table></div>

<p>clase bean para el formulario</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">formularios</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionForm</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.upload.FormFile</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionErrors</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletRequest</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionMapping</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionMessage</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Formularioupload <span style="color: #000000; font-weight: bold;">extends</span> ActionForm <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> 1L<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> FormFile fichero<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> informacion<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getInformacion<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> informacion<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setInformacion<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> informacion<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">informacion</span> <span style="color: #339933;">=</span> informacion<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> FormFile getFichero<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> fichero<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setFichero<span style="color: #009900;">&#40;</span>FormFile fichero<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">fichero</span> <span style="color: #339933;">=</span> fichero<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> ActionErrors  validate <span style="color: #009900;">&#40;</span>ActionMapping mapping, HttpServletRequest request<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
&nbsp;
		ActionErrors errores <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ActionErrors <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//ya esta subido miramos si es del tipo indicado</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>fichero.<span style="color: #006633;">getFileSize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">//esta vacío</span>
			errores.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;fichero&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> ActionMessage <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;error.fichero.vacio&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>		
		<span style="color: #000000; font-weight: bold;">else</span>
		<span style="color: #009900;">&#123;</span>
&nbsp;
		  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span>fichero.<span style="color: #006633;">getContentType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;application/x-zip-compressed&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		  <span style="color: #009900;">&#123;</span>
			  errores.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;fichero&quot;</span>,<span style="color: #000000; font-weight: bold;">new</span> ActionMessage<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;error.fichero.tipo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span>fichero.<span style="color: #006633;">getContentType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		  <span style="color: #009900;">&#125;</span>
&nbsp;
		  <span style="color: #000000; font-weight: bold;">else</span>
		  <span style="color: #009900;">&#123;</span>  
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> fichero.<span style="color: #006633;">getFileSize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">2000000</span> <span style="color: #009900;">&#41;</span> 
			<span style="color: #009900;">&#123;</span>
				errores.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;fichero&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> ActionMessage<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;error.fichero.tamanio&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
		  <span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> errores<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Situación en la que hemos dado al botón de enviar sin haber seleccionado un archivo</p>
<p><a href="http://www.railsymas.com/wp-content/uploads/2010/04/validacionfile1.png"><img src="http://www.railsymas.com/wp-content/uploads/2010/04/validacionfile1.png" alt="" title="validacionfile1" width="580" height="245" class="alignnone size-full wp-image-1458" /></a></p>
<p>Error de formato de archivo</p>
<p><a href="http://www.railsymas.com/wp-content/uploads/2010/04/formatoerroneo.png"><img src="http://www.railsymas.com/wp-content/uploads/2010/04/formatoerroneo.png" alt="" title="formatoerroneo" width="590" height="245" class="alignnone size-full wp-image-1463" /></a></p>
<p>Situación el archivo, sí tiene el formato zip pero nos hemos excedido de tamaño soportado por la aplicación</p>
<p><a href="http://www.railsymas.com/wp-content/uploads/2010/04/tamanio1.png"><img src="http://www.railsymas.com/wp-content/uploads/2010/04/tamanio1.png" alt="" title="tamanio" width="590" height="240" class="alignnone size-full wp-image-1471" /></a></p>
<p>Situación en la que hemos enviado correctamente el archivo</p>
<p><a href="http://www.railsymas.com/wp-content/uploads/2010/04/subido.png"><img src="http://www.railsymas.com/wp-content/uploads/2010/04/subido.png" alt="" title="subido" width="590" height="120" class="alignnone size-full wp-image-1462" /></a></p>
<p>Para comprobar el fichero subido al proyecto desplegamos la aplicación en el servidor tomcat mediante un war del proyecto</p>
]]></content:encoded>
			<wfw:commentRss>http://www.railsymas.com/2010/04/27/desarrollo-web-uploadfile-struts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mostrando listado de datos</title>
		<link>http://www.railsymas.com/2010/02/09/mostrando-listado-de-datos/</link>
		<comments>http://www.railsymas.com/2010/02/09/mostrando-listado-de-datos/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 23:36:59 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[struts]]></category>

		<guid isPermaLink="false">http://www.railsymas.com/?p=1272</guid>
		<description><![CDATA[Cuando mostramos los datos, recogidos de una bases de datos, puede que el número de items recogidos sea bastante amplio por lo que resultaría bastante engorroso para el usuario ver un listado muy largo. Se hace necesario dar una respuesta a este problema. para ello utlizamos la paginación Podemos crear de dos formas distintas, bien [...]]]></description>
			<content:encoded><![CDATA[<p>Cuando mostramos los datos, recogidos de una bases de datos, puede que el número de items recogidos sea bastante amplio por lo que resultaría bastante engorroso para el usuario ver un listado muy largo.</p>
<p>Se hace necesario dar una respuesta a este problema. para ello utlizamos la paginación</p>
<p><span id="more-1272"></span>   </p>
<p>Podemos crear de dos formas distintas, bien a mano o bien utilizando una librería de struts adecuada. Displaytag es un jar que permite la paginación y la exprotación de datos a diferentes formatos.</p>
<p>Para su utilización, necesitamos importar la librería display tag  ( http://displaytag.sf.net)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;%</span>@ page language<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;java&quot;</span> contentType<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/html; charset=UTF-8&quot;</span>   pageEncoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;%</span>@taglib uri<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://struts.apache.org/tags-html&quot;</span> prefix<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;html&quot;</span> <span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;%</span>@taglib uri<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://struts.apache.org/tags-bean&quot;</span> prefix<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;bean&quot;</span> <span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;%</span>@taglib uri<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://displaytag.sf.net&quot;</span> prefix<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;display&quot;</span> <span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;!</span>DOCTYPE html <span style="color: #000000; font-weight: bold;">PUBLIC</span> <span style="color: #0000ff;">&quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;</span> <span style="color: #0000ff;">&quot;http://www.w3.org/TR/html4/loose.dtd&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>html<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>base<span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>meta http<span style="color: #339933;">-</span>equiv<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Content-Type&quot;</span> content<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/html; charset=UTF-8&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;&lt;</span>bean<span style="color: #339933;">:</span>message key<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;displaytag.titulo&quot;</span><span style="color: #339933;">/&gt;&lt;/</span>title<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>link rel<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;stylesheet&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/css&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;../css/estilo.css&quot;</span><span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
&nbsp;
 <span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;contenedor&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
 <span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;tabla&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
 <span style="color: #339933;">&lt;</span>display<span style="color: #339933;">:</span>table id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;libro&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;sessionScope.listadolibros&quot;</span> pagesize<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;5&quot;</span> requestURI<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/mostrarlistadodisplaytag.do&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
  <span style="color: #339933;">&lt;</span>display<span style="color: #339933;">:</span>column property<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;titulo&quot;</span> title<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;t&amp;iacute;tulo&quot;</span> sortable<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;true&quot;</span><span style="color: #339933;">/&gt;</span>
  <span style="color: #339933;">&lt;</span>display<span style="color: #339933;">:</span>column property<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;autor&quot;</span> title<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;autor&quot;</span> sortable<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;true&quot;</span><span style="color: #339933;">/&gt;</span>
  <span style="color: #339933;">&lt;</span>display<span style="color: #339933;">:</span>column property<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;tematica&quot;</span> title<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;tem&amp;aacute;tica&quot;</span> sortable<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;true&quot;</span><span style="color: #339933;">/&gt;</span>
&nbsp;
 <span style="color: #339933;">&lt;/</span>display<span style="color: #339933;">:</span>table<span style="color: #339933;">&gt;</span>
&nbsp;
 <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
&nbsp;
 <span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;pie&quot;</span><span style="color: #339933;">&gt;</span>
   <span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span> www.<span style="color: #006633;">railsymas</span>.<span style="color: #006633;">com</span> <span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span>
 <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
&nbsp;
 <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">:</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>necesitamos incorporar el archivo de propiedades, para displaytag, </p>
<p><a href="http://www.railsymas.com/wp-content/uploads/2010/02/tabla1.png"><img src="http://www.railsymas.com/wp-content/uploads/2010/02/tabla1.png" alt="" title="tabla1" width="580" height="220" class="alignnone size-full wp-image-1275" /></a></p>
<p>Los datos que se muestran pueden cambiar, dejar menos es posible</p>
<p>al pasar la página<br />
<a href="http://www.railsymas.com/wp-content/uploads/2010/02/tabla2.png"><img src="http://www.railsymas.com/wp-content/uploads/2010/02/tabla2.png" alt="" title="tabla2" width="588" height="210" class="alignnone size-full wp-image-1276" /></a></p>
<p>en la carpeta src se copia el fichero de propiedades en castellano<a href="http://www.railsymas.com/wp-content/uploads/2010/02/img.png"><img src="http://www.railsymas.com/wp-content/uploads/2010/02/img.png" alt="" title="img" width="240" height="31" class="alignnone size-full wp-image-1278" /></a> </p>
<p>_es_zona geográfica    ejemplo es_AR, es_MX etc &#8230;. </p>
<p>  el action al que llama la tabla en su atributo uri, configurado en el struts-config.xml</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span> </span>
<span style="color: #009900;">         <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/mostrarlistadodisplaytag&quot;</span>         </span>
<span style="color: #009900;">         <span style="color: #000066;">forward</span>=<span style="color: #ff0000;">&quot;/pages/mostrarlistadodisplaytag.jsp&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.railsymas.com/2010/02/09/mostrando-listado-de-datos/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Datasource struts 1.3.x proyecto web eclipse</title>
		<link>http://www.railsymas.com/2010/01/11/datasource-struts-1-3-x-proyecto-web-eclipse/</link>
		<comments>http://www.railsymas.com/2010/01/11/datasource-struts-1-3-x-proyecto-web-eclipse/#comments</comments>
		<pubDate>Mon, 11 Jan 2010 11:30:55 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[struts]]></category>

		<guid isPermaLink="false">http://www.railsymas.com/?p=1064</guid>
		<description><![CDATA[integración del enlace en la página web de ejercicios 1 2 3 &#60;div class=&#34;enlace&#34;&#62; &#60;html:link action=&#34;/datasource&#34;&#62; 7&#41; datasource &#60;/html:link&#62; &#60;/div&#62; configuración de la acción datasource.do en el struts-config.xml 1 2 3 4 &#60;action path=&#34;/datasource&#34; forward=&#34;/pages/formulariodatasource.jsp&#34; /&#62; Configuración del bean en el fichero struts-config.xml, es la configuración del bean dinámico con las propiedades de la tabla [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.railsymas.com/wp-content/uploads/2009/12/ejerciciodatasource.png" alt="ejerciciodatasource" title="ejerciciodatasource" width="570" height="100" class="alignnone size-full wp-image-1081" /></p>
<p><span id="more-1064"></span></p>
<p>integración del enlace en la página web de ejercicios</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"> <span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;enlace&quot;</span><span style="color: #339933;">&gt;</span>
   <span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>link action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/datasource&quot;</span><span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">7</span><span style="color: #009900;">&#41;</span> datasource <span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">:</span>link<span style="color: #339933;">&gt;</span>
   <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p><!--more--></p>
<p>configuración de la acción datasource.do en el struts-config.xml</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"> <span style="color: #339933;">&lt;</span>action         
         path<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/datasource&quot;</span>
         forward<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/pages/formulariodatasource.jsp&quot;</span> 
         <span style="color: #339933;">/&gt;</span></pre></td></tr></table></div>

<p>Configuración del bean en el fichero struts-config.xml, es la configuración del bean dinámico con las propiedades de la tabla de la base de datos libros campo titulo, temática y autor vistos en jdbc j2ee</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>form<span style="color: #339933;">-</span>bean name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;beandatasource&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;org.apache.struts.validator.DynaValidatorForm&quot;</span><span style="color: #339933;">&gt;</span>    
    <span style="color: #339933;">&lt;</span>form<span style="color: #339933;">-</span>property name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;titulo&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;java.lang.String&quot;</span><span style="color: #339933;">/&gt;</span>
    <span style="color: #339933;">&lt;</span>form<span style="color: #339933;">-</span>property name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;tematica&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;java.lang.String&quot;</span><span style="color: #339933;">/&gt;</span>
    <span style="color: #339933;">&lt;</span>form<span style="color: #339933;">-</span>property name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;autor&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;java.lang.String&quot;</span><span style="color: #339933;">/&gt;</span>      
    <span style="color: #339933;">&lt;/</span>form<span style="color: #339933;">-</span>bean<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Configuración del fichero de propiedades</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;">#datasource
formulariodatasource.crear = enviar
formulariodatasource.cancelar = cancelar
formulariodatasource.titulo = formulario datasource
formulariodatasource.titulolibro = titulo
formulariodatasource.tematica = tematica
formulariodatasource.autor = autor
formulariodatasource.resultado = listado de libros
datasourceresultado.titulo = listado de datos</pre></td></tr></table></div>

<p>Si intentamos crear el datasource al igual que hacíamos con la versión 1.2 nos da error, en el archivo de configuración de struts</p>
<p><img src="http://www.railsymas.com/wp-content/uploads/2009/12/error2.png" alt="error2" title="error2" width="198" height="101" class="alignnone size-full wp-image-1067" /></p>
<p>La dtd de struts 1.3 soporta un conjunto de etiquetas en el orden que se indica en la siguiente imagen.</p>
<p><img src="http://www.railsymas.com/wp-content/uploads/2009/12/error1.png" alt="error1" title="error1" width="498" height="94" class="alignnone size-full wp-image-1070" /></p>
<p>El tag <data-sources> no viene soportado para esta dtd</p>
<p>Explicaciones del proyecto struts en el wiki <a href="http://wiki.apache.org/struts/StrutsUpgradeNotes12to13"> datasource</a></p>
<p>Removed the <data-sources> and <data-source> elements</p>
<p>creamos un package nuevo para albergar al bean libro, package vo, se llama así por el patrón value object</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">vo</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Libro <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> titulo<span style="color: #339933;">;</span> 
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> autor<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> tematica<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getTitulo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> titulo<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setTitulo<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> titulo<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">titulo</span> <span style="color: #339933;">=</span> titulo<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getAutor<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> autor<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setAutor<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> autor<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">autor</span> <span style="color: #339933;">=</span> autor<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getTematica<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> tematica<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setTematica<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> tematica<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">tematica</span> <span style="color: #339933;">=</span> tematica<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><a href="http://www.railsymas.com/wp-content/uploads/2010/01/libro.png"><img src="http://www.railsymas.com/wp-content/uploads/2010/01/libro.png" alt="" title="libro" width="132" height="45" class="alignnone size-full wp-image-1117" /></a></p>
<p>Clase Conexion dentro del package conexiones</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">conexiones</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.Connection</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.SQLException</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.sql.DataSource</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.naming.InitialContext</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.naming.NamingException</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Conexion <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Connection</span> conexion <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Conexion <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>  
&nbsp;
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Connection</span> establecerConexion<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">try</span>
		<span style="color: #009900;">&#123;</span>
&nbsp;
			<span style="color: #003399;">InitialContext</span> contexto <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">InitialContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			DataSource ds <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>DataSource<span style="color: #009900;">&#41;</span> contexto.<span style="color: #006633;">lookup</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;java:comp/env/jdbc/datasource&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">conexion</span> <span style="color: #339933;">=</span> ds.<span style="color: #006633;">getConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
&nbsp;
&nbsp;
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">NamingException</span> e<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>e.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">SQLException</span> e <span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>e.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">conexion</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><a href="http://www.railsymas.com/wp-content/uploads/2010/01/claseconexion.png"><img src="http://www.railsymas.com/wp-content/uploads/2010/01/claseconexion.png" alt="" title="claseconexion" width="174" height="48" class="alignnone size-full wp-image-1119" /></a></p>
<p>Configuración del fichero web.xml</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;resource-ref<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>DB Connection<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;res-ref-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>jdbc/datasource<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/res-ref-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;res-type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>javax.sql.DataSource<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/res-type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;res-auth<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Container<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/res-auth<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;res-sharing-scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>shareable<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/res-sharing-scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/resource-ref<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>fichero context.xml</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Context</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/DBTest&quot;</span> <span style="color: #000066;">docBase</span>=<span style="color: #ff0000;">&quot;DBTest&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">debug</span>=<span style="color: #ff0000;">&quot;5&quot;</span> <span style="color: #000066;">reloadable</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">crossContext</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- maxActive: Maximum number of dB connections in pool. Make sure you</span>
<span style="color: #808080; font-style: italic;">         configure your mysqld max_connections large enough to handle</span>
<span style="color: #808080; font-style: italic;">         all of your db connections. Set to -1 for no limit.</span>
<span style="color: #808080; font-style: italic;">         --&gt;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- maxIdle: Maximum number of idle dB connections to retain in pool.</span>
<span style="color: #808080; font-style: italic;">         Set to -1 for no limit.  See also the DBCP documentation on this</span>
<span style="color: #808080; font-style: italic;">         and the minEvictableIdleTimeMillis configuration parameter.</span>
<span style="color: #808080; font-style: italic;">         --&gt;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- maxWait: Maximum time to wait for a dB connection to become available</span>
<span style="color: #808080; font-style: italic;">         in ms, in this example 10 seconds. An Exception is thrown if</span>
<span style="color: #808080; font-style: italic;">         this timeout is exceeded.  Set to -1 to wait indefinitely.</span>
<span style="color: #808080; font-style: italic;">         --&gt;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- username and password: MySQL dB username and password for dB connections  --&gt;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- driverClassName: Class name for the old mm.mysql JDBC driver is</span>
<span style="color: #808080; font-style: italic;">         org.gjt.mm.mysql.Driver - we recommend using Connector/J though.</span>
<span style="color: #808080; font-style: italic;">         Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver.</span>
<span style="color: #808080; font-style: italic;">         --&gt;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- url: The JDBC connection url for connecting to your MySQL dB.</span>
<span style="color: #808080; font-style: italic;">         --&gt;</span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Resource</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;jdbc/datasource&quot;</span> <span style="color: #000066;">auth</span>=<span style="color: #ff0000;">&quot;Container&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;javax.sql.DataSource&quot;</span></span>
<span style="color: #009900;">               <span style="color: #000066;">maxActive</span>=<span style="color: #ff0000;">&quot;100&quot;</span> <span style="color: #000066;">maxIdle</span>=<span style="color: #ff0000;">&quot;30&quot;</span> <span style="color: #000066;">maxWait</span>=<span style="color: #ff0000;">&quot;10000&quot;</span></span>
<span style="color: #009900;">               <span style="color: #000066;">username</span>=<span style="color: #ff0000;">&quot;root&quot;</span> <span style="color: #000066;">password</span>=<span style="color: #ff0000;">&quot;root&quot;</span> <span style="color: #000066;">driverClassName</span>=<span style="color: #ff0000;">&quot;com.mysql.jdbc.Driver&quot;</span></span>
<span style="color: #009900;">               <span style="color: #000066;">url</span>=<span style="color: #ff0000;">&quot;jdbc:mysql://localhost:3306/libros_jdbc&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Context<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p><a href="http://www.railsymas.com/wp-content/uploads/2010/01/configcontex.png"><img src="http://www.railsymas.com/wp-content/uploads/2010/01/configcontex.png" alt="" title="configcontex" width="166" height="74" class="alignnone size-full wp-image-1128" /></a></p>
<p>Configuración de la acción formulariodatasource.do</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span> </span>
<span style="color: #009900;">         <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/formulariodatasource&quot;</span></span>
<span style="color: #009900;">         <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;acciones.ConexionDatasource&quot;</span></span>
<span style="color: #009900;">         <span style="color: #000066;">scope</span>=<span style="color: #ff0000;">&quot;request&quot;</span></span>
<span style="color: #009900;">         <span style="color: #000066;">validate</span>=<span style="color: #ff0000;">&quot;true&quot;</span></span>
<span style="color: #009900;">         <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;beandatasource&quot;</span></span>
<span style="color: #009900;">         <span style="color: #000066;">input</span>=<span style="color: #ff0000;">&quot;/pages/formulariodatasource.jsp&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;set-property</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;cancellable&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span> 
&nbsp;
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;forward</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;cancelada&quot;</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/pages/cancelada2.jsp&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/forward<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;forward</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;mostrardatos&quot;</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/pages/mostrarlistado.jsp&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/forward<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/action<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">acciones</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">conexiones.Conexion</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">vo.Libro</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.PrintWriter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.SQLException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.Action</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionForward</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionMapping</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletRequest</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletResponse</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionForm</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.validator.DynaValidatorForm</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.Statement</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.Connection</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.ResultSet</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpSession</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ConexionDatasource <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">Action</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Connection</span> conexionr <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> ActionForward envio  <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> Conexion conexion <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Statement</span> sentenciaSql<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">ResultSet</span> resultado <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> Libro libro<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> ArrayList<span style="color: #339933;">&lt;</span>Libro<span style="color: #339933;">&gt;</span> listalibros<span style="color: #339933;">;</span> 
	<span style="color: #000000; font-weight: bold;">private</span> HttpSession session <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> ActionForward execute <span style="color: #009900;">&#40;</span>ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> 
	<span style="color: #009900;">&#123;</span>
&nbsp;
		DynaValidatorForm formulario <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>DynaValidatorForm<span style="color: #009900;">&#41;</span>form<span style="color: #339933;">;</span>
		conexion <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Conexion<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		response.<span style="color: #006633;">setContentType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Text/html&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #003399;">PrintWriter</span> salida <span style="color: #339933;">=</span> response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">isCancelled</span><span style="color: #009900;">&#40;</span>request<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			envio <span style="color: #339933;">=</span> mapping.<span style="color: #006633;">findForward</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;cancelada&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">else</span>
		<span style="color: #009900;">&#123;</span>
&nbsp;
&nbsp;
		<span style="color: #000000; font-weight: bold;">try</span>
		<span style="color: #009900;">&#123;</span>
&nbsp;
		   conexionr <span style="color: #339933;">=</span> conexion.<span style="color: #006633;">establecerConexion</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		   sentenciaSql <span style="color: #339933;">=</span> conexionr.<span style="color: #006633;">createStatement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	   
&nbsp;
		   <span style="color: #666666; font-style: italic;">//ejecutamos la sentencia de selección de los campos menos el id</span>
&nbsp;
		   <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span>formulario.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;titulo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		   <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span>formulario.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;autor&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		   <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span>formulario.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;tematica&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		   
&nbsp;
		   sentenciaSql.<span style="color: #006633;">executeUpdate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; INSERT INTO libros VALUES (&quot;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;,'&quot;</span><span style="color: #339933;">+</span>formulario.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;titulo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;','&quot;</span><span style="color: #339933;">+</span>formulario.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;autor&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;','&quot;</span><span style="color: #339933;">+</span>formulario.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;tematica&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;');&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		   <span style="color: #666666; font-style: italic;">//recuperamos los datos de la base de datos</span>
&nbsp;
		   resultado <span style="color: #339933;">=</span> sentenciaSql.<span style="color: #006633;">executeQuery</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * from libros;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		   <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> resultado <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
		   <span style="color: #009900;">&#123;</span>
			   <span style="color: #666666; font-style: italic;">//creamos el array de libros</span>
			   listalibros <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span>  ArrayList<span style="color: #339933;">&lt;</span>Libro<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
			   <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>resultado.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
			   <span style="color: #009900;">&#123;</span>   
				   libro <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Libro<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
				   libro.<span style="color: #006633;">setTitulo</span><span style="color: #009900;">&#40;</span>resultado.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;titulo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				   libro.<span style="color: #006633;">setAutor</span><span style="color: #009900;">&#40;</span>resultado.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;autor&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				   libro.<span style="color: #006633;">setTematica</span><span style="color: #009900;">&#40;</span>resultado.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;tematica&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				   listalibros.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>libro<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			   <span style="color: #009900;">&#125;</span>
&nbsp;
			   <span style="color: #666666; font-style: italic;">//cerramos la conexion</span>
			   conexionr.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			   <span style="color: #666666; font-style: italic;">//establecemos el parámetro por peticion</span>
			   session <span style="color: #339933;">=</span> request.<span style="color: #006633;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			   session.<span style="color: #006633;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;listadolibros&quot;</span>, listalibros<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
&nbsp;
&nbsp;
			   envio <span style="color: #339933;">=</span> mapping.<span style="color: #006633;">findForward</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mostrardatos&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		   <span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span> 	<span style="color: #003399;">SQLException</span> e <span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			salida.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Excepcion Sql : &quot;</span><span style="color: #339933;">+</span> e.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> envio<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>	
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>formulariodatasource.jsp</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;%</span>@ page language<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;java&quot;</span> contentType<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/html; charset=UTF-8&quot;</span>  pageEncoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;%</span>@taglib uri<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://struts.apache.org/tags-bean&quot;</span> prefix<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;bean&quot;</span> <span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;%</span>@taglib uri<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://struts.apache.org/tags-html&quot;</span> prefix<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;html&quot;</span> <span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;!</span>DOCTYPE html <span style="color: #000000; font-weight: bold;">PUBLIC</span> <span style="color: #0000ff;">&quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;</span> <span style="color: #0000ff;">&quot;http://www.w3.org/TR/html4/loose.dtd&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>html<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>base<span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>meta http<span style="color: #339933;">-</span>equiv<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Content-Type&quot;</span> content<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/html; charset=UTF-8&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>link rel<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;stylesheet&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/css&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;../css/estilo.css&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;&lt;</span>bean<span style="color: #339933;">:</span>message key<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;formulariodatasource.titulo&quot;</span><span style="color: #339933;">/&gt;&lt;/</span>title<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;contenedor&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>h2<span style="color: #339933;">&gt;&lt;</span>bean<span style="color: #339933;">:</span>message key<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;formulariodatasource.titulo&quot;</span><span style="color: #339933;">/&gt;&lt;/</span>h2<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;errores&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>errors<span style="color: #339933;">/&gt;</span> 
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
 <span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;imagen&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>img srcKey<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;dinamico.registro.imagen&quot;</span> titleKey<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;dinamico.registro.imagen.alt&quot;</span><span style="color: #339933;">/&gt;</span>
  <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;contenido&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>form action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;formulariodatasource&quot;</span> focus<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;nombre&quot;</span> styleId<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;formulario&quot;</span> method<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;post&quot;</span><span style="color: #339933;">&gt;</span>
 <span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;campo&quot;</span><span style="color: #339933;">&gt;</span> 
  <span style="color: #339933;">&lt;</span>bean<span style="color: #339933;">:</span>message key<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;formulariodatasource.titulolibro&quot;</span><span style="color: #339933;">/&gt;</span>
  <span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>text property<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;titulo&quot;</span><span style="color: #339933;">/&gt;</span>
  <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span> 
  <span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;campo&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>bean<span style="color: #339933;">:</span>message key<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;formulariodatasource.autor&quot;</span><span style="color: #339933;">/&gt;</span>
  <span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>text property<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;autor&quot;</span><span style="color: #339933;">/&gt;</span>
  <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
&nbsp;
  <span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;campo&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>bean<span style="color: #339933;">:</span>message key<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;formulariodatasource.tematica&quot;</span> <span style="color: #339933;">/&gt;</span>
  <span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>text property<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;tematica&quot;</span><span style="color: #339933;">/&gt;</span>
  <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
&nbsp;
  <span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>submit styleClass<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;boton&quot;</span><span style="color: #339933;">&gt;</span> <span style="color: #339933;">&lt;</span>bean<span style="color: #339933;">:</span>message key<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;formulariodatasource.crear&quot;</span><span style="color: #339933;">/&gt;</span> <span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">:</span>submit<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>cancel styleClass<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;boton&quot;</span><span style="color: #339933;">&gt;</span> <span style="color: #339933;">&lt;</span>bean<span style="color: #339933;">:</span>message key<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;formulariodatasource.cancelar&quot;</span><span style="color: #339933;">/&gt;</span> <span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">:</span>cancel<span style="color: #339933;">&gt;</span>  
  <span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>reset  styleClass<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;boton&quot;</span><span style="color: #339933;">&gt;</span> borrar<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">:</span>reset<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">:</span>form<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;pie&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span> www.<span style="color: #006633;">railsymas</span>.<span style="color: #006633;">com</span> <span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">:</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Imagen correspondiente</p>
<p><img src="http://www.railsymas.com/wp-content/uploads/2009/12/formulariodatasource.png" alt="formulariodatasource" title="formulariodatasource" width="570" height="260" class="alignnone size-full wp-image-1087" /></p>
<p>mostrarlistado.jsp</p>
<p>Las etiquetas logic nos permiten iterar sobre los resultados obtenidos, logic:empty nos permite saber si el valor de la variable referenciada por name está vacío, en éste caso se utiliza para mostrar el mensaje correspondiente. Para comprobar que no está vacío se utiliza logic:notEmpty, para luego iterar por los resultado con logic:iterate</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;%</span>@ page language<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;java&quot;</span> contentType<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/html; charset=UTF-8&quot;</span> pageEncoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;%</span>@taglib uri<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://struts.apache.org/tags-html&quot;</span> prefix<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;html&quot;</span> <span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;%</span>@taglib uri<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://struts.apache.org/tags-logic&quot;</span> prefix<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;logic&quot;</span><span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;%</span>@taglib uri<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://struts.apache.org/tags-bean&quot;</span> prefix<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;bean&quot;</span> <span style="color: #339933;">%&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;!</span>DOCTYPE html <span style="color: #000000; font-weight: bold;">PUBLIC</span> <span style="color: #0000ff;">&quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;</span> <span style="color: #0000ff;">&quot;http://www.w3.org/TR/html4/loose.dtd&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>meta http<span style="color: #339933;">-</span>equiv<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Content-Type&quot;</span> content<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/html; charset=UTF-8&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>base<span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;&lt;</span>bean<span style="color: #339933;">:</span>message key<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;datasourceresultado.titulo&quot;</span><span style="color: #339933;">/&gt;&lt;/</span>title<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>link rel<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;stylesheet&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;../css/estilo.css&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/css&quot;</span><span style="color: #339933;">/&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;contenedor&quot;</span><span style="color: #339933;">&gt;</span>   
  <span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;tabla&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>logic<span style="color: #339933;">:</span>empty name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;listadolibros&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span> La base de datos de libros esta vac<span style="color: #339933;">&amp;</span>iacute<span style="color: #339933;">;</span>a <span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>logic<span style="color: #339933;">:</span>empty<span style="color: #339933;">&gt;</span>
&nbsp;
  <span style="color: #339933;">&lt;</span>logic<span style="color: #339933;">:</span>notEmpty name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;listadolibros&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>h2<span style="color: #339933;">&gt;&lt;</span>bean<span style="color: #339933;">:</span>message key<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;formulariodatasource.resultado&quot;</span><span style="color: #339933;">/&gt;&lt;/</span>h2<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>table title<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;tabla de datos libros&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;&lt;</span>th<span style="color: #339933;">&gt;</span> titulo <span style="color: #339933;">&lt;/</span>th<span style="color: #339933;">&gt;</span> <span style="color: #339933;">&lt;</span>th<span style="color: #339933;">&gt;</span> autor <span style="color: #339933;">&lt;/</span>th<span style="color: #339933;">&gt;</span> <span style="color: #339933;">&lt;</span>th<span style="color: #339933;">&gt;</span> tematica <span style="color: #339933;">&lt;/</span>th<span style="color: #339933;">&gt;&lt;/</span>tr<span style="color: #339933;">&gt;</span>
&nbsp;
  <span style="color: #339933;">&lt;</span>logic<span style="color: #339933;">:</span>iterate name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;listadolibros&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;libro&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>tr<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>td <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;par&quot;</span><span style="color: #339933;">&gt;</span>
     <span style="color: #339933;">&lt;</span>bean<span style="color: #339933;">:</span>write name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;libro&quot;</span> property<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;titulo&quot;</span><span style="color: #339933;">/&gt;</span>
    <span style="color: #339933;">&lt;/</span>td<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>td <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;impar&quot;</span><span style="color: #339933;">&gt;</span> 
     <span style="color: #339933;">&lt;</span>bean<span style="color: #339933;">:</span>write name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;libro&quot;</span> property<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;autor&quot;</span><span style="color: #339933;">/&gt;</span>
    <span style="color: #339933;">&lt;/</span>td<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>td <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;par&quot;</span><span style="color: #339933;">&gt;</span>
     <span style="color: #339933;">&lt;</span>bean<span style="color: #339933;">:</span>write name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;libro&quot;</span> property<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;tematica&quot;</span><span style="color: #339933;">/&gt;</span>
    <span style="color: #339933;">&lt;/</span>td<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>  
  <span style="color: #339933;">&lt;/</span>logic<span style="color: #339933;">:</span>iterate<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>table<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>logic<span style="color: #339933;">:</span>notEmpty<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>  
&nbsp;
  <span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;pie&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span> www.<span style="color: #006633;">railsymas</span>.<span style="color: #006633;">com</span> <span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>  
  <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Imagen correspondiente</p>
<p><img src="http://www.railsymas.com/wp-content/uploads/2009/12/listado1.png" alt="listado" title="listado" width="570" height="390" class="alignnone size-full wp-image-1091" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.railsymas.com/2010/01/11/datasource-struts-1-3-x-proyecto-web-eclipse/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Datasource Struts 1.2.x proyecto web eclipse</title>
		<link>http://www.railsymas.com/2009/12/14/datasource-struts-1-2-x-proyecto-web-eclipse/</link>
		<comments>http://www.railsymas.com/2009/12/14/datasource-struts-1-2-x-proyecto-web-eclipse/#comments</comments>
		<pubDate>Mon, 14 Dec 2009 15:41:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[struts]]></category>

		<guid isPermaLink="false">http://www.railsymas.com/?p=979</guid>
		<description><![CDATA[Tutorial de Struts Enlace al proyecto struts Datasource El datasource, se utiliza para establecer un pool de conexiones que implementan la interface javax.sql.DataSource. Permitiendo establecer una configuración para las bases de datos utilizadas en las aplicaciones de struts, y solamente desde las clases de negocio llamar a esta configuración para poder utilizarlas. Este tipo de [...]]]></description>
			<content:encoded><![CDATA[<p>Tutorial de Struts</p>
<p>Enlace al proyecto struts  <a href="http://struts.apache.org/1.1/faqs/database.html">Datasource</a></p>
<p>El datasource, se utiliza para establecer un pool de conexiones que implementan la interface javax.sql.DataSource. Permitiendo establecer una configuración para las bases de datos utilizadas en las aplicaciones de struts, y solamente desde las clases de negocio llamar a esta configuración para poder utilizarlas.</p>
<p><span id="more-979"></span></p>
<p>Este tipo de configuración es válido para las versiones de struts 1.1 hasta la versión 1.2.9. Este ejemplo a diferencia de los anteriores de struts en el que se utilizaba la versión 1.3.10, está realizado con la 1.2.9. </p>
<p>Datasource Struts </p>
<p>La declaración del datasource va dalante de los beans para la dtd struts-config_1_2.dtd</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE struts-config PUBLIC</span>
<span style="color: #00bbdd;">          &quot;-//Apache Software Foundation//DTD Struts Configuration 1.2//EN&quot;</span>
<span style="color: #00bbdd;">          &quot;http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd&quot;&gt;</span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;struts-config<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!-- ============================================ Data Source Configuration --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;data-sources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Configuración del archivo struts-config.xml  para el datasource</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;data-sources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;data-source</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;org.apache.tomcat.dbcp.dbcp.BasicDataSource&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;set-property</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;driverClassName&quot;</span>   <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;com.mysql.jdbc.Driver&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
&nbsp;
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;set-property</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;url&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;jdbc:mysql://localhost:3306/libros_jdbc&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;set-property</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;username&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;root&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;set-property</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;password&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;root&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;set-property</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;maxActive&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;10&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;set-property</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;defaultReadOnly&quot;</span>  <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;set-property</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;defaultAutoCommit&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;set-property</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;validationQuery&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;SELECT COUNT(*) FROM libros&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/data-source<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/data-sources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>configuración de los actions para el struts-config.xml  </p>
<p>Creamos los actions para el ejemplo, simplemente redireccionamos la acción Welcome.do a la acción &#8216;conexión&#8217;. Si se establece la conexión correctamente se redirecciona a la página</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!-- =========================================== Action Mapping Definitions --&gt;</span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action-mappings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #808080; font-style: italic;">&lt;!-- Default &quot;Welcome&quot; action --&gt;</span>
            <span style="color: #808080; font-style: italic;">&lt;!-- Forwards to Welcome.jsp --&gt;</span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span></span>
<span style="color: #009900;">            <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/Welcome&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">forward</span>=<span style="color: #ff0000;">&quot;/conexion.do&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span> </span>
&nbsp;
<span style="color: #009900;">           <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/conexion&quot;</span></span>
<span style="color: #009900;">           <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;accion.accionBaseDatos&quot;</span></span>
<span style="color: #009900;">           <span style="color: #000066;">scope</span>=<span style="color: #ff0000;">&quot;request&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
           <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;forward</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;satisfactoria&quot;</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/pages/registroSatisfactorio.jsp&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
           <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;forward</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;fallida&quot;</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/pages/fallida.jsp&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/action<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Código de la página fallida.jsp</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;%</span>@ page language<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;java&quot;</span> contentType<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/html; charset=ISO-8859-1&quot;</span> pageEncoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;ISO-8859-1&quot;</span><span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;%</span>@taglib uri<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/tags/struts-html&quot;</span> prefix<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;html&quot;</span> <span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;%</span>@taglib uri<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/tags/struts-bean&quot;</span> prefix<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;bean&quot;</span> <span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;!</span>DOCTYPE html <span style="color: #000000; font-weight: bold;">PUBLIC</span> <span style="color: #0000ff;">&quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;</span> <span style="color: #0000ff;">&quot;http://www.w3.org/TR/html4/loose.dtd&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>meta http<span style="color: #339933;">-</span>equiv<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Content-Type&quot;</span> content<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/html; charset=ISO-8859-1&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;</span>Conexion Fallida<span style="color: #339933;">&lt;/</span>title<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>base<span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>link rel<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;stylesheet&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/css&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;../css/estilo.css&quot;</span><span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;contenedor&quot;</span><span style="color: #339933;">&gt;</span>
   <span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;contenido&quot;</span><span style="color: #339933;">&gt;</span>
      <span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span> conexion fallida <span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span>
   <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
   <span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;pie&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span> www.<span style="color: #006633;">railsymas</span>.<span style="color: #006633;">com</span> <span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span>
   <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Código de la página registro satisfactorio</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;%</span>@ page language<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;java&quot;</span> contentType<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/html; charset=ISO-8859-1&quot;</span> pageEncoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;ISO-8859-1&quot;</span><span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;%</span>@taglib uri<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/tags/struts-html&quot;</span> prefix<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;html&quot;</span> <span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;%</span>@taglib uri<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/tags/struts-bean&quot;</span> prefix<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;bean&quot;</span> <span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;!</span>DOCTYPE html <span style="color: #000000; font-weight: bold;">PUBLIC</span> <span style="color: #0000ff;">&quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;</span> <span style="color: #0000ff;">&quot;http://www.w3.org/TR/html4/loose.dtd&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>html<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>meta http<span style="color: #339933;">-</span>equiv<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Content-Type&quot;</span> content<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/html; charset=ISO-8859-1&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;</span>Consulta a la base de datos<span style="color: #339933;">&lt;/</span>title<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>base<span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>link rel<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;stylesheet&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;../css/estilo.css&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/css&quot;</span><span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;contenedor&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;contenido&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span> Establecemos conexion <span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;pie&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span> www.<span style="color: #006633;">railsymas</span>.<span style="color: #006633;">com</span> <span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">:</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>resultado de la ejecución por pantalla</p>
<p><img src="http://www.railsymas.com/wp-content/uploads/2009/12/strut1.2datasource.png" alt="strut1.2datasource" title="strut1.2datasource" width="570" height="80" class="alignnone size-full wp-image-1026" /></p>
<p>Código de la clase java accionBaseDatos dentro del package de acciones</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">accion</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.Action</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionForward</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionForm</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionMapping</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletRequest</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletResponse</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.sql.DataSource</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.Connection</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.Statement</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.ResultSet</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.SQLException</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> accionBaseDatos <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">Action</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> DataSource datasource<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Connection</span> conexion<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> devolucion<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Statement</span> sentenciaSql<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">ResultSet</span> resultado<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> ActionForward execute <span style="color: #009900;">&#40;</span> ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">try</span>
		<span style="color: #009900;">&#123;</span>
			datasource <span style="color: #339933;">=</span> getDataSource<span style="color: #009900;">&#40;</span>request<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			conexion <span style="color: #339933;">=</span> datasource.<span style="color: #006633;">getConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			sentenciaSql <span style="color: #339933;">=</span> conexion.<span style="color: #006633;">createStatement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			resultado <span style="color: #339933;">=</span> sentenciaSql.<span style="color: #006633;">executeQuery</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Select * From libros&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>resultado.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>resultado.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;autor&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>conexion <span style="color: #339933;">!=</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				devolucion<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;satisfactoria&quot;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
			<span style="color: #009900;">&#125;</span>
&nbsp;
			conexion.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">SQLException</span> e<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
&nbsp;
		  devolucion<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;fallida&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> mapping.<span style="color: #006633;">findForward</span><span style="color: #009900;">&#40;</span>devolucion<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>		
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>resultado en consola de la ejecucion, los autores de la base de datos de libros_jdbc para la tabla libros</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;">INFO: Tiles definition factory found for request processor ''.
autor1
autor2
autor3
autor4
autor5
autor6
autor7
autor7
autor8
autor9
autor10</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.railsymas.com/2009/12/14/datasource-struts-1-2-x-proyecto-web-eclipse/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Tutorial de LookupDispatchaction struts</title>
		<link>http://www.railsymas.com/2009/11/20/tutorial-de-lookupdispatchaction-struts/</link>
		<comments>http://www.railsymas.com/2009/11/20/tutorial-de-lookupdispatchaction-struts/#comments</comments>
		<pubDate>Fri, 20 Nov 2009 15:01:59 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[struts]]></category>

		<guid isPermaLink="false">http://www.railsymas.com/?p=929</guid>
		<description><![CDATA[LookupDispatchaction es otra clase de struts que complementa los ejercicios anteriores sobre formularios struts. Habíamos visto la clase Dispatchaction en struts, que aportaba la posibilidad de realizar distintas acciones en una sola clase, añadiendo una nueva potencialidad a las clases action, las cuales solamente tenían el método service genérico para responder a las peticiones de [...]]]></description>
			<content:encoded><![CDATA[<p>LookupDispatchaction es otra clase de struts que complementa los ejercicios anteriores sobre formularios struts.</p>
<p>Habíamos visto la clase Dispatchaction en struts, que aportaba la posibilidad de realizar distintas acciones en una sola clase, añadiendo una nueva potencialidad a las clases action, las cuales solamente tenían el método service genérico para responder a las peticiones de los usuarios.</p>
<p>LookupDispatchaction tiene también en su configuración un parámetro parameter, que indica el nombre del método. Lookupdispatchaction tiene la ventaja que permite desde un mismo formulario poder llamar a distintos métodos. </p>
<p><span id="more-929"></span></p>
<p><img src="http://www.railsymas.com/wp-content/uploads/2009/11/Lookupdispatchaction.png" alt="Lookupdispatchaction" title="Lookupdispatchaction" width="570" height="300" class="alignnone size-full wp-image-933" /></p>
<p>código de página ejercicios.jsp como los anteriores ejemplos</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;enlace&quot;</span><span style="color: #339933;">&gt;</span>
   <span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>link action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/dinamicoLookup&quot;</span><span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">6</span><span style="color: #009900;">&#41;</span> lookupdispatchAction  <span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">:</span>link<span style="color: #339933;">&gt;</span>
   <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p><img src="http://www.railsymas.com/wp-content/uploads/2009/11/lookupdispatchaction.png" alt="lookupdispatchaction" title="lookupdispatchaction" width="570" height="220" class="alignnone size-full wp-image-947" /></p>
<p>Configuracion del bean DynaValidatorForm</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;">&nbsp;
!-- ================================================ Form Bean Definitions --&gt;
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form-bean</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;dinamicolibro&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;org.apache.struts.validator.DynaValidatorForm&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>    
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form-property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;titulo&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;java.lang.String&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form-property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;autor&quot;</span>  <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;java.lang.String&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>    
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form-property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;cambio&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;java.lang.String&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>    
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/form-bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Configuración del archivo struts-config.xml para Action Mapping</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!-- =========================================== Action Mapping Definitions --&gt;</span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span>        </span>
<span style="color: #009900;">        <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/dinamicoLookup&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">forward</span>=<span style="color: #ff0000;">&quot;/pages/formularioLookup.jsp&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span>         </span>
<span style="color: #009900;">        <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/lookupDispatchAction&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;acciones.AccionLookupDispatchAction&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">scope</span>=<span style="color: #ff0000;">&quot;request&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">validate</span>=<span style="color: #ff0000;">&quot;true&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;dinamicolibro&quot;</span>       </span>
<span style="color: #009900;">        input =<span style="color: #ff0000;">&quot;/pages/formularioLookup.jsp&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">parameter</span>=<span style="color: #ff0000;">&quot;method&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>         
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;set-property</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;cancellable&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span> 
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;forward</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;cancelada&quot;</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/pages/cancelada2.jsp&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;forward</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;editarlibros&quot;</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/pages/edicion2.jsp&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>   
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/action<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span>         </span>
<span style="color: #009900;">        <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/LookupDispatchAction2&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;acciones.AccionLookupDispatchAction&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">scope</span>=<span style="color: #ff0000;">&quot;request&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">validate</span>=<span style="color: #ff0000;">&quot;false&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">name</span>= <span style="color: #ff0000;">&quot;dinamicolibro&quot;</span>                       </span>
<span style="color: #009900;">        <span style="color: #000066;">parameter</span>= <span style="color: #ff0000;">&quot;method&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;set-property</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;cancellable&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span> 
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;forward</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;cancelada&quot;</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/pages/cancelada2.jsp&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;forward</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;datoslibro&quot;</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/pages/datoslibros.jsp&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/action<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Configuración de la validación de los campos del bean en el fichero validation.xml dentro del tag</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form-validation<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Como repaso desde DynaValidatorForm, se creaban unos formularios bean dinámicos, que permitían validar los campos de un formulario sin teneer que crear la clase actionForm. En el fichero xml validation.xml se crean las reglas para que los campos sean obligatorios.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;formset<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;dinamicolibro&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;field</span>       </span>
<span style="color: #009900;">        <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;titulo&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">depends</span>=<span style="color: #ff0000;">&quot;required&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>        
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;arg</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;libro.titulo&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/field<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;field</span></span>
<span style="color: #009900;">        <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;autor&quot;</span> </span>
<span style="color: #009900;">        <span style="color: #000066;">depends</span>=<span style="color: #ff0000;">&quot;required&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;arg</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;libro.autor&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/field<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>    
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/form<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>  
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/formset<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Configuración del fichero de propiedades properties</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;">#lookupdispathAction
formularioLookup.cancelar = cancelar
formularioLookup.crear = crear
formularioLookup.editartitulo= EditarTitulo
formularioLookup.editarautor = EditarAutor  
tituloLookup.libro = LookupDispatchAction
Lookup.titulo = titulo 
Lookup.autor = autor</pre></td></tr></table></div>

<p>Código de la página jsp formularioLookup.jsp</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;%</span>@ page language<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;java&quot;</span> contentType<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/html; charset=UTF-8&quot;</span>    pageEncoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;!</span>DOCTYPE html <span style="color: #000000; font-weight: bold;">PUBLIC</span> <span style="color: #0000ff;">&quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;</span> <span style="color: #0000ff;">&quot;http://www.w3.org/TR/html4/loose.dtd&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;%</span>@taglib uri<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://struts.apache.org/tags-html&quot;</span> prefix<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;html&quot;</span> <span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;%</span>@taglib uri<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://struts.apache.org/tags-bean&quot;</span> prefix<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;bean&quot;</span> <span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>html<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>meta http<span style="color: #339933;">-</span>equiv<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Content-Type&quot;</span> content<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/html; charset=UTF-8&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;&lt;</span>bean<span style="color: #339933;">:</span>message key<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;tituloLookup.libro&quot;</span><span style="color: #339933;">/&gt;&lt;/</span>title<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>base<span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>link rel<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;stylesheet&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/css&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;../css/estilo.css&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;contenedor&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>h2<span style="color: #339933;">&gt;&lt;</span>bean<span style="color: #339933;">:</span>message key<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;tituloLookup.libro&quot;</span><span style="color: #339933;">/&gt;&lt;/</span>h2<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;errores&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>errors<span style="color: #339933;">/&gt;</span> 
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
 <span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;imagen&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>img srcKey<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;libro.imagen&quot;</span> titleKey<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;libro.imagen.alt&quot;</span><span style="color: #339933;">/&gt;</span>
  <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;contenido&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>form action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;lookupDispatchAction&quot;</span> focus<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;nombre&quot;</span> styleId<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;formulario&quot;</span> method<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;post&quot;</span><span style="color: #339933;">&gt;</span>
 <span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;campo&quot;</span><span style="color: #339933;">&gt;</span> 
  <span style="color: #339933;">&lt;</span>bean<span style="color: #339933;">:</span>message key<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Lookup.titulo&quot;</span><span style="color: #339933;">/&gt;</span>
  <span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>text property<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;titulo&quot;</span> <span style="color: #339933;">/&gt;</span>
  <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span> 
&nbsp;
  <span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;campo&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>bean<span style="color: #339933;">:</span>message key<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Lookup.autor&quot;</span><span style="color: #339933;">/&gt;</span>
  <span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>text property<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;autor&quot;</span> <span style="color: #339933;">/&gt;</span>
  <span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
&nbsp;
&nbsp;
  <span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>submit styleClass<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;boton&quot;</span> property<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;method&quot;</span><span style="color: #339933;">&gt;&lt;</span>bean<span style="color: #339933;">:</span>message key<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;formularioLookup.crear&quot;</span><span style="color: #339933;">/&gt;</span>  <span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">:</span>submit<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>cancel styleClass<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;boton&quot;</span><span style="color: #339933;">&gt;&lt;</span>bean<span style="color: #339933;">:</span>message key<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;formularioLookup.cancelar&quot;</span><span style="color: #339933;">/&gt;</span> <span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">:</span>cancel<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;</span>html<span style="color: #339933;">:</span>reset  styleClass<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;boton&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;borrar&quot;</span><span style="color: #339933;">/&gt;</span> 
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">:</span>form<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;pie&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;</span> www.<span style="color: #006633;">railsymas</span>.<span style="color: #006633;">com</span> <span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span> 
&nbsp;
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">:</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Clase LookupDispatchAction  AccionLookupDispatchAction.java</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">acciones</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionForm</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionMapping</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionForward</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.actions.LookupDispatchAction</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.validator.DynaValidatorForm</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletRequest</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletResponse</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Map</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.HashMap</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> AccionLookupDispatchAction <span style="color: #000000; font-weight: bold;">extends</span> LookupDispatchAction <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> ActionForward envio<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> DynaValidatorForm formulario<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> titulo<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> autor<span style="color: #339933;">;</span>
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">Map</span> getKeyMethodMap <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">Map</span> map <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span>  <span style="color: #003399;">HashMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
		map.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;formularioLookup.editartitulo&quot;</span>,<span style="color: #0000ff;">&quot;EditarTitulo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		map.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;formularioLookup.editarautor&quot;</span>,<span style="color: #0000ff;">&quot;EditarAutor&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
		map.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;formularioLookup.crear&quot;</span>,<span style="color: #0000ff;">&quot;crear&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> map<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">protected</span> ActionForward cancelled <span style="color: #009900;">&#40;</span>ActionMapping mapping,ActionForm form,HttpServletRequest request, HttpServletResponse response<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isCancelled<span style="color: #009900;">&#40;</span>request<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
		     envio <span style="color: #339933;">=</span> mapping.<span style="color: #006633;">findForward</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;cancelada&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">return</span> envio<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #666666; font-style: italic;">//creamos los métodos que responden a las acciones</span>
	<span style="color: #000000; font-weight: bold;">public</span>  ActionForward crear <span style="color: #009900;">&#40;</span> ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response <span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span>
	<span style="color: #009900;">&#123;</span>
		  <span style="color: #666666; font-style: italic;">//recuperamos los datos del formulario bean dinámico Validator</span>
		  formulario <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>DynaValidatorForm<span style="color: #009900;">&#41;</span> form<span style="color: #339933;">;</span>
&nbsp;
		  titulo <span style="color: #339933;">=</span> formulario.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;titulo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		  autor <span style="color: #339933;">=</span> formulario.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;autor&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
		  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>titulo<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	      envio <span style="color: #339933;">=</span> mapping.<span style="color: #006633;">findForward</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;editarlibros&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> envio<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> ActionForward EditarTitulo <span style="color: #009900;">&#40;</span>ActionMapping mapping, ActionForm form, HttpServletRequest resquest, HttpServletResponse response<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span>
	<span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//recuperamos los datos del formulario bean dinámico Validator</span>
	    formulario <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>DynaValidatorForm<span style="color: #009900;">&#41;</span> form<span style="color: #339933;">;</span>
&nbsp;
	    <span style="color: #003399;">String</span> cadena <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Se ha cambiado &quot;</span><span style="color: #339933;">+</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">titulo</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot; por &quot;</span><span style="color: #339933;">+</span>formulario.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;titulo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	    formulario.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;cambio&quot;</span>,cadena<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	    <span style="color: #666666; font-style: italic;">//recargamos el formulario con el valor guardado</span>
	     formulario.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;autor&quot;</span>,<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">autor</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
		 envio <span style="color: #339933;">=</span> mapping.<span style="color: #006633;">findForward</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;datoslibro&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		 <span style="color: #000000; font-weight: bold;">return</span> envio<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> ActionForward EditarAutor <span style="color: #009900;">&#40;</span>ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//recuperamos los datos del formulario bean dinámico Validator</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//recuperamos los datos del formulario bean dinámico Validator</span>
	    formulario <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>DynaValidatorForm<span style="color: #009900;">&#41;</span> form<span style="color: #339933;">;</span>
&nbsp;
	    <span style="color: #003399;">String</span> cadena <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Se ha cambiado &quot;</span><span style="color: #339933;">+</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">autor</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot; por &quot;</span><span style="color: #339933;">+</span>formulario.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;autor&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
&nbsp;
	    formulario.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;cambio&quot;</span>,cadena<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	    <span style="color: #666666; font-style: italic;">//recargamos el formulario con el </span>
	    formulario.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;titulo&quot;</span>,<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">titulo</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
	    envio <span style="color: #339933;">=</span> mapping.<span style="color: #006633;">findForward</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;datoslibro&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
&nbsp;
	    <span style="color: #000000; font-weight: bold;">return</span> envio<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>LookupDispatchAction, permite al igual que con dispatchaction tener varias acciones (métodos) en la misma clase de tal forma que tenemos dos formularios que llaman a la misma clase.  A través de los diferentes métodos se realizan distintas acciones.</p>
<p>Se utiliza un HashMap para establecer un conjunto de contraseñas para los distintos métodos a usar, esas contraseñas son las ulilizadas en los botones de las páginas jsp.</p>
<p>Es necesario crear el método protected Map getKeyMethodMap para la creación del conjunto de claves método.</p>
<p>Si se quiere incluir el botón de cancelación con el uso de esta clase, la configuración en el fichero struts-config.xml  para ello  no responde, por lo que hay que hacer cambios en la clase java e incluir el método cancelled para sobreescribirlo y responder al botón de cancelación.</p>
<p>Imagen resultado del envío de los datos</p>
<p><img src="http://www.railsymas.com/wp-content/uploads/2009/09/libros22.png" alt="libros2" title="libros2" width="570" height="370" class="alignnone size-full wp-image-662" /></p>
<p>Introducimos titulo2 en el campo de titulo</p>
<p><img src="http://www.railsymas.com/wp-content/uploads/2009/09/libros3.png" alt="libros3" title="libros3" width="570" height="370" class="alignnone size-full wp-image-666" /></p>
<p>resultado final</p>
<p><img src="http://www.railsymas.com/wp-content/uploads/2009/09/libros4.png" alt="libros4" title="libros4" width="570" height="290" class="alignnone size-full wp-image-667" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.railsymas.com/2009/11/20/tutorial-de-lookupdispatchaction-struts/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Tutorial html struts tags</title>
		<link>http://www.railsymas.com/2009/11/04/tutorial-html-struts-tags/</link>
		<comments>http://www.railsymas.com/2009/11/04/tutorial-html-struts-tags/#comments</comments>
		<pubDate>Wed, 04 Nov 2009 16:45:13 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[struts]]></category>

		<guid isPermaLink="false">http://www.railsymas.com/?p=883</guid>
		<description><![CDATA[En ejemplos anteriores, se había utilizado unas etiquetas que podían resultar diferentes si venimos de otros lenguajes de programación web. Struts utiliza unas etiquetas html muy similares a las de html pero con algunas variantes. Siempre se antepone la etiqueta html, de tal forma que para indicar el inicio y el fin del documento se [...]]]></description>
			<content:encoded><![CDATA[<p>En ejemplos anteriores, se había utilizado unas etiquetas que podían resultar diferentes si venimos de otros lenguajes de programación web.</p>
<p>Struts utiliza unas etiquetas html muy similares a las de html pero con algunas variantes. Siempre se antepone la etiqueta html,  de tal forma que para indicar el inicio y el fin del documento se pone <html:html> y </html:html>  </p>
<p>Para su utilización en una página jsp debemos incluir la siguiente sintaxis para el caso de 1.3.10</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"> <span style="color: #339933;">&lt;%</span>@taglib uri<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://struts.apache.org/tags-html&quot;</span> prefix<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;html&quot;</span> <span style="color: #339933;">%&gt;</span></pre></div></div>

<p>el valor del atributo prefix se pone html para no equivocarse pero podría se cualquier otro nombre</p>
<p><span id="more-883"></span></p>
<table border="1" caption="listado de html struts tags">
<tr>
<td>html</td>
<td> equivale a html</td>
</tr>
<tr>
<td>button</td>
<td> equivalente a input type=button</td>
</tr>
<tr>
<td>cancel</td>
<td> equivale a input type=subtmit&#8221;</td>
</tr>
<tr>
<td>checkbox</td>
<td> equivale a input type=checkbox </td>
</tr>
<tr>
<td>hidden</td>
<td> equivale a input type=hidden </td>
</tr>
<tr>
<td>img</td>
<td> equivale a img </td>
</tr>
<tr>
<td>javascript</td>
<td> equivale a javascript </td>
</tr>
<tr>
<td>link</td>
<td> equivale a  &#8220;a&#8221;  </td>
</tr>
<tr>
<td>base</td>
<td>base de la página </td>
</tr>
<tr>
<td>messages</td>
<td> muestra los mensajes del idioma</td>
</tr>
<tr>
<td>multibox</td>
<td>  type checkbox pero permite un array de string de los property iguales</td>
</tr>
<tr>
<td>option</td>
<td> equivale a option</td>
</tr>
<tr>
<td>password</td>
<td> equivale a password</td>
</tr>
<tr>
<td>radio</td>
<td> equivale a radio</td>
</tr>
<tr>
<td>reset</td>
<td> equivale a reset</td>
</tr>
<tr>
<td>select</td>
<td> equivale a select</td>
</tr>
<tr>
<td>submit</td>
<td> equivale a type submit</td>
</tr>
<tr>
<td>text</td>
<td> equivale a type text</td>
</tr>
<tr>
<td>textarea</td>
<td> equivale a textarea</td>
</tr>
<tr>
<td>frame</td>
<td> equivale a frame</td>
</tr>
<tr>
<td>form</td>
<td> equivale a html</td>
</tr>
<tr>
<td>file</td>
<td> equivale type file</td>
</tr>
</table>
<p>En la descarga del proyecto struts existe una carpteta docs con html con toda la informacion referente a los tags html como bean logic tiles etc  &#8230;..</p>
]]></content:encoded>
			<wfw:commentRss>http://www.railsymas.com/2009/11/04/tutorial-html-struts-tags/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
