Results 1 to 4 of 4
  1. #1
    Tequila_Burp is offline Member
    Join Date
    Sep 2008
    Posts
    6
    Rep Power
    0

    Default Struts: Reuse data across multiple ActionForms problem

    Hi

    I tried to put in practice these best practices about struts from here: *removed*

    but am having problems with the first one. Since am using the same data in many StrutsActionForms i decided to put them all in a javabean and associate it with the action forms. However, my application is not saving the data when i submit from a jsp. I debbuged it and every attribute from the bean is empty:(

    here is the important code:

    The Java Bean
    Java Code:
    public class PostulanteBean {
    
        private String _nombre;
        private String _apellido_paterno;
        private String _apellido_materno;
        private _Tipo_documento _tipo_documento;
        private int _numero_documento;
        private _Sexo _sexo;
        private String _correo;
        private String _curso;
        private String _area;
        private String _datos_personales;
        private String _informacion_domiciliaria;
        private String _estudios_escolares;
        private String _estudios_postgrado;
        private String _experiencia_profesional;
        private String _experiencia_docente;
        private String _referencia_profesionales;
    
        public enum _Sexo {
    
            Masculino,
            Femenino;
        }
    
        private enum _Tipo_documento {
            tipo1,
            tipo2,
            tipo3;
        }
    
        public String getSexo() {
            if (_sexo == null) { return ""; } else return _sexo.toString();
        }
    
        public void setSexo(String _sexo) {
            if (_sexo.equals("Masculino")) { this._sexo = _Sexo.Masculino; }
            if (_sexo.equals("Femenino")) { this._sexo = _Sexo.Femenino; }
            throw new IllegalArgumentException();
        }
        
        public String getTipo_documento() {
            if (_tipo_documento == null) { return ""; } else return _tipo_documento.toString();
        }
    
        public void setTipo_documento(String _sexo) {
            if (_tipo_documento.equals("tipo1")) { this._tipo_documento = _Tipo_documento.tipo1; }
            if (_tipo_documento.equals("tipo2")) { this._tipo_documento = _Tipo_documento.tipo2; }
            if (_tipo_documento.equals("tipo3")) { this._tipo_documento = _Tipo_documento.tipo3; }
            throw new IllegalArgumentException();
        }
    
        public String getApellido_materno() {
            return _apellido_materno;
        }
    
        public void setApellido_materno(String _apellido_materno) {
            this._apellido_materno = _apellido_materno;
        }
    
        public String getApellido_paterno() {
            return _apellido_paterno;
        }
    
        public void setApellido_paterno(String _apellido_paterno) {
            this._apellido_paterno = _apellido_paterno;
        }
    
        public String getArea() {
            return _area;
        }
    
        public void setArea(String _area) {
            this._area = _area;
        }
    
        public String getCorreo() {
            return _correo;
        }
    
        public void setCorreo(String _correo) {
            this._correo = _correo;
        }
    
        public String getCurso() {
            return _curso;
        }
    
        public void setCurso(String _curso) {
            this._curso = _curso;
        }
    
        public String getNombre() {
            return _nombre;
        }
    
        public void setNombre(String _nombre) {
            this._nombre = _nombre;
        }
    
        public int getNumero_documento() {
            return _numero_documento;
        }
    
        public void setNumero_documento(int _numero_documento) {
            this._numero_documento = _numero_documento;
        }
        
        public String getDatos_personales() {
            return _datos_personales;
        }
    
        public void setDatos_personales(String _datos_personales) {
            this._datos_personales = _datos_personales;
        }
    
        public String getEstudios_escolares() {
            return _estudios_escolares;
        }
    
        public void setEstudios_escolares(String _estudios_escolares) {
            this._estudios_escolares = _estudios_escolares;
        }
    
        public String getEstudios_postgrado() {
            return _estudios_postgrado;
        }
    
        public void setEstudios_postgrado(String _estudios_postgrado) {
            this._estudios_postgrado = _estudios_postgrado;
        }
    
        public String getExperiencia_docente() {
            return _experiencia_docente;
        }
    
        public void setExperiencia_docente(String _experiencia_docente) {
            this._experiencia_docente = _experiencia_docente;
        }
    
        public String getExperiencia_profesional() {
            return _experiencia_profesional;
        }
    
        public void setExperiencia_profesional(String _experiencia_profesional) {
            this._experiencia_profesional = _experiencia_profesional;
        }
    
        public String getInformacion_domiciliaria() {
            return _informacion_domiciliaria;
        }
    
        public void setInformacion_domiciliaria(String _informacion_domiciliaria) {
            this._informacion_domiciliaria = _informacion_domiciliaria;
        }
    
        public String getReferencia_profesionales() {
            return _referencia_profesionales;
        }
    
        public void setReferencia_profesionales(String _referencia_profesionales) {
            this._referencia_profesionales = _referencia_profesionales;
        }
    }
    ActionForm:
    Java Code:
    public class PostulanteActionForm extends org.apache.struts.action.ActionForm {
        
       private PostulanteBean _postulante;
    
        public PostulanteBean get_postulante() {
            return _postulante;
        }
    
        public void set_postulante(PostulanteBean _postulante) {
            this._postulante = _postulante;
        }
    
       /**
        *
        */
       public PostulanteActionForm() {
           super();
           // TODO Auto-generated constructor stub
           this._postulante= new PostulanteBean();
       }
       
       @Override
       public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
           ActionErrors errors = new ActionErrors();
           if (_postulante.getNombre() == null || _postulante.getNombre().length() < 1) {
               errors.add("nombre", new ActionMessage("error.nombre.required"));
           }
           if (_postulante.getApellido_paterno() == null || _postulante.getApellido_paterno().length() < 1) {
               errors.add("apellido_paterno", new ActionMessage("error.apellido_paterno.required"));
           }
           if (_postulante.getApellido_materno() == null || _postulante.getApellido_materno().length() < 1) {
               errors.add("apellido_materno", new ActionMessage("error.apellido_materno.required"));
           }
           if (_postulante.getArea() == null || _postulante.getArea().length() < 1) {
               errors.add("area", new ActionMessage("error.area.required"));
           } 
           return errors;
       }
    }
    The action:
    Java Code:
    public class PostulanteAction extends org.apache.struts.action.Action {
        
        /* forward name="success" path="" */
        private final static String SUCCESS = "success";
        private final static String CANCEL = "cancel";
        
        /**
         * This is the action called from the Struts framework.
         * @param mapping The ActionMapping used to select this instance.
         * @param form The optional ActionForm bean for this request.
         * @param request The HTTP Request we are processing.
         * @param response The HTTP Response we are processing.
         * @throws java.lang.Exception
         * @return
         */
        
        @Override
        public ActionForward execute(ActionMapping mapping, ActionForm  form,
                HttpServletRequest request, HttpServletResponse response)
                throws Exception {
            if (isCancelled(request)) {
                return mapping.findForward(CANCEL);
            }
            PostulanteBean bean = new PostulanteBean();
            bean.setApellido_materno((String)request.getParameter("apellido_materno"));
            bean.setApellido_paterno((String)request.getParameter("apellido_paterno"));
            bean.setArea((String)request.getParameter("area"));
            bean.setCorreo((String)request.getParameter("correo"));
            bean.setCurso((String)request.getParameter("curso"));
            bean.setNombre((String)request.getParameter("nombre"));
            bean.setNumero_documento(Integer.parseInt(request.getParameter("numero_documento")));
            bean.setSexo((String)request.getParameter("sexo"));
            bean.setTipo_documento((String)request.getParameter("tipo_documento"));
            
            // Aqui se hace lo q sea con los datos
            
            return mapping.findForward(SUCCESS);
        }
    All the data i want to save is empty in the action:(

    My struts config
    Java Code:
    <?xml version="1.0" encoding="UTF-8" ?>
    
    <!DOCTYPE struts-config PUBLIC
              "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
              "*removed*">
    <struts-config>
        <form-beans>
            <form-bean name="AprobacionActionForm" type="com.upc.struts.AprobacionActionForm"/>
            <form-bean name="ActualizarActionForm" type="com.upc.struts.ActualizarActionForm"/>
            <form-bean name="RegistrarActionForm" type="com.upc.struts.RegistrarActionForm"/>
            <form-bean name="PostulanteActionForm" type="com.upc.struts.PostulanteActionForm"/>
        </form-beans>
        <global-exceptions>
        </global-exceptions>
        <global-forwards>
            <forward name="formulario"  path="/formulario"/>
        </global-forwards>
        <action-mappings>
            <action input="/form1.jsp" name="PostulanteActionForm" path="/convocar" scope="session" type="com.upc.struts.PostulanteAction">
                <forward name="success" path="/success.jsp"/>
                <forward name="cancel" path="/cancelar.jsp"/>
                <set-property property="cancellable" value="true"/>
            </action>
            <action input="/form2.jsp" name="RegistrarActionForm" path="/registrar" scope="session" type="com.upc.struts.RegistrarAction">
                <forward name="success" path="/success.jsp"/>
                <forward name="cancel" path="/cancelar.jsp"/>
                <set-property property="cancellable" value="true"/>
            </action>
            <action input="/form3.jsp" name="ActualizarActionForm" path="/actualizar" scope="session" type="com.upc.struts.ActualizarAction">
                <forward name="success" path="/success.jsp"/>
                <forward name="cancel" path="/cancelar.jsp"/>
                <set-property property="cancellable" value="true"/>
            </action>
            <action input="/form4.jsp" name="AprobacionActionForm" path="/aprobacion" scope="session" type="com.upc.struts.AprobacionAction"/>
            <action path="/formulario" forward="/formulario.jsp"/>
        </action-mappings>
        <controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>
        <message-resources parameter="com/upc/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="*removed*" />
            <set-property property="moduleAware" value="true" />
        </plug-in>
        
        <!-- ========================= Validator plugin ================================= -->
        <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
            <set-property
                property="pathnames"
                value="/*removed*"/>
        </plug-in>
    </struts-config>
    The JSP

    Java Code:
    <%@ taglib uri="*removed*" prefix="bean" %>
    <%@ taglib uri="*removed*" prefix="html" %>
    
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "*removed*">
    
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>Convocar Postulante JSP</title>
        </head>
        <body>
            <h2>Convocar Postulante</h2>
            <html:form action="convocar">
                <bean:define name="PostulanteActionForm" property="_postulante" id="postulante"
                    type="com.upc.bean.PostulanteBean" />
                <table border="1">
    
                    <tbody>
                        <tr>
                            <td><bean:message key="form.area" /></td>
                            <td><html:text name="postulante" property="area" /></td>
                        </tr>
                        <tr>
                            <td><bean:message key="form.nombre" /></td>
                            <td><html:text name="postulante" property="nombre" /></td>
                        </tr>
                        <tr>
                            <td><bean:message key="form.apellido_paterno" /></td>
                            <td><html:text name="postulante" property="apellido_paterno" /></td>
                        </tr>
                        <tr>
                            <td><bean:message key="form.apellido_materno" /></td>
                            <td><html:text name="postulante" property="apellido_materno" /></td>
                        </tr>
                    </tbody>
                </table>
                <html:submit value="Convocar" />
                <html:reset />
                <html:cancel />
                <html:errors />
            </html:form>            
        </body>
    </html>
    I removed all links because this is my first post :p
    Why the setter is not working. :( help!

  2. #2
    nielisheng is offline Member
    Join Date
    Oct 2008
    Posts
    6
    Rep Power
    0

    Default

    The code above is very good.

  3. #3
    Tequila_Burp is offline Member
    Join Date
    Sep 2008
    Posts
    6
    Rep Power
    0

    Default

    pc thinks its not good =p
    i get an empty bean :/

    i moved on, now am developing with Struts Portlet in liferay =p

  4. #4
    boumbh is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default Re: Struts: Reuse data across multiple ActionForms problem

    Hi

    I had the same problem and I was about to give up when I managed to work it around by making changes in the JSP.

    In the JSP, do not call bean:define and then use "dot notation" to identify the properties. Example:

    Java Code:
    <html:text property="beanName.attributeName" />
    In your case it would be something like:
    Java Code:
            <html:form action="convocar">
    
                <table border="1">
    
                    <tbody>
                        <tr>
                            <td><bean:message key="form.nombre" /></td>
                            <td><html:text property="_postulante.nombre" /></td>
                        </tr>
                        <tr>
                            <td><bean:message key="form.apellido_paterno" /></td>
                            <td><html:text property="_postulante.apellido_paterno" /></td>
                        </tr>
                        <tr>
                            <td><bean:message key="form.apellido_materno" /></td>
                            <td><html:text property="_postulante.apellido_materno" /></td>
                        </tr>
                    </tbody>
                </table>
                <html:submit value="Convocar" />
                <html:reset />
                <html:cancel />
                <html:errors />
            </html:form>
    I didnít test it with the underscores in the attributes, I personally prefer to follow the naming conventions when possible.

    Thatís strange, nobody ever used struts 1 for that even best practices does not work?

    *Note: I shortened some of your code in the quote below.
    Quote Originally Posted by Tequila_Burp View Post
    Hi

    I tried to put in practice these best practices about struts from here: *removed*

    but am having problems with the first one. Since am using the same data in many StrutsActionForms i decided to put them all in a javabean and associate it with the action forms. However, my application is not saving the data when i submit from a jsp. I debbuged it and every attribute from the bean is empty:(

    here is the important code:

    The Java Bean
    Java Code:
    public class PostulanteBean {
    
        private String _nombre;
        private String _apellido_paterno;
        private String _apellido_materno;
        private _Sexo _sexo;
    
        public enum _Sexo {
    
            Masculino,
            Femenino;
        }
    
        public String getSexo() {
            if (_sexo == null) { return ""; } else return _sexo.toString();
        }
    
        public void setSexo(String _sexo) {
            if (_sexo.equals("Masculino")) { this._sexo = _Sexo.Masculino; }
            if (_sexo.equals("Femenino")) { this._sexo = _Sexo.Femenino; }
            throw new IllegalArgumentException();
        }
        
        public String getApellido_materno() {
            return _apellido_materno;
        }
    
        public void setApellido_materno(String _apellido_materno) {
            this._apellido_materno = _apellido_materno;
        }
    
        public String getApellido_paterno() {
            return _apellido_paterno;
        }
    
        public void setApellido_paterno(String _apellido_paterno) {
            this._apellido_paterno = _apellido_paterno;
        }
    
        public String getNombre() {
            return _nombre;
        }
    
        public void setNombre(String _nombre) {
            this._nombre = _nombre;
        }
    }
    ActionForm:
    Java Code:
    public class PostulanteActionForm extends org.apache.struts.action.ActionForm {
        
       private PostulanteBean _postulante;
    
        public PostulanteBean get_postulante() {
            return _postulante;
        }
    
        public void set_postulante(PostulanteBean _postulante) {
            this._postulante = _postulante;
        }
    
       /**
        *
        */
       public PostulanteActionForm() {
           super();
           // TODO Auto-generated constructor stub
           this._postulante= new PostulanteBean();
       }
       
       @Override
       public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
           ActionErrors errors = new ActionErrors();
           if (_postulante.getNombre() == null || _postulante.getNombre().length() < 1) {
               errors.add("nombre", new ActionMessage("error.nombre.required"));
           }
           if (_postulante.getApellido_paterno() == null || _postulante.getApellido_paterno().length() < 1) {
               errors.add("apellido_paterno", new ActionMessage("error.apellido_paterno.required"));
           }
           if (_postulante.getApellido_materno() == null || _postulante.getApellido_materno().length() < 1) {
               errors.add("apellido_materno", new ActionMessage("error.apellido_materno.required"));
           }
           return errors;
       }
    }
    The action:
    Java Code:
    public class PostulanteAction extends org.apache.struts.action.Action {
        
        /* forward name="success" path="" */
        private final static String SUCCESS = "success";
        private final static String CANCEL = "cancel";
        
        /**
         * This is the action called from the Struts framework.
         * @param mapping The ActionMapping used to select this instance.
         * @param form The optional ActionForm bean for this request.
         * @param request The HTTP Request we are processing.
         * @param response The HTTP Response we are processing.
         * @throws java.lang.Exception
         * @return
         */
        
        @Override
        public ActionForward execute(ActionMapping mapping, ActionForm  form,
                HttpServletRequest request, HttpServletResponse response)
                throws Exception {
            if (isCancelled(request)) {
                return mapping.findForward(CANCEL);
            }
            PostulanteBean bean = new PostulanteBean();
            bean.setApellido_materno((String)request.getParameter("apellido_materno"));
            bean.setApellido_paterno((String)request.getParameter("apellido_paterno"));
            bean.setNombre((String)request.getParameter("nombre"));
            bean.setSexo((String)request.getParameter("sexo"));
            
            // Aqui se hace lo q sea con los datos
            
            return mapping.findForward(SUCCESS);
        }
    All the data i want to save is empty in the action:(

    My struts config
    Java Code:
    <?xml version="1.0" encoding="UTF-8" ?>
    
    <!DOCTYPE struts-config PUBLIC
              "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
              "*removed*">
    <struts-config>
        <form-beans>
            <form-bean name="PostulanteActionForm" type="com.upc.struts.PostulanteActionForm"/>
        </form-beans>
        <global-exceptions>
        </global-exceptions>
        <global-forwards>
        </global-forwards>
        <action-mappings>
            <action input="/form1.jsp" name="PostulanteActionForm" path="/convocar" scope="session" type="com.upc.struts.PostulanteAction">
                <forward name="success" path="/success.jsp"/>
                <forward name="cancel" path="/cancelar.jsp"/>
                <set-property property="cancellable" value="true"/>
            </action>
        </action-mappings>
        <controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>
        <message-resources parameter="com/upc/struts/ApplicationResource"/>    
        
        <!-- ========================= Tiles plugin ===============================-->
        <plug-in className="org.apache.struts.tiles.TilesPlugin" >
            <set-property property="definitions-config" value="*removed*" />
            <set-property property="moduleAware" value="true" />
        </plug-in>
        
        <!-- ========================= Validator plugin ================================= -->
        <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
            <set-property
                property="pathnames"
                value="/*removed*"/>
        </plug-in>
    </struts-config>
    The JSP

    Java Code:
    <%@ taglib uri="*removed*" prefix="bean" %>
    <%@ taglib uri="*removed*" prefix="html" %>
    
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "*removed*">
    
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>Convocar Postulante JSP</title>
        </head>
        <body>
            <h2>Convocar Postulante</h2>
            <html:form action="convocar">
                <bean:define name="PostulanteActionForm" property="_postulante" id="postulante"
                    type="com.upc.bean.PostulanteBean" />
                <table border="1">
    
                    <tbody>
                        <tr>
                            <td><bean:message key="form.nombre" /></td>
                            <td><html:text name="postulante" property="nombre" /></td>
                        </tr>
                        <tr>
                            <td><bean:message key="form.apellido_paterno" /></td>
                            <td><html:text name="postulante" property="apellido_paterno" /></td>
                        </tr>
                        <tr>
                            <td><bean:message key="form.apellido_materno" /></td>
                            <td><html:text name="postulante" property="apellido_materno" /></td>
                        </tr>
                    </tbody>
                </table>
                <html:submit value="Convocar" />
                <html:reset />
                <html:cancel />
                <html:errors />
            </html:form>            
        </body>
    </html>
    I removed all links because this is my first post :p
    Why the setter is not working. :( help!

Similar Threads

  1. enctype=multipart/form-data with form data in struts
    By vk_satheesh in forum New To Java
    Replies: 0
    Last Post: 09-19-2008, 12:48 PM
  2. Data Leakage in Struts 2
    By arun3m in forum Web Frameworks
    Replies: 0
    Last Post: 07-26-2008, 07:54 PM
  3. Replies: 7
    Last Post: 07-09-2008, 12:06 AM
  4. Replies: 0
    Last Post: 04-07-2008, 07:54 PM
  5. Struts problem
    By ziniestro in forum Web Frameworks
    Replies: 2
    Last Post: 05-10-2007, 05:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •