• Post Reply Bookmark Topic Watch Topic
  • New Topic

Validation is not worked in strust  RSS feed

sathish sanigneni
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html><head><title>Submit example</title></head>
<body bgcolor=yellow><center><h1>Example Submit Page</h1></center>
<table align=center width="500" cellspacing=10>
<html:form action="submit.do">
<tr><td align=right>Last Name:</td><td>
<html:text property="lastName"/></td></tr>
<tr><td align=right>Address:</td><td>
<html:textarea property="address"/></td></tr>
<tr><td align=right>Sex: </td><td>
<html:radio property="sex" value="M"/>Male
<html:radio property="sex" value="F"/>Female<br></td></tr>
<tr><td align=right>Married:</td><td>
<html:checkbox property="married"/></td></tr>
<tr><td align=right>Age:</td><td>
<html:select property="age">
<htmlption value="a">0-19</htmlption>
<htmlption value="b">20-49</htmlption>
<htmlption value="c">50-</htmlption>
</html:select></td></tr> <tr></tr> <tr></tr>
<tr><td colspan=1 align=right><html:submit/></td>
<td><html:reset /></td></tr>
<logic:present name="lastName" scope="request">
<logic:equal name="submitForm" property="age" value="a">
<logic:equal name="submitForm" property="age" value="c">
<bean:write name="lastName" scope="request"/>

Action Form

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;

public final class SubmitForm extends ActionForm
/* Last Name */
private String lastName ; // default value
public String getLastName() {
return (this.lastName);
public void setLastName(String lastName) {
this.lastName = lastName;

/* Address */
private String address = null;
public String getAddress() {
return (this.address);
public void setAddress(String address) {
this.address = address;

/* Sex */
private String sex = null;
public String getSex() {
return (this.sex);
public void setSex(String sex) {
this.sex = sex;

/* Married status */
private String married = null;
public String getMarried() {
return (this.married);
public void setMarried(String married) {
this.married = married;

/* Age */
private String age = null;
public String getAge() {
return (this.age);
public void setAge(String age) {
this.age = age;

public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request)
// Log the forms data
servlet.log("Lastname:" + lastName);
servlet.log("Address:" + address);
servlet.log("Sex:" + sex);
servlet.log("Married:" + married);
servlet.log("Age:" + age);

// Check for mandatory data
ActionErrors errors = new ActionErrors();

if (lastName == null || lastName.equals("")) {
errors.add("Last Name", new ActionMessage("error.lastName"));
if (address == null || address.equals("")) {
errors.add("Address", new ActionMessage("error.address"));
if (sex == null || sex.equals("")) {
errors.add("Sex", new ActionMessage("error.sex"));

if (age == null || age.equals("")) {
errors.add("Age", new ActionMessage("error.age"));

return errors;
public void reset(ActionMapping mapping, HttpServletRequest request)



<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.0//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd">

<form-bean name="submitForm" type="SubmitForm"/>

<action path="/submit" type="SubmitAction" name="submitForm" input="/submit.jsp" validate="true" scope="request">
<forward name="success" path="/submit.jsp"/>
<forward name="failure" path="/submit.jsp"/>

Properties file

errors.header=<h4>Validation Error(s)</h4><ul>
error.lastName=<li>Enter your last name
error.address=<li>Enter your address
error.sex=<li>Enter your sex
error.age=<li>Enter your age


import javax.servlet.http.*;
import org.apache.struts.action.*;

public final class SubmitAction extends Action

public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
SubmitForm f = (SubmitForm) form; // get the form bean

// and take the last name value
String lastName = f.getLastName();

// Translate the name to upper case
//and save it in the request object
request.setAttribute("lastName", lastName.toUpperCase());

// Forward control to the specified success target
return (mapping.findForward("success"));

Where the mistake took place i don't know ... i have been trying this from two days .... please find solution for this problem
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!