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" ?>
<!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>
[code]
register.jsp:
[code]
<%@ taglib uri="WEB-INF/struts-html.tld" prefix="form"%>
<form:form action="register.do">
UserName:<form:text property="username"/><br>
enter password:<form:password property="password1"/><br>
RE-ENTER PASSWORD<form:password property="password2"/><br>
<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");
}
}
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;
}
}
/*
* 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;
}
}
:(