Problemas Básicos com Struts

2 respostas
S

Pessoal, gostaria que alguém me ajudasse a configurar o struts...
Tenho os seguintes arquivos:

- RegisterForm (bean para o formulario de login)

package app;

import org.apache.struts.action.*;

public class RegisterForm extends ActionForm {
    protected String username;
    protected String password1;
    protected String password2;

    //getters e setters

}

- RegisterAction (valida o login)
ignorem a classe UserDirectory...

package app;

import org.apache.struts.action.*;

import javax.servlet.http.*;

public class RegisterAction extends Action {
    public ActionForward perform (ActionMapping mapping, ActionForm form,
                                  HttpServletRequest request, HttpServletResponse response){
        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("success");
            } catch (UserDirectoryException e) {
                return mapping.findForward("failure");
            }
        }
        return mapping.findForward("failure");
    }
}

- struts-config.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.0//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_0.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>

- web.xml (naum coloquei nada)

- register.jsp

<%@ page language="java" %>
<%@taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<html>
<body>
<html:form action="register.do">
    Username: <html:text property="username"/><br>
    Pass1: <html:password property="password1"/><br>
    pass2: <html:password property="password2"/><br>
    <html:submit value="register"/>

</html:form>
</body>
</html>

- failure.html e success.html (naum interessa muito)

O q tem de errado??? tem q fazer alguma coisa no web.xml???+

2 Respostas

W

no web.xml e necessario mapear o controller do struts

veja abaixo como seria

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
  

<!-- SERVLET CONTROLLER DO STRUTS -->
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>2</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
  </servlet>

<!-- NOME DOS ACTION: XXX.do -->
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

<!-- TAG DO STRUTS -->
  <taglib>
    <taglib-uri>/WEB-INF/struts-form.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-form.tld</taglib-location>
  </taglib>
  <taglib>
    <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
  </taglib>
  <taglib>
    <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
  </taglib>
  <taglib>
    <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
  </taglib>
  <taglib>
    <taglib-uri>/WEB-INF/struts-template.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-template.tld</taglib-location>
  </taglib>
  <taglib>
    <taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
  </taglib>
  <taglib>
    <taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
  </taglib>

</web-app>
S

Cara porque as Tag não são reconhecidas pelo IDEA, se eu jah coloquei os jars no classpath???

Criado 12 de fevereiro de 2005
Ultima resposta 12 de fev. de 2005
Respostas 2
Participantes 2