
[STRUTS 1] Error en el flujo de ejecución de ActionForm
Publicado por Emiliano (3 intervenciones) el 22/09/2015 11:26:42
Hola.
Estoy realizando el ejercicio de evaluacion con Struts para un curso que estoy haciendo y me pasa algo que no entiendo.
Se trata de una aplicacion web (sencilla) de gestion de una libreria con Struts 1.
He realizado correctamente la funcionalidad de visualizar todos los libros, sin embargo implementando la de visualizar un libro por titulo al pinchar sobre el enlace de la INDEX.JSP "Consulta libro titulo" cuyo href apunta "formularioConsultaTitulo.jsp" en vez de irme a la JSP y de aqui despues de pinchar el boton submit llamar el correspondiente ActionForm y seguidamente el Action, el flujo de ejecucion toma una "dirección extraña" y pasa antes por el ACTIONFORM, seguidamente por la JSP, y por ultimo por el Action.
El resultado de esto es que el BuacarLibroActionForm intenta recuperer un parametro que todavia no existe (el formualrio no se ha enviado todavía y el contexto está vacio) y por supesto por ultimo el Action no recibe ningun parametro ya que no hay nincguna clase ActionForm que lo recoja del contexto.
los xml de configuracion me parecen correctos asi que no se...
podéis echarme una mano porfa?
aquí os pego el código
WEB.XML
STRUTS-CONFIG.XML
INDEX.JSP
FORMULARIOCONSULTATITULO.JAVA
BUSCARLIBROACTIONFORM.JAVA
ACTIONCONSULTATITULO.JAVA
Gracias a todos los que me podrán ayudar.
Estoy realizando el ejercicio de evaluacion con Struts para un curso que estoy haciendo y me pasa algo que no entiendo.
Se trata de una aplicacion web (sencilla) de gestion de una libreria con Struts 1.
He realizado correctamente la funcionalidad de visualizar todos los libros, sin embargo implementando la de visualizar un libro por titulo al pinchar sobre el enlace de la INDEX.JSP "Consulta libro titulo" cuyo href apunta "formularioConsultaTitulo.jsp" en vez de irme a la JSP y de aqui despues de pinchar el boton submit llamar el correspondiente ActionForm y seguidamente el Action, el flujo de ejecucion toma una "dirección extraña" y pasa antes por el ACTIONFORM, seguidamente por la JSP, y por ultimo por el Action.
El resultado de esto es que el BuacarLibroActionForm intenta recuperer un parametro que todavia no existe (el formualrio no se ha enviado todavía y el contexto está vacio) y por supesto por ultimo el Action no recibe ningun parametro ya que no hay nincguna clase ActionForm que lo recoja del contexto.
los xml de configuracion me parecen correctos asi que no se...
podéis echarme una mano porfa?
aquí os pego el código
WEB.XML
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
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="Libreria_Web" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>LibreriaWeb</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<context-param>
<param-name>LibroHoy</param-name>
<param-value>La sombra del viento (C. Ruiz Zafon)</param-value>
</context-param>
</web-app>
STRUTS-CONFIG.XML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation/DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="buscarForm" type="com.libreriaweb.actionforms.BuscarLibroActionForm"></form-bean>
</form-beans>
<action-mappings>
<action path="/todos" type="com.libreriaweb.actions.ActionConsultaTodos">
<forward name ="ok" path="/vistaConsultaLibros.jsp" />
</action>
<action path="/buscarTitulo" type="com.libreriaweb.actions.ActionConsultaTitulo" name ="buscarForm" input="/formularioConsultaTitulo.jsp">
<forward name ="ok" path="/vistaConsultaLibros.jsp" />
</action>
</action-mappings>
</struts-config>
INDEX.JSP
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
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Libreria Web</title>
</head>
<body>
<h3>LIBRERIA STRUTS</h3>
<a href="formularioAltaLibro.jsp">Alta libro</a>
<br>
<a href="todos.do">Consulta todos libros (STRTUS)</a>
<br>
<a href="formularioConsultaTitulo.jsp">Conuslta libro titulo (STRUTS)</a>
<br>
<a href="formularioConsultaISBN.jsp">Consulta libro ISBN</a>
<br>
<a href="formularioBajaLibro.jsp">Baja libro</a>
<br>
<a href="formularioModificacionLibro.jsp">Modificar precio libro</a>
<br>
<br>
<a href="vistaCarrito.jsp">Visualizar carrito compra</a>
<h4>Oferta de hoy: <%=application.getAttribute("ofertaHoy") %></h4>
</body>
</html>
FORMULARIOCONSULTATITULO.JAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib prefix="html" uri="http://jakarta.apache.org/struts/tags-html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Consulta libro (STRUTS)</title>
</head>
<body>
CONSULTA LIBRO POR TITULO (STRUTS)
<html:form action="buscarTitulo">
Titulo : <html:text property="parametroBusqueda"/> <br>
<html:submit value ="Buscar"/> <html:reset value="Borrar"/>
</html:form>
</body>
</html>
BUSCARLIBROACTIONFORM.JAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.libreriaweb.actionforms;
import org.apache.struts.action.ActionForm;
@SuppressWarnings("serial")
public class BuscarLibroActionForm extends ActionForm{
private String parametroBusqueda;
public String getParametroBusqueda(){
return parametroBusqueda;
}
public void setParametroBuqueda(String param){
parametroBusqueda=param;
}
}
ACTIONCONSULTATITULO.JAVA
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
package com.libreriaweb.actions;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.libreriaweb.actionforms.BuscarLibroActionForm;
import com.libreriaweb.modelo.Libro;
import com.libreriaweb.negocio.CapaBusinessLibreria;
import com.libreriaweb.negocio.ItfzCapaBusinessLibreria;
public class ActionConsultaTitulo extends Action {
@SuppressWarnings("finally")
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// Recuperar valores del actionForm
String tituloLibro = ((BuscarLibroActionForm) form)
.getParametroBusqueda();
ItfzCapaBusinessLibreria business = new CapaBusinessLibreria();
List<Libro> librosEncontrados = null;
try {
// intenta recuperar el libro de BD
librosEncontrados = business.consultarTitulo(tituloLibro);
} catch (Exception e) {
librosEncontrados = null;
e.printStackTrace();
} finally {
// redirecciona a JSP de mensaje y envia el mensaje
request.setAttribute("librosEncontrados", librosEncontrados);
// RequestDispatcher rd =
// request.getRequestDispatcher("/vistaConsultaLibros.jsp");
return mapping.findForward("ok");
}
}
}
Gracias a todos los que me podrán ayudar.
Valora esta pregunta


0