Hi i have developed a simple application which is used to ask the user to add their name and age. It should then verify both fields in such a way that the age should be between 18 - 65 and the name must not be NULL. After it is validated it should go to my acknowledge page . With in my Applictaion Resource file i have included
errors.name.required=Name must be entered!
errors.age.range=Age out of range 18 - 65
with <th> <html:errors property="age" /> and <html:errors property="name" /> in my jsp file but it still does not show up the errors. Can somebody show me why this is not showing up.
enterDetails.jsp
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%--
The taglib directive below imports the JSTL library. If you uncomment it,
you must also add the JSTL library to the project. The Add Library... action
on Libraries node in Projects view can be used to add the JSTL 1.1 library.
--%>
<%--
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
--%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Enter Details Page</title>
</head>
<body>
<h1>Enter Details Page</h1>
<html:form action="ValidateDetails">
<table border="1">
<thead>
<tr>
<td> Please enter your name:
<th> <bean:message key="validatedetails.name" /> </th>
<th><html:text property="name" /></th>
<th> <html:errors property="name" />
</tr>
</thead>
<tbody>
<tr>
<td> Please enter your age 18 - 65
<td><bean:message key="validatedetails.age" /></td>
<td><html:text property="age" /></td>
<th> <html:errors property="age" />
</tr>
<tr>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<html:submit value="ValidateDetails" />
</html:form>
<%--
This example uses JSTL, uncomment the taglib directive above.
To test, display the page like this: index.jsp?sayHello=true&name=Murphy
--%>
<%--
<c:if test="${param.sayHello}">
<!-- Let's welcome the user ${param.name} -->
Hello ${param.name}!
</c:if>
--%>
</body>
</html>
struts-config.xml file:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="DetailsForm" type="com.myapp.struts.DetailsForm"/>
</form-beans>
<global-exceptions>
</global-exceptions>
<global-forwards>
<forward name="welcome" path="/Welcome.do"/>
</global-forwards>
<action-mappings>
<action input="/enterDetails.jsp"
name="DetailsForm"
path="/ValidateDetails"
scope="request"
type="com.myapp.struts.ValidateDetails">
<forward name="success" path="/Acknowledge.jsp"/>
</action>
<action path="/Welcome"
forward="/welcomeStruts.jsp"/>
<action/>
</action-mappings>
<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>
<message-resources parameter="com/myapp/struts/ApplicationResource"/>
<!-- ========================= 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>
DetailsForm.java
/*
* DetailsForm.java
*
* Created on 05 April 2008, 20:51
*/
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 luong
* @version
*/
public class DetailsForm extends org.apache.struts.action.ActionForm {
private String name;
private String age;
/**
* @return
*/
public String getName() {
return name;
}
/**
* @param string
*/
public void setName(String string) {
name = string;
}
/**
* @return
*/
public String getAge() {
return age;
}
/**
* @param i
*/
public void setAge(String i) {
age = i;
}
/**
*
*/
public DetailsForm() {
super();
// TODO Auto-generated constructor stub
}
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if (getName() == null || getName().length() < 1) {
errors.add("name", new ActionMessage("error.name.required"));
// TODO: add 'error.name.required' key to your resources
}
if (getAge() == null || getAge().length() < 1) {
errors.add("age", new ActionMessage("error.age.required"));
} else {
try {
int a = Integer.parseInt(getAge()) ;
if (a < 18 || a > 65) {
errors.add("age", new ActionMessage("error.age.range"));
}
} catch (NumberFormatException e) {
errors.add("age", new ActionMessage("error.age.format"));
}
}
return errors;
}
}
ValidateDetails.java
/*
* ValidateDetails.java
*
* Created on 05 April 2008, 21:05
*/
package com.myapp.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
/**
*
* @author luong
* @version
*/
public class ValidateDetails extends Action {
/* forward name="success" path="" */
private final static String SUCCESS = "success";
/**
* This is the action called from the Struts framework.
* @param mapping The ActionMapping used to select this instance.
* @param form The optional ActionForm bean for this request.
* @param request The HTTP Request we are processing.
* @param response The HTTP Response we are processing.
* @throws java.lang.Exception
* @return
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
return mapping.findForward(SUCCESS);
}
}
ApplicationResource file
errors.name.required=Name must be entered!
errors.age.range=Age out of range 18 - 65
errors.header=
errors.prefix=<span style="color: red">
errors.suffix=</span>
errors.footer=
validatedetails.age=Age
validatedetails.name=Name
errors.invalid={0} is invalid.
errors.maxlength={0} can not be greater than {1} characters.
errors.minlength={0} can not be less than {1} characters.
errors.range={0} is not in the range {1} through {2}.
errors.required={0} is required.
errors.byte={0} must be an byte.
errors.date={0} is not a date.
errors.double={0} must be an double.
errors.float={0} must be an float.
errors.integer={0} must be an integer.
errors.long={0} must be an long.
errors.short={0} must be an short.
errors.creditcard={0} is not a valid credit card number.
errors.email={0} is an invalid e-mail address.
errors.cancel=Operation cancelled.
errors.detail={0}
errors.general=The process did not complete. Details should follow.
errors.token=Request could not be completed. Operation is not in sequence.
welcome.title=Struts Application
welcome.heading=Struts Applications in Netbeans!
welcome.message=It's easy to create Struts applications with NetBeans.
Acknowledge.jsp file
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%--
The taglib directive below imports the JSTL library. If you uncomment it,
you must also add the JSTL library to the project. The Add Library... action
on Libraries node in Projects view can be used to add the JSTL 1.1 library.
--%>
<%--
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
--%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Acknowledge Page</title>
</head>
<body>
<h1>Acknowledge Page</h1>
<%--
This example uses JSTL, uncomment the taglib directive above.
To test, display the page like this: index.jsp?sayHello=true&name=Murphy
--%>
<%--
<c:if test="${param.sayHello}">
<!-- Let's welcome the user ${param.name} -->
Hello ${param.name}!
</c:if>
--%>
</body>
</html>
Everytime i enter a reasonable value and a string, it goes to my acknowledge page, but when entering an out of range age and with no name, it simply does not show my errors. Could somebody give some insigh on this please.
Thanks