-
Utilisation du Validator
Bonjour,
Cela fait pas tres longtemps que je travaille sous java webappli.
Je demande votre aide parceque je suis un peu coincer par rapport aux document.
Je dois utiliser Struts pour valider des formulaires, mais je sais pas par ou commencer, sachant que jutilise Maven 2 (Maven Modular Struts), Appfuse.
Quelqun possede des liens ou peut maider svp ?
Sachant que la jai le formulaire et la page MachinClass-validation.xml et la je sais pas comment m'y prendre pour continuer.
Merci pour votre aide
NB : il s'agit d'un projet avec des contraints, pcq si c'est moi j'aurais pas utilise des trucs aussi compliquer.
-
1 . Available for Eclipse EDI.
2 . Check that the project deploys and run correctly before begining.
3 . Create a QuizAction.jsp page with a form in the repertory src/main/webapp or src/main/webapp/WEB-INF/pages. With something like this :
* Add this instruction at the top of your jsp page for using struts tag
<%@ taglib prefix="s" uri="/struts-tags" %>
* Add this block in the body of the page
<s:form method="post" validate="true" action="QuizAction">
<s:textfield label="Name" name="name"/>
<s:textfield label="Age" name="age"/>
<s:submit/>
</s:form>
(If you use the basic project template for running your jsp page you must call by mypage.html instead of mypage.jsp)
(In the form tag, attributs validate="true" and action="QuizAction" is important)
(Don't forget <%@ taglib prefix="s" uri="/struts-tags" %> else your jsp page will never run) correctly.
4 . Create the action class in namepackage.webapp.action in the repertory src/main/java
Your action class can look like this :
package tutorial.webapp.action;
import com.opensymphony.xwork2.ActionSupport;
public class QuizAction extends ActionSupport {
private static final long serialVersionUID = -7505437345373234225L;
String name="test";
int age;
String answer;
public String getName()
{
return name;
}
}
In fact it's not very important yet to have , you must just have an action class extends ActionSupport running without error for testing Struts 2 Validator.
5 . Create a XML file for the validation the name is important it must take this kind of form name_of_action_class-validation.xml. You must add it in the repertory src/main/resources/you_ package_name/webapp/action.
* The contain must have this form :
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="name">
<field-validator type="requiredstring">
<message>You must enter a name</message>
</field-validator>
</field>
<field name="age">
<field-validator type="int">_
<param name="min">13</param>
<param name="max">19</param>
<message>Only people ages 13 to 19</message>
</field-validator>
</field>
</validators>
* In the tag field the name is the name of your field in the validating form
* The attribut type in the tag field-validator is for define the type of validation you want to use
* The tag message is almost required and is used for message display
6 . Add the mapping of your action in the struts.xml that you can find in the repertory src/main/resources. It can look like something like this :
<struts>_
<!---others packages--->
<package name="<your package>.webapp.action" extends="struts-default">
<action name="QuizAction" class="<yourpackage>.webapp.action.QuizAction ">
<result name="input">/WEB-INF/pages/QuizAction.jsp</result>_
<result name="success">/WEB-INF/pages/QuizAction.jsp </result>_
</action>
</package>
</struts>
REFERENCES
* http://struts.apache.org/2.x/docs/validation.html