Reputation: 126
I read through the struts tutorial and was stuck a bit at struts validation(Source: http://www.tutorialspoint.com/struts_2/struts_validations.htm). I was wondering if its necessary to use struts tags when I wish to use the implicit validate() function provided by the struts framework. Is there any possible way using which I dont have to use the struts tags and still, I am allowed to use the implicit validate function along with the option 'addFieldError'?
I had tried to make a code for the same but it seems that we can not make use of 'addFieldError' unless we make use of struts tags completely in our form. Can anyone provide a little clarification on this?
Below is the code I tried, but the 'addFieldError' didnt work and the webpage got redirected directly to the page specified in 'struts.xml' where result was specified as "input"
/*public void validate()//Function to check implicit validations provided by Struts
{
System.out.println("Inside implicit validate\n");
if(!name.equalsIgnoreCase("harshit"))
{
addFieldError("name","Only Harshit is allowed in name field");
}
}*/
Sorry as I am not familiar with the 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 be blank..");
document.getElementById("error").innerHTML="Name shouldnt be blank";
document.frm.name.focus();
return false;
}
else if(empId=="")
{
alert("Employee ID should not be blank");
document.frm.empId.focus();
return false;
}
else if(isNaN(empId)==true)
{
alert("Employee ID should be a number");
return false;
}
else if(!name.match(letters))
{
alert("Name should be an ablphabet");
return false;
}
else if(!email.match(".com"))
{
alert("Please enter a valid email");
return false;
}
}
</script>
<title>Hello World</title>
</head>
<body bgcolor="beige">
<h1>Employee Details</h1>
<form action="hello" name="frm" onSubmit="return validate()">
<label>Please enter your 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 my Action Class
package 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() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public long getPhone() {
return phone;
}
public void setPhone(long phone) {
this.phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String execute() throws Exception
{
//String result=serverValidation();
String result="success";//Temporarily making it success as the server validation is commented. Aim is to check the implicit validate method provided by struts
return 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)
return "success";
else
{
System.out.println("d="+d);
return "error";
}
}*/
public void validate()//Function to check implicit validations provided by Struts
{
System.out.println("Inside implicit validate\n");
if(!name.equalsIgnoreCase("harshit"))
{
addFieldError("name","Only Harshit is allowed in name field");
}
}
}
Upvotes: 1
Views: 801
Reputation: 126
Thanks to @AleksandrM and @AndreaLigios for providing the answer. Below is a little detailed description:
The implicit validate()
method provided by Struts2 framework can be used even without having to use the Struts2 based form tags. All you need to do is use <s:fielderror>
tag in the respective jsp/ html page where you want to display the error messages, followed by using the <param>
tag to specify for which parameter, the error has to be displayed. The parameter name should be the same as used in the Action Class. Please refer the validate()
method in the above code to see how the error messages are defined for a particular parameter. We can use the below snippet in the html/jsp to display the error message:
<s:fielderror>
<s:param> name</s:param>
</s:fielderror>
Here, name
is the parameter for which validation has been done.
Upvotes: 1