PDA

View Full Version : Spring validator problem


kuanfai
May 23rd, 2007, 03:47 AM
Hi, I am using spring 1.2.9 with WSAD5.1. I bind the error messages to the view, but it doesn't show up although the validator does catch the error. Following are my codes:

user-servlet.xml

<bean id="userValidator" class="com.sph.xd.validator.UserValidator"/>
<bean id="userController" class="com.controller.UserController">
<property name="sessionForm"><value>true</value></property>
<property name="commandName"><value>user</value></property>
<property name="commandClass"><value>com.bean.User</value></property>
<property name="validator"><ref bean="userValidator"/></property>
<property name="formView"><value>User</value></property>
<property name="successView"><value>UserUpdate</value></property>
</bean>


User.jsp

Firstname:
<spring:bind path="user.firstname">
<INPUT type="text" id="firstname" name="firstname" value="<c:out value="${status.value}" />">
<c:out value="${status.errorMessage}"/>
</spring:bind>


UserValidator.java

public class UserValidator implements Validator {

public boolean supports(Class clazz) {
return clazz.equals(User.class);
}

public void validate(Object command, Errors errors) {
1 logger.debug("[UserValidator.validate");
2 if (user.getFirstname() == null || user.getFirstname().equals("")) {
3 logger.debug("[Firstname is not set]");
4 errors.rejectValue("firstname", "required", null, "Value required.");
}
}

}


the line 1 and line 3 message does appear in the console while i put nothing in the firstname field, but it does not show in the form (user.jsp).

is there anything i missed out?

kuanfai

Marten Deinum
May 23rd, 2007, 06:42 AM
The only thing wrong with your code that I can see is that you use a static name 'firstname' instead of the desired ${status.expression}.

Also if you use the formview User there should be a User.jsp instead of a user.jsp.

The only thing that I can think of which is wrong here is that you have some weird custom controller implementation.

kuanfai
May 23rd, 2007, 07:02 AM
The only thing wrong with your code that I can see is that you use a static name 'firstname' instead of the desired ${status.expression}.

Also if you use the formview User there should be a User.jsp instead of a user.jsp.

The only thing that I can think of which is wrong here is that you have some weird custom controller implementation.

hi mdeinum, thanks for your reply. i tried with the code:

Firstname:
<spring:bind path="user.firstname">
<INPUT type="text" id="<c:out value="${status.expression}"/>" name="<c:out value="${status.expression}"/>" value="<c:out value="${status.value}" />">
<span class="fieldError"><c:out value="${status.errorMessage}"/></span>
</spring:bind>


but still, it doesn't give the error message.

by the way, i have the messages.properties file located in /WEB-INF/classes

required=Entry required.

kuanfai
May 23rd, 2007, 11:39 PM
Is there anyone know how to solve this problem?

Marten Deinum
May 24th, 2007, 02:56 AM
How have you configured your MessageSource? For starters you could try setting the property 'useCodeAsDefaultMessage' to true. That way when nothing is to be found you will see the code instead of the error message or nothing.

Also try wrapping your ${status.errorMessage} inside a spring:hasBindErrors tag this might help.


<spring:hasBindErrors name="user.firstname">
<c:forEach items="${errors}" var="error">
<c:out value="${error}"/>
</c:forEach>
</spring:hasBindErrors>


Although I expect the code above to deliver the same results I suggest you try it. As I mentioned in my previous post is that the only other thing I can imagine is that you have some weird behavior in your controller, can you post its code.

kuanfai
May 24th, 2007, 03:27 AM
Hi Marten:

My controller configuration is as below:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

<bean id="userValidator" class="com.validator.UserViewUpdateValidator"/>
<bean id="userLoginController" class="com.controller.UserLoginController">
<property name="sessionForm"><value>true</value></property>
<property name="commandName"><value>user</value></property>
<property name="commandClass"><value>com.bean.User</value></property>
<property name="validator"><ref bean="userValidator"/></property>
<property name="formView"><value>secure/user/User</value></property>
<property name="successView"><value>secure/user/User</value></property>
<property name="errorView"><value>defaultError</value></property>
</bean>


