Tentando inciar com struts

Boa Tarde Amigos,

Comprei o livro struts em ação e tenho problemas na primeira aplicação que registra um usuário e redireciona para a página de sucesso ou de falha, mas o problema é que não está redirecionando quando acessa o register.do:

struts-config.xml

[code]<!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>

&lt;form-bean
        name=&quot;registerForm&quot;
        type=&quot;app.RegisterForm&quot;/&gt;
&lt;/form-beans&gt;

&lt;action-mappings&gt;
      
        &lt;action
        path=&quot;/register&quot;
        type=&quot;app.RegisterAction&quot;
        name=&quot;registerForm&quot;&gt;
        
        
        &lt;forward name=&quot;success&quot; path=&quot;/success.html&quot;/&gt;
        &lt;forward name=&quot;failure&quot; path=&quot;/failure.html&quot;/&gt;
        &lt;/action&gt;
    &lt;/action-mappings&gt;

</struts-config>
[/code]

register.jsp:

[code]<%@ taglib uri="WEB-INF/struts-html.tld" prefix="form"%>
<form:form action="register.do">
UserName:<form:text property="username"/>

enter password:<form:password property="password1"/>

RE-ENTER PASSWORD<form:password property="password2"/>

<form:submit value="register"/>
</form:form>
[/code]

RegisterAction.java:

[code]/*

  • Created on 25/11/2004
  • TODO To change the template for this generated file go to
  • Window - Preferences - Java - Code Style - Code Templates
    */
    package app;

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;

/**

  • @author Ademir

  • TODO To change the template for this generated type comment go to Window -

  • Preferences - Java - Code Style - Code Templates
    */
    public class RegisterAction extends Action {

    public ActionForward perform(ActionMapping mapping, ActionForm form,
    HttpServletRequest req, HttpServletResponse res) {
    RegisterForm rf = (RegisterForm) form;

     String username = rf.getUsername&#40;&#41;;
     String password1 = rf.getPassword1&#40;&#41;;
     String password2 = rf.getPassword2&#40;&#41;;
    
     if &#40;password1.equals&#40;password2&#41;&#41; &#123;
    
     	UserDirectory.getInstance&#40;&#41;.setUser&#40;username, password1&#41;;
     	return mapping.findForward&#40;&quot;sucess&quot;&#41;;
     &#125;
    
     return mapping.findForward&#40;&quot;failure&quot;&#41;;
    

    }

}
[/code]
RegisterForm.java:

[code]package app;

import org.apache.struts.action.ActionForm;

/*

  • Created on 25/11/2004
  • TODO To change the template for this generated file go to
  • Window - Preferences - Java - Code Style - Code Templates
    */

/**

  • @author Ademir

  • TODO To change the template for this generated type comment go to Window -

  • Preferences - Java - Code Style - Code Templates
    */
    public class RegisterForm extends ActionForm {

    protected String username;

    protected String password1;

    protected String password2;

    public String getUsername() {
    return this.username;
    }

    public String getPassword1() {
    return this.password1;
    }

    public String getPassword2() {
    return this.password2;
    }

    public void setUsername(String username) {
    this.username = username;
    }

    public void setPassword1(String password) {
    this.password1 = password;
    }

    public void setPassword2(String password) {
    this.password2 = password;

    }
    }

[/code]
UserDirectory.java:

[code]/*

  • Created on 25/11/2004
  • TODO To change the template for this generated file go to
  • Window - Preferences - Java - Code Style - Code Templates
    */
    package app;

/**

  • @author Ademir

  • TODO To change the template for this generated type comment go to Window -

  • Preferences - Java - Code Style - Code Templates
    */
    public class UserDirectory {

    private static UserDirectory instance;

    private String username;

    private String password;

    public void setUser(String username, String password) {
    instance.username = username;
    instance.password = password;
    }

    public static UserDirectory getInstance() {
    if (instance == null) {
    instance = new UserDirectory();

     &#125;
    
     return instance;
    

    }

}

[/code]

[quote=“incomplete”]Boa Tarde Amigos,
RegisterAction.java:

[code]/*

  • Created on 25/11/2004
  • TODO To change the template for this generated file go to
  • Window - Preferences - Java - Code Style - Code Templates
    */
    package app;

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;

/**

  • @author Ademir
  • TODO To change the template for this generated type comment go to Window -
  • Preferences - Java - Code Style - Code Templates
    */
    public class RegisterAction extends Action {

//Esse método não deveria se chamar execute() ???
public ActionForward perform(ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res) {
RegisterForm rf = (RegisterForm) form;

	String username = rf.getUsername&#40;&#41;;
	String password1 = rf.getPassword1&#40;&#41;;
	String password2 = rf.getPassword2&#40;&#41;;

	if &#40;password1.equals&#40;password2&#41;&#41; &#123;

		UserDirectory.getInstance&#40;&#41;.setUser&#40;username, password1&#41;;
		return mapping.findForward&#40;&quot;sucess&quot;&#41;;
	&#125;

	return mapping.findForward&#40;&quot;failure&quot;&#41;;

&#125;

}
[/code]
[/quote]

Bem, acho que o erro está no nome do método desta classe acima…

A assinatura do método padrão do Struts é:

[code]public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{

//Seu código normal aqui.

}[/code]

Altera isso aí e veja se funciona…

té +