IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Struts 1 Java Discussion :

validation en struts


Sujet :

Struts 1 Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Inscrit en
    Mars 2007
    Messages
    100
    Détails du profil
    Informations personnelles :
    Âge : 44

    Informations forums :
    Inscription : Mars 2007
    Messages : 100
    Par défaut validation en struts
    Bonjour à tous j'ai un petit problème que j'arrive à le résoudre puisque je suis débutant en struts.

    j'ai un tableau qui affiche une liste à la fin de chaque ligne il y a un lien qui affiche un formulaire de modification.
    tout marche très bien. le formulaire de modification est initialisé par des données et l'utilisateur dois les modifier mais j'ai un problème avec la validation du champs mail.
    voici la trace d'erreur

    GRAVE: "Servlet.service()" pour la servlet jsp a lanc� une exception
    javax.servlet.jsp.JspException: Cannot find bean user in any scope
    at org.apache.struts.taglib.TagUtils.lookup(TagUtils.java:934)
    at org.apache.struts.taglib.html.BaseFieldTag.prepareValue(BaseFieldTag.java:121)
    at org.apache.struts.taglib.html.BaseFieldTag.renderInputElement(BaseFieldTag.java:102)
    at org.apache.struts.taglib.html.BaseFieldTag.doStartTag(BaseFieldTag.java:81)
    at org.apache.jsp.UpdateUser_jsp._jspx_meth_html_005ftext_005f0(UpdateUser_jsp.java:270)
    at org.apache.jsp.UpdateUser_jsp._jspx_meth_html_005fform_005f0(UpdateUser_jsp.java:203)
    at org.apache.jsp.UpdateUser_jsp._jspx_meth_html_005fhtml_005f0(UpdateUser_jsp.java:129)
    at org.apache.jsp.UpdateUser_jsp._jspService(UpdateUser_jsp.java:84)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:630)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
    at org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1063)
    at org.apache.struts.action.RequestProcessor.internalModuleRelativeForward(RequestProcessor.java:1001)
    at org.apache.struts.action.RequestProcessor.processValidate(RequestProcessor.java:966)
    at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:204)
    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
    at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Thread.java:595)
    14 août 2008 09:49:39 org.apache.catalina.core.StandardWrapperValve invoke.

    Voici la jsp qui affiche la liste
    <CODE>
    <%@ page language="java" pageEncoding="ISO-8859-1"%>
    <%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
    <%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
    <%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
    <%@taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Listing all users</title>
    </head>

    <body>
    <center><h3>User List</h3></center>
    <html:errors/>

    <logic:present name="list">
    <table border="0" cellspacing="0" cellpadding="0" align="center" width="70%" style="border-collapse:collapse;">
    <tr bgcolor="#98AFCC">
    <th>User ID</th>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Email</th>
    </tr>
    <%boolean even = false; %>
    <logic:iterate id="user" name="list" indexId="userId">
    <% even = !even; %>
    <tr bgcolor="<%=even?"#B7D3F5":"#D6E0F5" %>">
    <td>
    <bean:write name="user" property="userId" />
    </td>
    <td>
    <bean:write name="user" property="firstName" />
    </td>
    <td>
    <bean:write name="user" property="lastName" />
    </td>
    <td>
    <bean:write name="user" property="email" />
    </td>
    <td>
    <html:link href="ForwordUserToModify.do" paramId="userId" paramName="user" paramProperty="userId" >Modify</html:link>
    </td>
    </tr>
    </logic:iterate>
    <tr>
    <td colspan="6" align="center">
    <a href="adduser.jsp">Add New User</a>
    </td>
    </tr>
    </table>
    </logic:present>

    </body>
    </html>

    </CODE>

    Voici l'action du lien qui remplis la formulaire
    <CODE>
    package com.struts.action;

    import java.sql.SQLException;
    import java.util.LinkedList;
    import java.util.List;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import org.hibernate.HibernateException;

    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.action.ActionMessage;
    import org.apache.struts.action.ActionMessages;

    import com.struts.beans.User;
    import com.struts.db.UserManager;



    public class ForwardUserFromId extends Action {




    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
    //Get a list of users and save to the request
    String userid = request.getParameter("userId");
    User usr = new User();
    // List list = new LinkedList();
    try {
    usr= UserManager.getInstance().getUser(userid);// récupération du Bean User àpartir de l'id
    //list.add(usr);
    request.setAttribute("user", usr);System.out.print("User intercepted success");
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    request.setAttribute("User", usr);
    ActionMessages errors = new ActionMessages();
    ActionMessage error = new ActionMessage("error.generic",e.getMessage());
    errors.add("error",error);
    saveErrors(request, errors);
    e.printStackTrace();
    }
    return mapping.findForward("success");
    }
    }
    </CODE>

    et puis voici la jsp du formulaire

    <CODE>
    <%@ page language="java" pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
    <%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
    <%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html:html lang="true">
    <head>
    <html:base />
    <title>Modify user</title>
    </head>
    <body>
    <center><h3>Modify User</h3></center>
    <CENTER><html:errors/></CENTER>

    <html:form action="/Updateuser" method="post">

    <table border="0" align="center">
    <tr>
    <td>User ID:</td>
    <td><html:text name="user" property="userId" /></td>
    </tr>
    <tr>
    <td>First Name:</td>
    <td><html:text name="user" property="firstName" /></td>
    </tr>
    <tr>
    <td>Last Name:</td>
    <td><html:text name="user" property="lastName" /></td>
    </tr>
    <tr>
    <td>Age:</td>
    <td><html:text name="user" property="age" /></td>
    </tr>
    <tr>
    <td>Email:</td>
    <td><html:text name="user" property="email" /></td>
    </tr>
    <tr>
    <td colspan="2" align="center"><html:submit /></td>
    </tr>
    </table>

    <center> <a href="manageusers.do">Show users list </a></center>
    </html:form>


    </body>
    </html:html>

    </CODE>

    et voici le code de l'action de modification
    <CODE>
    package com.struts.action;
    import java.sql.SQLException;

    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;
    import org.apache.struts.action.ActionMessage;
    import org.apache.struts.action.ActionMessages;
    import com.struts.beans.User;
    import com.struts.db.UserManager;


    public class UpdateUser extends Action {


    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
    User user = (User) form;

    try {
    UserManager.getInstance().UpdateUser(user);
    } catch (SQLException e) {
    ActionMessages errors = new ActionMessages();
    ActionMessage error = new ActionMessage("error.generic",e.getMessage());
    errors.add("error",error);
    saveErrors(request, errors);
    e.printStackTrace();
    }
    return mapping.findForward("success");
    }
    }
    </CODE>

    et en fin voici le bean User

    <CODE>
    package com.struts.beans;

    import javax.servlet.http.HttpServletRequest;

    import org.apache.struts.action.ActionErrors;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.action.ActionMessage;


    public class User extends ActionForm{
    private Integer userId=0 ;
    private String firstName = "";
    private String lastName = "";
    private int age = 0;
    private String email = "";
    private String password = "";

    public User(Integer userId) {
    super();
    this.userId = userId;
    }
    public User(){
    super();
    }
    public int getAge() {
    return age;
    }
    public void setAge(int age) {
    this.age = age;
    }
    public String getEmail() {
    return email;
    }
    public void setEmail(String email) {
    this.email = email;
    }
    public String getFirstName() {
    return firstName;
    }
    public void setFirstName(String firstName) {
    this.firstName = firstName;
    }
    public String getLastName() {
    return lastName;
    }
    public void setLastName(String lastName) {
    this.lastName = lastName;
    }
    public Integer getUserId() {
    return userId;
    }
    public void setUserId(Integer userId) {
    this.userId = userId;
    }
    public String getPassword() {
    return password;
    }
    public void setPassword(String password) {
    this.password = password;
    }
    public ActionErrors validate(
    ActionMapping mapping, HttpServletRequest request ) {

    ActionErrors errors = new ActionErrors();
    if( getUserId().toString().length()<1 ) {
    errors.add("name",new ActionMessage("null.check","User ID"));
    }
    if(getEmail().length()>0)
    {
    //Yes! user entered email address. validate it
    if(!getEmail().matches("^[a-zA-Z][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$")){
    errors.add("name",new ActionMessage("email.invalid"));
    }
    }
    return errors;
    }
    }

    </CODE>


    je pense que le problème arrive du name="user" dans la jsp de modification aprés validation

  2. #2
    Membre confirmé
    Inscrit en
    Mars 2007
    Messages
    100
    Détails du profil
    Informations personnelles :
    Âge : 44

    Informations forums :
    Inscription : Mars 2007
    Messages : 100
    Par défaut
    --------------------------------------------------------------------------------
    Désolé comme ça c'est plus claire. je demande pardons

    Bonjour à tous j'ai un petit problème que j'arrive à le résoudre puisque je suis débutant en struts.

    j'ai un tableau qui affiche une liste à la fin de chaque ligne il y a un lien qui affiche un formulaire de modification.
    tout marche très bien. le formulaire de modification est initialisé par des données et l'utilisateur dois les modifier mais j'ai un problème avec la validation du champs mail.
    voici la trace d'erreur
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
     
    GRAVE: "Servlet.service()" pour la servlet jsp a lanc� une exception
    javax.servlet.jsp.JspException: Cannot find bean user in any scope
    at org.apache.struts.taglib.TagUtils.lookup(TagUtils.java:934)
    at org.apache.struts.taglib.html.BaseFieldTag.prepareValue(BaseFieldTag.java:121)
    at org.apache.struts.taglib.html.BaseFieldTag.renderInputElement(BaseFieldTag.java:102)
    at org.apache.struts.taglib.html.BaseFieldTag.doStartTag(BaseFieldTag.java:81)
    at org.apache.jsp.UpdateUser_jsp._jspx_meth_html_005ftext_005f0(UpdateUser_jsp.java:270)
    at org.apache.jsp.UpdateUser_jsp._jspx_meth_html_005fform_005f0(UpdateUser_jsp.java:203)
    at org.apache.jsp.UpdateUser_jsp._jspx_meth_html_005fhtml_005f0(UpdateUser_jsp.java:129)
    at org.apache.jsp.UpdateUser_jsp._jspService(UpdateUser_jsp.java:84)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:630)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
    at org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1063)
    at org.apache.struts.action.RequestProcessor.internalModuleRelativeForward(RequestProcessor.java:1001)
    at org.apache.struts.action.RequestProcessor.processValidate(RequestProcessor.java:966)
    at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:204)
    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
    at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Thread.java:595)
    14 août 2008 09:49:39 org.apache.catalina.core.StandardWrapperValve invoke.
    Voici la jsp qui affiche la liste
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
     
    <%@ page language="java" pageEncoding="ISO-8859-1"%>
    <%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
    <%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
    <%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
    <%@taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>
     
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Listing all users</title>
    </head>
     
    <body>
    <center><h3>User List</h3></center>
    <html:errors/>
     
    <logic:present name="list">
    <table border="0" cellspacing="0" cellpadding="0" align="center" width="70%" style="border-collapse:collapse;">
    <tr bgcolor="#98AFCC">
    <th>User ID</th>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Email</th> 
    </tr>
    <%boolean even = false; %>
    <logic:iterate id="user" name="list" indexId="userId">
    <% even = !even; %>
    <tr bgcolor="<%=even?"#B7D3F5":"#D6E0F5" %>">
    <td>
    <bean:write name="user" property="userId" />
    </td>
    <td>
    <bean:write name="user" property="firstName" />
    </td>
    <td>
    <bean:write name="user" property="lastName" />
    </td>
    <td>
    <bean:write name="user" property="email" />
    </td>
    <td>
    <html:link href="ForwordUserToModify.do" paramId="userId" paramName="user" paramProperty="userId" >Modify</html:link>
    </td>
    </tr>
    </logic:iterate>
    <tr>
    <td colspan="6" align="center">
    <a href="adduser.jsp">Add New User</a>
    </td>
    </tr>
    </table>
    </logic:present>
     
    </body>
    </html>
    Voici l'action du lien qui remplis la formulaire

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
     
    package com.struts.action;
     
    import java.sql.SQLException;
    import java.util.LinkedList;
    import java.util.List;
     
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
     
    import org.hibernate.HibernateException;
     
    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.action.ActionMessage;
    import org.apache.struts.action.ActionMessages;
     
    import com.struts.beans.User;
    import com.struts.db.UserManager;
     
     
     
    public class ForwardUserFromId extends Action {
     
     
     
     
    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
    //Get a list of users and save to the request
    String userid = request.getParameter("userId");
    User usr = new User();
    // List list = new LinkedList();
    try {
    usr= UserManager.getInstance().getUser(userid);// récupération du Bean User àpartir de l'id
    //list.add(usr);
    request.setAttribute("user", usr);System.out.print("User intercepted success");
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    request.setAttribute("User", usr);
    ActionMessages errors = new ActionMessages();
    ActionMessage error = new ActionMessage("error.generic",e.getMessage());
    errors.add("error",error);
    saveErrors(request, errors);
    e.printStackTrace();
    }
    return mapping.findForward("success");
    }
    }
    et puis voici la jsp du formulaire

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
     
    <%@ page language="java" pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
    <%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
    <%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html:html lang="true">
    <head>
    <html:base /> 
    <title>Modify user</title>
    </head> 
    <body>
    <center><h3>Modify User</h3></center>
    <CENTER><html:errors/></CENTER>
     
    <html:form action="/Updateuser" method="post">
     
    <table border="0" align="center">
    <tr>
    <td>User ID:</td>
    <td><html:text name="user" property="userId" /></td>
    </tr>
    <tr>
    <td>First Name:</td>
    <td><html:text name="user" property="firstName" /></td>
    </tr>
    <tr>
    <td>Last Name:</td>
    <td><html:text name="user" property="lastName" /></td>
    </tr>
    <tr>
    <td>Age:</td>
    <td><html:text name="user" property="age" /></td>
    </tr>
    <tr>
    <td>Email:</td>
    <td><html:text name="user" property="email" /></td>
    </tr>
    <tr>
    <td colspan="2" align="center"><html:submit /></td>
    </tr>
    </table>
     
    <center> <a href="manageusers.do">Show users list </a></center>
    </html:form>
     
     
    </body>
    </html:html>
    et voici le code de l'action de modification
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
     
    package com.struts.action;
    import java.sql.SQLException;
     
    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;
    import org.apache.struts.action.ActionMessage;
    import org.apache.struts.action.ActionMessages;
    import com.struts.beans.User;
    import com.struts.db.UserManager;
     
     
    public class UpdateUser extends Action {
     
     
    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
    User user = (User) form;
     
    try {
    UserManager.getInstance().UpdateUser(user);
    } catch (SQLException e) {
    ActionMessages errors = new ActionMessages();
    ActionMessage error = new ActionMessage("error.generic",e.getMessage());
    errors.add("error",error);
    saveErrors(request, errors);
    e.printStackTrace();
    }
    return mapping.findForward("success");
    }
    }
    et en fin voici le bean User

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
     
    package com.struts.beans;
     
    import javax.servlet.http.HttpServletRequest;
     
    import org.apache.struts.action.ActionErrors;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.action.ActionMessage;
     
     
    public class User extends ActionForm{
    private Integer userId=0 ;
    private String firstName = "";
    private String lastName = "";
    private int age = 0;
    private String email = "";
    private String password = "";
     
    public User(Integer userId) {
    super();
    this.userId = userId;
    }
    public User(){
    super();
    }
    public int getAge() {
    return age;
    }
    public void setAge(int age) {
    this.age = age;
    }
    public String getEmail() {
    return email;
    }
    public void setEmail(String email) {
    this.email = email;
    }
    public String getFirstName() {
    return firstName;
    }
    public void setFirstName(String firstName) {
    this.firstName = firstName;
    }
    public String getLastName() {
    return lastName;
    }
    public void setLastName(String lastName) {
    this.lastName = lastName;
    }
    public Integer getUserId() {
    return userId;
    }
    public void setUserId(Integer userId) {
    this.userId = userId;
    }
    public String getPassword() {
    return password;
    }
    public void setPassword(String password) {
    this.password = password;
    }
    public ActionErrors validate( 
    ActionMapping mapping, HttpServletRequest request ) {
     
    ActionErrors errors = new ActionErrors(); 
    if( getUserId().toString().length()<1 ) {
    errors.add("name",new ActionMessage("null.check","User ID"));
    }
    if(getEmail().length()>0)
    {
    //Yes! user entered email address. validate it
    if(!getEmail().matches("^[a-zA-Z][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$")){
    errors.add("name",new ActionMessage("email.invalid"));
    }
    }
    return errors;
    }
    }
    je pense que le problème arrive du name="user" dans la jsp de modification aprés validation

  3. #3
    Membre confirmé
    Profil pro
    Inscrit en
    Février 2006
    Messages
    198
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2006
    Messages : 198
    Par défaut
    Le problème arrive avant d'afficher le formulaire ou après avoir validé les modifications du formulaire?
    si c'est avant:
    je vois dans ton code que lors d'une SQLException tu va mettre en request le usr avec le nom "User" et sinon tu mets "user". Donc du coup la jsp ne va pas trouver le "user". Est ce que tu passes dans le catch?

  4. #4
    Membre confirmé
    Inscrit en
    Mars 2007
    Messages
    100
    Détails du profil
    Informations personnelles :
    Âge : 44

    Informations forums :
    Inscription : Mars 2007
    Messages : 100
    Par défaut
    Salut, le problème n'est pas là puisque j'a pas d'exception SQL

    le problème c'est qu'aprés la validation( exection de ma méthode validate du bean) et il y a erreur généré et dois me retourner vers la mme page pour ressaisir et c'est à ce moment là ou l'erreur est retourné.
    et c'est avand l'execution de l'action

  5. #5
    Membre confirmé
    Inscrit en
    Mars 2007
    Messages
    100
    Détails du profil
    Informations personnelles :
    Âge : 44

    Informations forums :
    Inscription : Mars 2007
    Messages : 100
    Par défaut
    j'ai modifié le code jsp de la page de modification de la façons suivante et ça marche mais je pense pas que c'est la meilleure solution à utiliser.
    j'ai utilisé logic:empty et logic:notEmpty

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
     
     
    <%@ page language="java" pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
    <%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
    <%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html:html lang="true">
      <head>
        <html:base />    
        <title>Modify user</title>
      </head>  
      <body>
      <center><h3>Modify User</h3></center>
      <CENTER><html:errors/></CENTER>
     <logic:notEmpty name="user">
        <html:form action="/Updateuser" method="post">
     
          <table border="0" align="center">
            <tr>
              <td></td>
                   <td><html:hidden name="user" property="userId" /></td>
            </tr>
            <tr>
              <td>First Name:</td>
              <td><html:text name="user" property="firstName" /></td>
            </tr>
            <tr>
              <td>Last Name:</td>
              <td><html:text name="user" property="lastName" /></td>
            </tr>
            <tr>
              <td>Age:</td>
              <td><html:text name="user" property="age" /></td>
            </tr>
            <tr>
              <td>Email:</td>
              <td><html:text name="user" property="email" /></td>
            </tr>
            <tr>
              <td colspan="2" align="center"><html:submit /></td>
            </tr>
          </table>
     
          <center> <a href="manageusers.do">Show users list </a></center>
        </html:form>
     </logic:notEmpty>
      <logic:empty name="user">
        <html:form action="/Updateuser" method="post">
     
          <table border="0" align="center">
            <tr>
              <td></td>
                   <td><html:hidden property="userId" /></td>
            </tr>
            <tr>
              <td>First Name:</td>
              <td><html:text  property="firstName" /></td>
            </tr>
            <tr>
              <td>Last Name:</td>
              <td><html:text  property="lastName" /></td>
            </tr>
            <tr>
              <td>Age:</td>
              <td><html:text  property="age" /></td>
            </tr>
            <tr>
              <td>Email:</td>
              <td><html:text property="email" /></td>
            </tr>
            <tr>
              <td colspan="2" align="center"><html:submit /></td>
            </tr>
          </table>
     
          <center> <a href="manageusers.do">Show users list </a></center>
        </html:form>
     </logic:empty>
      </body>
    </html:html>

    comme ça si user est vide c'est qu'une erreur de validation est détecté si non toute est bien.

    si vous avez une idée meilleure merci de m'informer

  6. #6
    Membre confirmé
    Profil pro
    Inscrit en
    Février 2006
    Messages
    198
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2006
    Messages : 198
    Par défaut
    oui ok, en fait quand il fait le validate et qu'il revient sur la page, comme ton user est en request et bien il ne le retrouve pas vu qu'il a été détruit.
    normalement tu devrai faire dans la classe qui affiche le formulaire:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
     
    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
    //Get a list of users and save to the request
    String userid = request.getParameter("userId");
    User usr = (User)form;
    // List list = new LinkedList();
    try {
    usr= UserManager.getInstance().getUser(userid);// récupération du Bean User àpartir de l'id
    //list.add(usr);
    System.out.print("User intercepted success");
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    ActionMessages errors = new ActionMessages();
    ActionMessage error = new ActionMessage("error.generic",e.getMessage());
    errors.add("error",error);
    saveErrors(request, errors);
    e.printStackTrace();
    }
    return mapping.findForward("success");
    }
    }

Discussions similaires

  1. maitriser validation de struts
    Par osman.amine dans le forum Struts 1
    Réponses: 1
    Dernier message: 05/11/2007, 11h31
  2. Pbs de Validation avec Struts
    Par NizarK dans le forum Struts 1
    Réponses: 1
    Dernier message: 15/05/2007, 17h03
  3. Plugin Validator pour Struts
    Par k2vin dans le forum Struts 1
    Réponses: 8
    Dernier message: 30/01/2007, 18h02
  4. [Validator] problème struts validator
    Par ge-lt dans le forum Struts 1
    Réponses: 2
    Dernier message: 05/07/2006, 15h55
  5. Réponses: 2
    Dernier message: 20/08/2005, 19h23

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo