Ashwini Kulkarny

Greenhorn
+ Follow
since Aug 12, 2008
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Ashwini Kulkarny

validation.xml :-
<form name="CheckWageCriteriaForm">
<field depends="validateAllQtrs">
<msg name="mask" key="CheckWageCriteriaForm.currQtr1.maskmessage"/>
<arg key="CheckWageCriteriaForm.currQtr1"/>
<var>
<var-name>mask</var-name>
<var-value>^[0-9]*$</var-value>
</var>
</field>
</form>
16 years ago
Hi,

Is it possible to validate multiple textboxes on a jsp using struts validator without having to specify each property in the validator.xml?
I have a jsp which has 20 textboxes (currQuarter1,currQuarter2,..prevQuarter1...etc)
I want to validate all of them so that the user may only enter numerical values. I am using the mask - ^[0-9]*$ and it works fine if i explicily specify the property name in the validation.xml
eg -
<form name="CheckWageCriteriaForm">
<field property="currQtr1" depends="mask">
<msg name="mask" key="CheckWageCriteriaForm.currQtr1.maskmessage"/>
<arg key="CheckWageCriteriaForm.currQtr1"/>
<var>
<var-name>mask</var-name>
<var-value>^[0-9]*$</var-value>
</var>
</field>
</form>

However, I have 20 properties and i dont want to write the form tag for all of them since they have the same validation.
So, i wrote a custom validation class, and added it to validation-rules.xml.

WagePaymentsValidator Class :-
public class WagePaymentsValidator implements Serializable{

/**
*
*/
private static final long serialVersionUID = 1L;

public static boolean validateAllQtrs(
Form form,
Object bean,
ValidatorAction va,
Field field,
ActionErrors errors,
HttpServletRequest request,
ServletContext application) {

//String value = ValidatorUtils.getValueAsString(
//bean,
//field.getProperty());
try
{
String currYearQtr = null;
List<String> currQtrList = new ArrayList<String>();
String prevYearQtr = null;
List<String> prevQtrList = new ArrayList<String>();

for(int i=0;i<4;i++)
{
currYearQtr = "currQtr"+i;
field = form.getField(currYearQtr);
String sProperty = field.getVarValue(currYearQtr);
String currQtrValue = ValidatorUtils.getValueAsString(bean, sProperty);
if (!GenericValidator.isBlankOrNull(currQtrValue))
{
if(!containsOnlyNumbers(currQtrValue))
return false;
}
//currQtrList.add(currQtrValue);
}
for(int i=1;i<=4;i++)
{
for(int j=1;j<=4;i++)
{
prevYearQtr = "prev"+i+"Qtr"+j;
field = form.getField(prevYearQtr);
String sProperty = field.getVarValue(prevYearQtr);
String prevQtrValue = ValidatorUtils.getValueAsString(bean, sProperty);
if (!GenericValidator.isBlankOrNull(prevQtrValue))
{
if(!containsOnlyNumbers(prevQtrValue))
return false;
}
//prevQtrList.add(prevQtrValue);
}

}
}
catch (Exception e) {
errors.add(field.getKey(),
Resources.getActionMessage(request, va, field));
return false;
}

return true;

}
/**
* This method checks if a String contains only numbers
*/
private static boolean containsOnlyNumbers(String str) {

//Check for null or empty fields
if (str == null || str.length() == 0)
return false;

for (int i = 0; i < str.length(); i++) {

//If we find a non-digit character we return false.
if (!Character.isDigit(str.charAt(i)))
return false;
}

return true;
}

}

validation-rules.xml :-
<!--Custom Validator for Wage Payments page-->
<validator name="validateAllQtrs"
classname="gov.iowa.wd.ui.employer.determineliability.WagePaymentsValidator"
method="validateAllQtrs"
methodParams="org.apache.commons.validator.Form,
java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionErrors,
org.apache.commons.validator.Validator,
javax.servlet.http.HttpServletRequest"
depends=""
msg="CheckWageCriteriaForm.currQtr1.maskmessage"/>

However, the system does not validate the fields and if i enter a non-numerical value in any of the textboxes and submit, i get numberformatexception in my ejb method as it can handle only numeric values.

Is there any other way I can achieve validation for multiple textboxes?
16 years ago

Originally posted by Ashwini Kulkarny:


Hi, I had already tried that. It gives me this error - Error 404: com.ibm.ws.webcontainer.servlet.exception.NoTargetForURIException: No target servlet configured for uri: /gonext

No, i do not have 4 years. I have n no of years, and this no will be known at runtime. all i know is tht they have 4 columns. thats y i cant use a form bean directly. i did try using a form bean having a List as its member. eg - YearList. Yearlist was a bean and had 4 properties (1 for each qtr).but that too does not work as the form does not get populated.



Also, i dont fetch the no of years from DB, i get them frm session..
16 years ago
JSP

Originally posted by Raj Kamal:
Hi,

Try out the suggestion Amit made. I feel that should do it.

They way I understood your problem is that you have 4 years with their corresponding quarter columns. You fetch them from the database and so you know which year is currently referenced and which quarters belong to which years. Why use the actual year values in the form element names? Would not a Year1Quarter1, Year2Quarter2....... suffice? You can then have matching fields in the Form bean too. In case you wont be able to make out which year Year1 refers to why not have a hidden field called Year1 in your form holding that value? Now from the Action you should be able to get what you want done right?

Cheers,
Raj.



Hi, I had already tried that. It gives me this error - Error 404: com.ibm.ws.webcontainer.servlet.exception.NoTargetForURIException: No target servlet configured for uri: /gonext

No, i do not have 4 years. I have n no of years, and this no will be known at runtime. all i know is tht they have 4 columns. thats y i cant use a form bean directly. i did try using a form bean having a List as its member. eg - YearList. Yearlist was a bean and had 4 properties (1 for each qtr).but that too does not work as the form does not get populated.
16 years ago
JSP

Originally posted by Ashwini Kulkarny:


Hi Amit,Raj,
Thanks for your reply
Sorry for the bad formatting, actually this is the first time i am using javaranch.. Actually i did try using a submit button instead of a link an wrote a javascript function for the onclick event of the button -
function goForward()
{
document.forms[0].action = "gonext.do";
document.forms[0].submit();
}
However, somehow in this case too the form was not getting submitted and the action class was not getting called. So i decided to try and use a link. It did call the action, but then as you said, the request would not be populated.



One more thing, in the javascript function, i tried calling the action in all possible ways -
"/gonext.do"
"/gonext"
"gonext.do"
"gonext"
but it did not work..
16 years ago
JSP

Originally posted by Raj Kamal:
Yes the link wont act like a form Submit thats why the parameters are not present in the request.. Speaking about STRUTS, are you not defeating the whole point about STRUTS by making calls to request.getParameter() while it already provides the mapping for FORM elements to Form bean properties (which have to be defined in the struts-config.xml)?

Cheers,
Raj.



Hi raj,

Actually the thing is that the code I have posted here is just a sample code that i developed just to check if the request object was getting populated or not. The jsp that I am developing for my project consists of tables that are dymanically generated according to a value that I retrieve from session.
For eg, I have a table with 4 columns which are textboxes(qtr1,Qtr2,qtr3,Qtr4) - one for each quarter of the year. Now, depending on a value (effective date) that i get from session, i have to generate tables. So, if effective date is someting like 1/1/2005, I have 4 tables on my jsp (i.e. for 2005,2006,2007,2008(current date)), each with 4 columns for 4 quarters.
Since i dont know how many tables i will have at runtime, i cannot use the form bean.
So what I did was that for each quarter(textbox), i named it according to year and quarter eg- txt2006qtr1, txt2006qtr2 etc..and was hoping to retrieve them in the action class simply by using a for loop.
But unfortunately the javascript function that I had written earlier to call action class onclick of submit button is not working...

