pdvprasad
Aug 8th, 2007, 08:48 AM
i am trying to validate textarea using validation .xml,in this example created iam using daos,hibernate for creating database to store textarea information
this is my model class
Thsi is my service class
DiscussBoardManagaer.java i adding th efollowing method
public void setValidator(Validator validator);
this is my service implement class
DiscussBoardManagerImpl.java
public class DiscussBoardManagerImpl extends BaseManager implements DiscussBoardManager {
private static Log log = LogFactory.getLog(DiscussBoardManagerImpl.class);
private DiscussBoardDao discussBoardDao;
private Validator validator;
public void setValidator(Validator validator) {
this.validator = validator;
}
public void saveDiscussBoard(DiscussBoard discussBoard) {
// if new user, lowercase userId
BindException errors = new BindException(discussBoard, "discussBoard");
validator.validate(discussBoard, errors);
if (errors.hasErrors()) {
throw new RuntimeException("validation failed!", errors);
}
try {
discussBoardDao.saveDiscussBoard(discussBoard);
} catch (Exception e) {
e.printStackTrace();
}
}
);
}
}
this is my controll class
DisplayCommentsController.java
package com.aikia.webapp.action;
public class DisplayCommentsController extends BaseFormController {
private DiscussBoardManager discussBoardManager;
/**
* @param discussBoardManager The discussBoardManager to set.
*/
public void setDiscussBoardManager(DiscussBoardManager discussBoardManager) {
this.discussBoardManager = discussBoardManager;
}
public DisplayCommentsController() {
setCommandName("DiscussComment");
setCommandClass(DiscussBoard.class);
}
public ModelAndView handleRequestInternal(HttpServletRequest request,HttpServletResponse response)throws Exception{
List l = discussBoardManager.getDiscussBoards();
return new ModelAndView("disc_forum","list",l);
}
}
another controll class
this is my validator class
DiscussBoardValidator.java
package com.aikia.webapp.util;
public class DiscussBoardValidator implements Validator{
private Log log = LogFactory.getLog(DiscussBoardValidator.class);
public boolean supports(Class clazz) {
return clazz.equals(DiscussBoard.class);
}
public void validate(Object obj, Errors errors) {
if (log.isDebugEnabled()) {
log.debug("entering 'validate' method...");
}
DiscussBoard discussBoard = (DiscussBoard) obj;
ValidationUtils.rejectIfEmptyOrWhitespace(errors,
"userComment", "errors.required", "Value required.");
}
}
this is my applicationContext_service.xml
<bean id="discussBoardManager" parent="txProxyTemplate">
<property name="target">
<bean class="com.aikia.service.impl.DiscussBoardManagerImpl">
<property name="discussBoardDao" ref="discussBoardDao"/>
<property name="validator"><ref bean="DiscussBoardValidator"/></property>
<bean id="DiscussBoardValidator" class="com.aikia.webapp.util.DiscussBoardValidator"/>
<bean id="validatorFactory" class="org.springmodules.commons.validator.DefaultValidat orFactory">
<property name="validationConfigLocations">
<list>
<value>/WEB-INF/validator-rules.xml</value>
<value>/WEB-INF/validation.xml</value>
</list>
</property>
</bean>
and my jsp page is dis
</bean>
</property>
<bean id="userValidator" class="org.appfuse.web.UserValidator"/>
<!-- Override default transaction attributes b/c of UserExistsException -->
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED,-UserExistsException</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
my jsp page i s disc_forum.jsp
<%@ include file="/common/taglibs.jsp"%>
<%@ taglib uri="/web/WEB-INF/pager-taglib.tld" prefix ="pg" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><title>Aikia - Discussion Forums </title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="styles/custom.css" rel="stylesheet" type="text/css">
</head>
<body>
<td valign="top" width="80%">
<form name="inputForm" method="post" action="comment.html" onsubmit="return validateUser(this)">
<fmt:message key="DiscussComment.userComment"/>
<div id="commentInputContainer">
<table border="0" cellpadding="0" cellspacing="0">
<tbody><tr>
<td rowspan="3" style="padding-right: 40px;">
<spring:bind path="DiscussComment.userComment">
<span class="fieldError"><c:out value="${status.errorMessage}"/></span>
<input type="textarea" name="userComment" value="<c:out value="${status.value}"/>" id="userComment" cols="55" rows="4"align="" class="text large"/>
</spring:bind>
</td>
</tr>
<tr>
<br>
<td valign="bottom">
<input type="submit" class="button" value="Post" />
</tr>
</tbody></table>
</td>
</tr>
<tr><td> </td></tr>
</tbody></table>
</div>
</form>
<br><br>
</html>
<script type="text/javascript" src="<c:url value="/scripts/validator.jsp"/>"></script>
and validation .xml file is
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_1.dtd">
<form-validation>
<formset>
<form name="inputForm">
<field property="userComment" depends="required">
<arg0 key="DiscussComment.userComment"/>
</field>
</form>
</formset>
</form-validation>
and iam facing follwing errors when iam using spring bind in the jsp
javax.servlet.jsp.JspTagException: Neither Errors instance nor plain target object for bean name 'DiscussComment' available as request attribute
at org.springframework.web.servlet.tags.BindTag.doSta rtTagInternal(BindTag.java:118)
at org.springframework.web.servlet.tags.RequestContex tAwareTag.doStartTag(RequestContextAwareTag
and when i changing applicationcontext_service file it fshow the following error
at java.lang.Thread.run(Thread.java:595)
ERROR [http-8080-Processor25] .listenerStart(3767) | Exception sending context initialized event to listener instance of class com.aikia.webapp.listener.StartupListener
org.springframework.beans.factory.BeanDefinitionSt oreException: Line 58 in XML document from ServletContext resource [/WEB-INF/applicationContext-service.xml] is invalid; nested exception is org.xml.sax.SAXParseException: The content of element type "bean" must match "(description?,(constructor-arg|property|lookup-method|replaced-method)*)".
org.xml.sax.SAXParseException: The content of element type "bean" must match "(description?,(constructor-arg|property|lookup-method|replaced-method)*)".
this is my model class
Thsi is my service class
DiscussBoardManagaer.java i adding th efollowing method
public void setValidator(Validator validator);
this is my service implement class
DiscussBoardManagerImpl.java
public class DiscussBoardManagerImpl extends BaseManager implements DiscussBoardManager {
private static Log log = LogFactory.getLog(DiscussBoardManagerImpl.class);
private DiscussBoardDao discussBoardDao;
private Validator validator;
public void setValidator(Validator validator) {
this.validator = validator;
}
public void saveDiscussBoard(DiscussBoard discussBoard) {
// if new user, lowercase userId
BindException errors = new BindException(discussBoard, "discussBoard");
validator.validate(discussBoard, errors);
if (errors.hasErrors()) {
throw new RuntimeException("validation failed!", errors);
}
try {
discussBoardDao.saveDiscussBoard(discussBoard);
} catch (Exception e) {
e.printStackTrace();
}
}
);
}
}
this is my controll class
DisplayCommentsController.java
package com.aikia.webapp.action;
public class DisplayCommentsController extends BaseFormController {
private DiscussBoardManager discussBoardManager;
/**
* @param discussBoardManager The discussBoardManager to set.
*/
public void setDiscussBoardManager(DiscussBoardManager discussBoardManager) {
this.discussBoardManager = discussBoardManager;
}
public DisplayCommentsController() {
setCommandName("DiscussComment");
setCommandClass(DiscussBoard.class);
}
public ModelAndView handleRequestInternal(HttpServletRequest request,HttpServletResponse response)throws Exception{
List l = discussBoardManager.getDiscussBoards();
return new ModelAndView("disc_forum","list",l);
}
}
another controll class
this is my validator class
DiscussBoardValidator.java
package com.aikia.webapp.util;
public class DiscussBoardValidator implements Validator{
private Log log = LogFactory.getLog(DiscussBoardValidator.class);
public boolean supports(Class clazz) {
return clazz.equals(DiscussBoard.class);
}
public void validate(Object obj, Errors errors) {
if (log.isDebugEnabled()) {
log.debug("entering 'validate' method...");
}
DiscussBoard discussBoard = (DiscussBoard) obj;
ValidationUtils.rejectIfEmptyOrWhitespace(errors,
"userComment", "errors.required", "Value required.");
}
}
this is my applicationContext_service.xml
<bean id="discussBoardManager" parent="txProxyTemplate">
<property name="target">
<bean class="com.aikia.service.impl.DiscussBoardManagerImpl">
<property name="discussBoardDao" ref="discussBoardDao"/>
<property name="validator"><ref bean="DiscussBoardValidator"/></property>
<bean id="DiscussBoardValidator" class="com.aikia.webapp.util.DiscussBoardValidator"/>
<bean id="validatorFactory" class="org.springmodules.commons.validator.DefaultValidat orFactory">
<property name="validationConfigLocations">
<list>
<value>/WEB-INF/validator-rules.xml</value>
<value>/WEB-INF/validation.xml</value>
</list>
</property>
</bean>
and my jsp page is dis
</bean>
</property>
<bean id="userValidator" class="org.appfuse.web.UserValidator"/>
<!-- Override default transaction attributes b/c of UserExistsException -->
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED,-UserExistsException</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
my jsp page i s disc_forum.jsp
<%@ include file="/common/taglibs.jsp"%>
<%@ taglib uri="/web/WEB-INF/pager-taglib.tld" prefix ="pg" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><title>Aikia - Discussion Forums </title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="styles/custom.css" rel="stylesheet" type="text/css">
</head>
<body>
<td valign="top" width="80%">
<form name="inputForm" method="post" action="comment.html" onsubmit="return validateUser(this)">
<fmt:message key="DiscussComment.userComment"/>
<div id="commentInputContainer">
<table border="0" cellpadding="0" cellspacing="0">
<tbody><tr>
<td rowspan="3" style="padding-right: 40px;">
<spring:bind path="DiscussComment.userComment">
<span class="fieldError"><c:out value="${status.errorMessage}"/></span>
<input type="textarea" name="userComment" value="<c:out value="${status.value}"/>" id="userComment" cols="55" rows="4"align="" class="text large"/>
</spring:bind>
</td>
</tr>
<tr>
<br>
<td valign="bottom">
<input type="submit" class="button" value="Post" />
</tr>
</tbody></table>
</td>
</tr>
<tr><td> </td></tr>
</tbody></table>
</div>
</form>
<br><br>
</html>
<script type="text/javascript" src="<c:url value="/scripts/validator.jsp"/>"></script>
and validation .xml file is
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_1.dtd">
<form-validation>
<formset>
<form name="inputForm">
<field property="userComment" depends="required">
<arg0 key="DiscussComment.userComment"/>
</field>
</form>
</formset>
</form-validation>
and iam facing follwing errors when iam using spring bind in the jsp
javax.servlet.jsp.JspTagException: Neither Errors instance nor plain target object for bean name 'DiscussComment' available as request attribute
at org.springframework.web.servlet.tags.BindTag.doSta rtTagInternal(BindTag.java:118)
at org.springframework.web.servlet.tags.RequestContex tAwareTag.doStartTag(RequestContextAwareTag
and when i changing applicationcontext_service file it fshow the following error
at java.lang.Thread.run(Thread.java:595)
ERROR [http-8080-Processor25] .listenerStart(3767) | Exception sending context initialized event to listener instance of class com.aikia.webapp.listener.StartupListener
org.springframework.beans.factory.BeanDefinitionSt oreException: Line 58 in XML document from ServletContext resource [/WEB-INF/applicationContext-service.xml] is invalid; nested exception is org.xml.sax.SAXParseException: The content of element type "bean" must match "(description?,(constructor-arg|property|lookup-method|replaced-method)*)".
org.xml.sax.SAXParseException: The content of element type "bean" must match "(description?,(constructor-arg|property|lookup-method|replaced-method)*)".