« Return to Thread: Java API - Estrutura de árvore.
Bom dia ,
To Usando o struts 1.3.9 e não consigo retornar as
ActionMessage. Qdo simulo um erro, não o recebo do ActionForm. Alguma ajuda???
Por exemplo, fiz um teste....
package br.com.surson.negocio;
import java.util.Iterator;
import java.util.LinkedList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.*;
public class ExcluirCursosAction extends Action{
public ActionForward execute(ActionMapping
mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse
response)throws Exception {
ActionErrors errors = new
ActionErrors();
try{
HttpSession s =
request.getSession();
String
idCurso = request.getParameter("idCurso");
// remove o bean
da pagina altera jsp para garantir que não haja sugeira
// traz a lista
de cursos para poder listá-los afim de achar o que bate com a solicitação
LinkedList listaCursos =
(LinkedList)s.getAttribute("listabean");
Iterator i =
listaCursos.iterator();
while
(i.hasNext()){
Curso
curso = (Curso) i.next();
if
(curso.getIdCurso()== Integer.parseInt(idCurso)){
AdminCursos
c = new AdminCursos();
c.excluirCurso(curso);
LinkedList
lista = c.obterListasCursos();
s.setAttribute("listabean",
lista);
break;
}
}
}catch (Exception e) {
return(mapping.findForward("failure"));
}
return(mapping.findForward("success"));
}
}
package
br.com.surson.negocio;
import
javax.servlet.http.HttpServletRequest;
import
org.apache.struts.action.*;
import
org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessage;
// função de lidar
com os campos do nosso formulário... Ele
Validam e resetam
public class SalvarCursoForm
extends ActionForm{
private int idCurso;
private
String curso;
public void
reset(ActionMapping mapping, HttpServletRequest request){
idCurso = 0;
curso = "";
}
public
ActionErrors validate(ActionMapping mapping, HttpServletRequest request){
ActionErrors
errors = new ActionErrors();
if (( curso == null) ||
(curso.length()<1)){
errors.add("curso", new ActionMessage("Curso é
requerido"));
}
return
errors;
}
public void
setIdCurso(int idCurso){
this.idCurso = idCurso;
}
public int getIdCurso(){
return this.idCurso;
}
public void
setCurso(String curso){
this.curso = curso;
}
public String
getCurso(){
return this.curso;
}
}
<%@ page language="java"
contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import = "br.com.surson.negocio.*"
%>
<%@ page import = "java.util.*"
%>
<%@ taglib uri="http://struts.apache.org/tags-bean"
prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-logic"
prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-html"
prefix="html" %>
<logic:notPresent name="incluirCursoBean" scope="session" >
<logic:redirect forward="error"/>
</logic:notPresent>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html:html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Incluir
Curso</title>
</head>
<body>
<center>
<br>
<html:form action="incluirNovoCurso.do" method="get">
<table border = 0 >
<tr>
<td align="rigth" Curso:></td>
<td align="left" ><b>
<html:text property="curso" name="incluirCursoBean"
/>
</b>
</td>
</tr>
<tr>
<td colspan=2 align="center">
<html:submit>Incluir</html:submit>
<html:reset>Cancelar</html:reset>
</td>
</tr>
</table>
</html:form>
<logic:messagesPresent message="true" >
<html:messages id="curso" message="true">
<bean:write name="curso" />
</html:messages>
</logic:messagesPresent>
</body>
</html:html>
<?xml version="1.0" encoding="ISO-8859-1"
?>
<struts-config>
<!--
================================================ Form Bean Definitions -->
<form-beans>
<form-bean dynamic="false" name="salvarCursoForm"
type="br.com.surson.negocio.SalvarCursoForm"/>
<form-bean dynamic="false" name="incluirCursoForm"
type="br.com.surson.negocio.IncluirCursoForm"/>
</form-beans>
<!--
========================================= Global Exception Definitions -->
<global-exceptions>
</global-exceptions>
<!--
=========================================== Global Forward Definitions -->
<global-forwards>
<!--
Default forward to "Welcome" action -->
<!--
Demonstrates using index.jsp to forward -->
<forward
name="welcome"
path="/Welcome.do"/>
<forward name="failure"
path="/pages/error.jsp" redirect="false" contextRelative="false"/>
</global-forwards>
<!--
=========================================== Action Mapping Definitions -->
<action-mappings>
<action path="/listaCursos"
scope="session"
unknown="false"
validate="false"
type="br.com.surson.negocio.ListaCursosAction">
<forward
name="success"
path="/pages/listacursos.jsp"
redirect="false"
contextRelative="false">
</forward>
</action>
<action path="/alterarCursos"
scope="session"
unknown="false"
validate="false"
type="br.com.surson.negocio.AlterarCursosAction">
<forward
name="success"
path="/pages/alterarcursos.jsp"
redirect="false"
contextRelative="false">
</forward>
</action>
<action
path="/salvarCurso"
attribute="salvarCursoForm"
input = "/pages/alterarcursos.jsp"
name="salvarCursoForm"
scope="session"
unknown="false"
validate="true"
type="br.com.surson.negocio.SalvarCursoAction">
<forward
name="success"
path="/pages/listacursos.jsp"
redirect="false"
contextRelative="false">
</forward>
</action>
<action
attribute="incluirCursoForm"
input = "/pages/incluircurso.jsp"
name="incluirCursoForm"
path="/incluirNovoCurso"
scope="session"
unknown="false"
validate="true"
type="br.com.surson.negocio.SalvarNovoCursoAction">
<forward
name="success"
path="/pages/listacursos.jsp"
redirect="false"
contextRelative="false">
</forward>
</action>
<action
path="/incluirCurso"
scope="session"
unknown="false"
validate="true"
type="br.com.surson.negocio.IncluirCursoAction">
<forward
name="success"
path="/pages/incluircurso.jsp"
redirect="false"
contextRelative="false">
</forward>
</action>
<action path="/excluirCursos"
scope="session"
unknown="false"
validate="false"
type="br.com.surson.negocio.ExcluirCursosAction">
<forward
name="success"
path="/pages/listacursos.jsp"
redirect="false"
contextRelative="false">
</forward>
</action>
<action
path="/listaEventos"
scope="session"
unknown="false"
validate="false"
type="br.com.surson.negocio.ListaEventosAction">
<forward
name="success"
path="/pages/listaeventos.jsp"
redirect="false"
contextRelative="false">
</forward>
</action>
</action-mappings>
<!--
======================================== Message Resources Definitions -->
<message-resources parameter="MessageResources" />
</struts-config>