1.In web applications in the most of the cases an application is need
to check whether input values
are entered by a client or not, before the processing logic
2.While applying validations on input, we can define Java script code
to do validations at client
side and we can also defined validations at form bean class by overriding
validate method to
implement Server side validations.
3. In a Struts Applicaion, to
implement validations at Server side manually then we use to override
validate method of ActionForm class into our FormBean class.
4. While overriding validate
method in FormBean class, we create following objects.
i. ActionErrors class
ii. ActionMessage class.
5. If validation error is
occurred then we create ActionMessage class object and encapsulates Action
Message class and encapsulate the error message key in the Resource
Bundle File.
6. In validate method we need to
store all ActionMessage objects into AtionErrors objects it means
ActionErrors object is a group of ActionMessage objects.
7. ActionServlet that is
Controller calls validate the method of a FormBean, before moving the
Control to execute(-,-,-,-) of an Action class.
Login.jsp
<%@ taglib uri="http://struts.apache.org/tags-bean"
prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html"
prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic"
prefix="logic" %>
<%--
Document : login
Created on : Oct 8, 2013,
11:30:41 AM
Author : Satya<satyanarayana@greenbuds.co.in
& satyajohnny@live.com>
--%>
<%@page contentType="text/html"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta
http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>JSP
Page</title>
</head>
<body>
<h1>Hello
World!</h1>
<html:form action="/login">
<bean:message
key="label.user"/> <html:text
property="uname"/>
<html:errors
property="uname"/><br/>
<bean:message
key="label.pwd"/><html:password
property="pwd"/>
<html:errors
property="pwd"/><br/><br/>
<html:submit
value="ok"/>
</html:form>
</body>
</html>
LoginForm
__________
/*
* To change this template,
choose Tools | Templates
* and open the template in the
editor.
*/
package com.myapp.struts;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
/**
*
* @author Satya<satyanarayana@greenbuds.co.in
& satyajohnny@live.com>
*/
public class LoginForm extends org.apache.struts.action.ActionForm {
private String uname;
private String pwd;
public String getUname() {
return uname;
}
public void setUname(String
uname) {
this.uname = uname;
}
public String getPwd() {
return pwd;
}
public void setPwd(String
pwd) {
this.pwd = pwd;
}
@Override
public ActionErrors
validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new
ActionErrors();
if(uname.isEmpty())
{
ActionMessage m1 = new
ActionMessage("user.req");
errors.add("uname", m1);
} if(pwd.isEmpty())
{
ActionMessage m2 = new
ActionMessage("pwd.req");
errors.add("pwd",m2);
}
return errors;
}
}
Struts.config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache
Software Foundation//DTD Struts Configuration 1.3//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean
name="LoginForm" type="com.myapp.struts.LoginForm"/>
</form-beans>
<global-exceptions>
</global-exceptions>
<global-forwards>
<forward
name="welcome"
path="/Welcome.do"/><forward name="ok" path="/ok.jsp"/>
<forward name="fail"
path="/fail.jsp"/>
</global-forwards>
<action-mappings>
<action
input="/login.jsp" name="LoginForm" path="/login"
scope="request" type="com.myapp.struts.LoginAction"/>
<action
path="/Welcome" forward="/welcomeStruts.jsp"/>
</action-mappings>
<controller
processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>
<message-resources
parameter="com/myapp/struts/ApplicationResource"/>
<message-resources
parameter="com/myapp/struts/satya" />
<!-- =========================
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="/WEB-INF/tiles-defs.xml"
/>
<set-property
property="moduleAware" value="true" />
</plug-in>
<!--
========================= Validator plugin =================================
-->
<plug-in
className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
</struts-config>
Error Message
Identifying:
500 error: If we get 500 Errros-Module „null‟ found then we
have to check <load-
not in web.xml file, is there any problem in Resource Bundle
File and check once s
and every thing.
Java.lang.NoClassFoundError: If we find this error verify struts-taglib.jar. havin
The absolute uri: If we get this error check once uri whether it is correct or not.