Friday 15 January 2010

java - Is it necessary to use struts tags in form when using the validate function? -



java - Is it necessary to use struts tags in form when using the validate function? -

i read through struts tutorial , stuck bit @ struts validation(source: http://www.tutorialspoint.com/struts_2/struts_validations.htm). wondering if necessary utilize struts tags when wish utilize implicit validate() function provided struts framework. there possible way using dont have utilize struts tags , still, allowed utilize implicit validate function along alternative 'addfielderror'?

i had tried create code same seems can not create utilize of 'addfielderror' unless create utilize of struts tags in our form. can provide little clarification on this?

below code tried, 'addfielderror' didnt work , webpage got redirected straight page specified in 'struts.xml' result specified "input"

/*public void validate()//function check implicit validations provided struts { system.out.println("inside implicit validate\n"); if(!name.equalsignorecase("harshit")) { addfielderror("name","only harshit allowed in name field"); } }*/

sorry not familiar formatting here.

contents of struts.xml

<?xml version="1.0" encoding="utf-8"?> <!doctype struts public "-//apache software foundation//dtd struts configuration 2.0//en" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devmode" value="false"/> <package name="helloworld" extends="struts-default"> <action name="hello" class="com.tutorialspoint.struts2.helloworldaction" method="execute"> <result name="success">/helloworld.jsp</result> <result name="error">/error.jsp</result> <result name="input">/index.jsp</result> </action> </package> </struts>

contents of web.xml

<?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="webapp_id" version="3.0"> <display-name>struts 2</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.filterdispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>

conetents of index.jsp page

<%@ page language="java" contenttype="text/html; charset=iso-8859-1" pageencoding="iso-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <script> function validate() { var empid= document.frm.empid.value; var name=document.frm.name.value; var letters = /^[a-za-z]+$/; var email=document.frm.email.value; if(name=="") { //alert("name should not blank.."); document.getelementbyid("error").innerhtml="name shouldnt blank"; document.frm.name.focus(); homecoming false; } else if(empid=="") { alert("employee id should not blank"); document.frm.empid.focus(); homecoming false; } else if(isnan(empid)==true) { alert("employee id should number"); homecoming false; } else if(!name.match(letters)) { alert("name should ablphabet"); homecoming false; } else if(!email.match(".com")) { alert("please come in valid email"); homecoming false; } } </script> <title>hello world</title> </head> <body bgcolor="beige"> <h1>employee details</h1> <form action="hello" name="frm" onsubmit="return validate()"> <label>please come in name</label><br/> <input type="text" name="name"/><font size="4" color="red"> <div id="error"> </div></font> <br/> <label>employee id:</label><br/> <input type="text" name="empid"/><br/> <label>email id:</label><br/> <input type="text" name="email"> <br/> <label>phone number:</label><br/> <input type="text" name="phone"> <br/> <br/> <br/> <input type= "button" value="check" onclick="return validate()"> <input type="submit" value="submit"/> </form> </body> </html>

contents of action class

bundle com.tutorialspoint.struts2; import com.opensymphony.xwork2.actionsupport; public class helloworldaction extends actionsupport { private string name ; private string email; private long phone; private int empid; public int getempid() { homecoming empid; } public void setempid(int empid) { this.empid = empid; } public string getemail() { homecoming email; } public void setemail(string email) { this.email = email; } public long getphone() { homecoming phone; } public void setphone(long phone) { this.phone = phone; } public string getname() { homecoming name; } public void setname(string name) { this.name = name; } public string execute() throws exception { //string result=servervalidation(); string result="success";//temporarily making success server validation commented. aim check implicit validate method provided struts homecoming result; } /*public string servervalidation() throws exception { long phnum= phone; system.out.println("phone="+phone); system.out.println("phnum="+phnum); system.out.println("employee id=" +empid); int d=0; while(phnum>0) { phnum=phnum/10; d=d+1; } if(d==10) homecoming "success"; else { system.out.println("d="+d); homecoming "error"; } }*/ public void validate()//function check implicit validations provided struts { system.out.println("inside implicit validate\n"); if(!name.equalsignorecase("harshit")) { addfielderror("name","only harshit allowed in name field"); } } }

thanks @aleksandrm , @andrealigios providing answer. below little detailed description:

the implicit validate() method provided struts2 framework can used without having utilize struts2 based form tags. need utilize <s:fielderror> tag in respective jsp/ html page want display error messages, followed using <param> tag specify parameter, error has displayed. parameter name should same used in action class. please refer validate() method in above code see how error messages defined particular parameter. can utilize below snippet in html/jsp display error message:

<s:fielderror> <s:param> name</s:param> </s:fielderror>

here, name parameter validation has been done.

java jsp struts2

No comments:

Post a Comment