<!-- URL Mapping definition -->
<bean id="simpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlH andlerMapping">
<property name="mappings">
<props>
<prop key="/User.html">userLoginController</prop>
</props>
</property>
</bean>

<!-- View Resolver definition -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResou rceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix"><value>/WEB-INF/jsp/</value></property>
<property name="suffix"><value>.jsp</value></property>
</bean>

<bean id="messageSource" class="org.springframework.context.support.ResourceBundle MessageSource">
<property name="basename"><value>messages</value></property>
<property name="useCodeAsDefaultMessage"><value>true</value></property>
</bean>
</beans>


I tested the way you mentioned above, but still it shows nothing while the validator catches the error.

I have been bothering by this strange error few days, but could not find any solutions to it yet. I am wondering whether using status.errorMessage is the only way to display the error? Is there any other way to access the Errors object in the JSP page?

Marten Deinum
May 24th, 2007, 03:35 AM
I would like to see the code of your UserController the configuration we had already :).

kuanfai
May 24th, 2007, 03:46 AM
I would like to see the code of your UserController the configuration we had already :).

sorry, here it is:

public class UserController extends SimpleFormController {
private XdAdminDB dbcon;
private String errorView;
private Logger logger = Logger.getLogger(UserController.class);

public UserLoginController() {
setCommandName("user");
setCommandClass(User.class);
}

protected Object formBackingObject(HttpServletRequest request) throws Exception {

logger.debug("[UserController.formBackingObject]");

try {
User user = new User();
/*code to search user with user's login id*/
return user;

} catch (Exception ex) {
ex.printStackTrace();
request.setAttribute("exception", ex.toString());
return null;
}
}


protected Map referenceData(HttpServletRequest request, Object command, Errors errors) throws Exception {
Map referenceData = new HashMap();

logger.debug("[UserController.referenceData]");

referenceData.put("salutationMap", DropDownListConfig.getSalutationMap());
referenceData.put("genderMap", DropDownListConfig.getGenderMap());
referenceData.put("countryMap", DropDownListConfig.getCountryMap());
referenceData.put("zoneMap", DropDownListConfig.getZoneMap());
referenceData.put("maritalstatusMap", DropDownListConfig.getMaritalStatusMap());
referenceData.put("educationMap", DropDownListConfig.getEducationMap());
referenceData.put("occupationMap", DropDownListConfig.getOccupationMap());
referenceData.put("industryMap", DropDownListConfig.getIndustryMap());
referenceData.put("incomeMap", DropDownListConfig.getIncomeMap());
return referenceData;
}


public XdAdminDB getDbcon() {
return dbcon;
}

public void setDbcon(XdAdminDB adminDB) {
dbcon = adminDB;
}

public String getErrorView() {
return errorView;
}

public void setErrorView(String string) {
errorView = string;
}

}

Marten Deinum
May 24th, 2007, 05:21 AM
Plain simple implementation. I'm a bit at a loss here.

I would attach a debugger to it, and debug the showForm method and handleRequestInternal method of the SimpleFormController. Maybe due to some settings in your environment something weird is happening.

kuanfai
May 24th, 2007, 05:33 AM
Thanks Marten, for taking the valuable time to debug....it is very weird...i am not sure whether it is because i used acegi....anyway i will try to find some other ways to display the error message instead of using the <spring:bind>.

kuanfai
May 24th, 2007, 10:45 PM
Hi Marten, i found the problem with my configuration now. it should because i was trying to use spring 2.0, but after i found it has some problem to integrate with WSAD5.1, i switched to spring 1.2.9 instead. however there are some libraries used (for example spring.jar) conflict. after i opened a new workspace and using the old codes with the newly imported spring 1.2.9 libraries, everything seems fine now.

Also you mentioned useCodeAsDefaultMessage property needs to be set, otherwise it will give locale not found error.

Thank you so much for the valuable suggestions.
Cheers!

faisalloe
May 28th, 2007, 02:18 AM
i had same issue, i made trick some thing like this


<spring:hasBindErrors name="useremailform">
<font color="red"><core:out value="Must Agree"/></font>
</spring:hasBindErrors>


"Must Agree" will display every time error occurs instead reading from property file.

thanks
Faisal khan