Dúvida com o livro Struts em Ação

2 respostas
marcusluiz83

Boa noite, amigos!

Tô acompanhando o livro Struts em Ação, e já tô apresentando problema no primeiro exemplo. É o seguinte, quando envio os dados do formulário, não aparece nem a página de erro e nem a de sucesso. Eis os arquivos: RegisterForm
package app;
import org.apache.struts.action.*;

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

}
RegisterAction
package app;

import org.apache.struts.action.*;
import javax.servlet.http.*;
import java.io.*;

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)) {
            try {


                UserDirectory.getInstance().setUser(username, password1);
                return mapping.findForward("sucess");
            } catch (UserDirectoryException e) {
                return mapping.findForward("failure");
            }
        }


        return mapping.findForward("failure");
    }
}
struts-config.xml
<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">


<struts-config>

    <form-beans>
        <form-bean name="registerForm" type="app.RegisterForm"/>
    </form-beans>

    <global-exceptions>

    </global-exceptions>

    <global-forwards>
        <forward name="welcome"  path="/Welcome.do"/>
    </global-forwards>

    <action-mappings>
        <action path="/Welcome" forward="/welcomeStruts.jsp"/>
        <action path="/register" type="app.RegisterAction" name="registerForm">
            <forward name="sucess" path="/sucess.html"/>
            <forward name="failure" path="/failure.html"/>
        </action>
    </action-mappings>

    <controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>

    <message-resources parameter="com/myapp/struts/ApplicationResource"/>

    <!-- ========================= Tiles plugin ===============================-->
    <!--
    This plugin initialize Tiles definition factory. This later can takes some
    parameters explained here after. The plugin first read parameters from
    web.xml, thenoverload them with parameters defined here. All parameters
    are optional.
    The plugin should be declared in each struts-config file.
    - definitions-config: (optional)
    Specify configuration file names. There can be several comma
    separated file names (default: ?? )
    - moduleAware: (optional - struts1.1)
    Specify if the Tiles definition factory is module aware. If true
    (default), there will be one factory for each Struts module.
    If false, there will be one common factory for all module. In this
    later case, it is still needed to declare one plugin per module.
    The factory will be initialized with parameters found in the first
    initialized plugin (generally the one associated with the default
    module).
    true : One factory per module. (default)
    false : one single shared factory for all modules
    - definitions-parser-validate: (optional)
    Specify if xml parser should validate the Tiles configuration file.
    true : validate. DTD should be specified in file header (default)
    false : no validation

    Paths found in Tiles definitions are relative to the main context.
    -->
    <plug-in className="org.apache.struts.tiles.TilesPlugin" >
        <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />
        <set-property property="moduleAware" value="true" />
    </plug-in>

    <!-- ========================= Validator plugin ================================= -->
    <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
        <set-property
            property="pathnames"
            value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
    </plug-in>

</struts-config>
register.jsp
<%@ 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>

Os únicos erros que aparecem para mim no NetBeans, são nas linhas 22 e 24 na classe RegisterAction, com UserDirectory. Não sei se realmente este é o único erro, portanto, não sei como corrigir este problema. Espero que possam me ajudar.

Um abraço.

2 Respostas

Andre_Fonseca

oi,

após uma “googliada” achei isto veja se resolve

abs

marcusluiz83

Cara, não tenho como testar agora, mas com certeza este era o problema. O NetBeans sugeriu a criação desta classe, mas não saberia como faze-la.

Muito obrigado!

Criado 11 de abril de 2010
Ultima resposta 12 de abr. de 2010
Respostas 2
Participantes 2