I have even tried using a form bean that has a list of 'YearBeans'. and YearBean is a class having 4 properties, one for each qtr, but that also does not work, as the form does not get populated.

Can you suggest anything?

Thanks in advance
16 years ago
JSP

Originally posted by Amit Ghorpade:
Hi Ashwini Kulkarny welcome to Javaranch
Firstly please UseCodeTags.Unformatted code is hard to read and will generate less responses. You can edit your post by clicking the .


Simply because its not passed with the request.


Hope this helps



Hi Amit,Raj,
Thanks for your reply
Sorry for the bad formatting, actually this is the first time i am using javaranch.. Actually i did try using a submit button instead of a link an wrote a javascript function for the onclick event of the button -
function goForward()
{
document.forms[0].action = "gonext.do";
document.forms[0].submit();
}
However, somehow in this case too the form was not getting submitted and the action class was not getting called. So i decided to try and use a link. It did call the action, but then as you said, the request would not be populated.
16 years ago
JSP

Originally posted by Amit Ghorpade:
Hi Ashwini Kulkarny welcome to Javaranch
Firstly please UseCodeTags.Unformatted code is hard to read and will generate less responses. You can edit your post by clicking the .


Simply because its not passed with the request.


Hope this helps

16 years ago
JSP
Hi,

I realize this is very much a beginner's level problem, but unfotunately I could not find any solution to it. i am coding a J2EE application using jsp and struts 1.3 for the frontend using RAD7.5.

My issue is that the value of request.getParameter is null in th srtuts action class.So, i created a sample application with 2 simple jsps and an action class. in the sample code, i have an input jsp with only 1 text field and a link 'next'. When I click this link, it calls the action. In the action, it reads the text field value using request.getParameter, and sets the value read to the request object. However, when I debug the application , I am getting null value for request.getParameter.
One more issue is that inspite of setting the method type as 'GET', I do not see the parameters in the url after it is submitted.
My code is as follows -

JSP 1 - start.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form name="startform" method="GET">
<input type="text" name="txtstart"/>
<html:link action="/gonext">Next</html:link>
</form>
</body>
</html>


JSP - end.jsp -

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>output</title>
</head>
<body>
<%=request.getAttribute("txtstart") %>
</body>
</html>

Struts-config.xml file -

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
</form-beans>
<global-exceptions>
</global-exceptions>
<global-forwards>
</global-forwards>
<action-mappings>
<action path="/gonext" type="org.wd.testget.actions.GonextAction">
<forward name="success" path="/jsp/end.jsp">
</forward>
<forward name="failure" path="">
</forward>
</action>
</action-mappings>
<message-resources parameter="org.wd.testget.resources.ApplicationResources"/>
</struts-config>


Action Class -

package org.wd.testget.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
* @version 1.0
* @author
*/
public class GonextAction extends Action

{

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception {

ActionMessages errors = new ActionMessages();
ActionForward forward = new ActionForward(); // return value

try {

String op = (String) request.getParameter("txtstart");
request.setAttribute("outputtext", op);


} catch (Exception e) {

// Report the error using the appropriate name and ID.
errors.add("name", new ActionMessage("id"));

}

// If a message is required, save the specified key(s)
// into the request for use by the <struts:errors> tag.

if (!errors.isEmpty()) {
saveErrors(request, errors);

// Forward control to the appropriate 'failure' URI (change name as desired)
//forward = mapping.findForward(failure");

} else {

// Forward control to the appropriate 'success' URI (change name as desired)
forward = mapping.findForward("success");

}

// Finish with
return (forward);

}
}

Can anyone please tell me -
1.why request.getParameter is returning null value?
2.why is the text parameter not showing in the url after it is submitted even though the method is 'GET'?

Would really really appreciate any help!

Thanks in advance,
Ashwini
[ August 13, 2008: Message edited by: Bear Bibeault ]
16 years ago
JSP