Tentando inciar com struts

1 resposta
I

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

<!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="registerForm"
            type="app.RegisterForm"/>
    </form-beans>

    <action-mappings>
          
            <action
            path="/register"
            type="app.RegisterAction"
            name="registerForm">
            
            
            <forward name="success" path="/success.html"/>
            <forward name="failure" path="/failure.html"/>
            </action>
        </action-mappings>

</struts-config>

register.jsp:

<%@ 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>
RegisterAction.java:
/*
 * 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();
		String password1 = rf.getPassword1();
		String password2 = rf.getPassword2();

		if (password1.equals(password2)) {

			UserDirectory.getInstance().setUser(username, password1);
			return mapping.findForward("sucess");
		}

		return mapping.findForward("failure");

	}

}
RegisterForm.java:
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;

	}
}
UserDirectory.java:
/*
 * 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();

		}

		return instance;
	}

}

1 Resposta

F
"incomplete":
Boa Tarde Amigos, RegisterAction.java:
/*
 * 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();
		String password1 = rf.getPassword1();
		String password2 = rf.getPassword2();

		if (password1.equals(password2)) {

			UserDirectory.getInstance().setUser(username, password1);
			return mapping.findForward("sucess");
		}

		return mapping.findForward("failure");

	}

}

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

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

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

//Seu código normal aqui.

}

Altera isso aí e veja se funciona...

té +

Criado 27 de novembro de 2004
Ultima resposta 16 de dez. de 2004
Respostas 1
Participantes 2