Tentando inciar com Struts

2 respostas
aconstantino

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
<?xml version="1.0" encoding="ISO-8859-1" ?>

&lt;!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"&gt;


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

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

&lt;/struts-config&gt;

[code]

 register.jsp:

[code]

&lt;%@ taglib uri="WEB-INF/struts-html.tld" prefix="form"%&gt;
&lt;form:form action="register.do"&gt;
 UserName:&lt;form:text property="username"/&gt;<br>
 enter password:&lt;form:password property="password1"/&gt;<br>
 RE-ENTER PASSWORD&lt;form:password property="password2"/&gt;<br>
 &lt;form:submit value="register"/&gt;
 &lt;/form:form&gt;

 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;
	}

}

:(

2 Respostas

C

Na sua action (RegisterAction) muda o nome do método perform para execute. O struts, por default, sempre chama o método execute para uma Action. Se você quiser especificar outro nome de método para o struts executar, da uma estudada na classe DispatchAction.

aconstantino

valeu cara funcionou!!! acho que algo deve ter mudado no struts desde a versão que é exemplificada no livro!

Criado 26 de novembro de 2004
Ultima resposta 2 de dez. de 2004
Respostas 2
Participantes 2