Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Validation is not worked in strust

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
submit.jsp

<%@ 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:errors/>
<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">
<html:option value="a">0-19</html:option>
<html:option value="b">20-49</html:option>
<html:option value="c">50-</html:option>
</html:select></td></tr> <tr></tr> <tr></tr>
<tr><td colspan=1 align=right><html:submit/></td>
<td><html:reset /></td></tr>
</table><br><br>
<center><h3>
<logic:present name="lastName" scope="request">
Hello
<logic:equal name="submitForm" property="age" value="a">
young
</logic:equal>
<logic:equal name="submitForm" property="age" value="c">
old
</logic:equal>
<bean:write name="lastName" scope="request"/>
</logic:present>
</html:form></h3>
</center>
</body>
</html>



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)
{
lastName="";
address="";
sex="";
married="";
age="";


}
}



Strust.xml

<?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">;

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

<action-mappings>
<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"/>
</action>
</action-mappings>
</struts-config>




Properties file

errors.header=<h4>Validation Error(s)</h4><ul>
errors.footer=</ul><hr>
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




ActionServlet


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
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic