Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-07-2008, 11:41 PM
Member
 
Join Date: Aug 2007
Posts: 9
nvidia is on a distinguished road
Error Messages Not Showing Up
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
Code:
<%@ 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:
Code:
<?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
Code:
/* * 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
Code:
/* * 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
Code:
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
Code:
<%@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

Last edited by nvidia : 04-07-2008 at 11:50 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
client/server messages exchange after 5 min dim_ath Advanced Java 2 01-22-2008 09:46 AM
how can server send messages every 5 min? dim_ath Networking 7 01-10-2008 04:59 PM
Log messages are not appearing in my log file indu_raj Java Servlet 1 08-09-2007 12:59 AM
Jsf Error Messages - Retain vaswin JavaServer Faces 0 08-05-2007 09:22 PM
Compile failed, messages should have been provided leonard New To Java 2 07-30-2007 06:36 PM


All times are GMT +3. The time now is 10:36 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org