• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1704
  • Last Modified:

Spring JSP not showing Error messages

I have a JSp in which I I used Spring:bind and spring:hasBindErrors tags. I am not able to display the error messages on the screen. Moreover I am not knowing where to write the properties as we write in resources.properties file.
//////////////////////
///LogonValidator.java
//////////////////////

package com.addrbk.valid;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.addrbk.domain.Login;
import com.addrbk.domain.UserBean;


public class LogonFormValidator implements Validator
{
        public boolean supports(Class clazz) 
        {
                return clazz.equals(Login.class);
        }

        public void validate(Object obj, Errors errors) 
        {
                Login user = (Login) obj;
                ValidationUtils.rejectIfEmpty(errors, "userid", "error.login.invalid.user", "UserID Required");
                ValidationUtils.rejectIfEmpty(errors, "passwd", "error.login.not.specified", "Password Required");
                if (user == null) 
                {
                        errors.rejectValue("userid", "error.login.not.specified", null,"Value required.");
                } 
                else 
                {
                        if (user.getUserid()== null || user.getUserid().trim().length() <= 0) 
                        {
                                System.out.println("user name null value");
                                errors.rejectValue("userid", "error.login.invalid.user",
                                null, "Username is Required.");
                        } 
                        else 
                        {
                                if (user.getPasswd()== null || user.getPasswd().trim().length() <= 0) 
                                {
                                        errors.rejectValue("passwd", "error.login.invalid.pass",
                                        null, "Password is Required.");
                                }
                        }               
                }
        }
}
//////////////////////////////////////////
///////////////////
//////LogonForm.jsp
///////////////////
<%@ taglib prefix="core" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>


<%@ taglib prefix="core" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>


<html>
	<head><title>techfaq360 Spring Validation Example</title></head>
	<body>
	
	<center>
	
	<h1>techfaq360 Spring Validation Example</h1>
	<br/>
	<form method="post" action="/addressbook/logonForm.do">
	<table width="25%" border="1">
	<tr>
	<td align="center" bgcolor="lightblue">Log on</td>
	</tr>
	<tr>
	<td>
	<table border="0" width="100%">

		<tr>
			<td width="33%" align="right">Username: </td>
			<td width="66%" align="left">
				<spring:bind path="userBean.userid">
					<input type="text" name="userid"/>
				</spring:bind>
			</td>
		</tr>
		<tr>
			<td colspan="2" align="center">
				<spring:hasBindErrors name="userBean.userid">
					<font color="red">
					  e1<c:out  value="${status.errorMessage}"/><br>
					  	
					</font>
				</spring:hasBindErrors>
			</td>
		</tr>
		<tr>
			<td width="33%" align="right">Password: </td>
			<td width="66%" align="left">
				<spring:bind path="userBean.passwd">
					<input type="password" name="passwd" />
				</spring:bind>
			</td>
		</tr>
		  <tr>
			<td colspan="2" align="center">
				<spring:hasBindErrors name="userBean.passwd">
					<font color="red">
					e2: <core:out value="${status.errorMessage}"/>
					</font>
				</spring:hasBindErrors>
			</td>
		</tr>
		
		<tr>
			<td align="center" colspan="2">
				<input type="submit" alignment="center" value="Submit">
			</td>
		</tr>
	</table>
	
	</td>
	</tr>
	</table>
	
	</form>
	<!-- 
	<form method="post" action="/addressbook/logonForm.do">
	<table width="25%" border="1">
	<tr>
	<td align="center" bgcolor="lightblue">Log on</td>
	</tr>
	<tr>
	<td>
	<table border="0" width="100%">
		<tr>
			<td width="33%" align="right">Username: </td>
			<td width="66%" align="left">
				<spring:bind path="userBean.userid">
					<input type="text"
						   name="userid"
						   value="<core:out value=""/>"/>
				</spring:bind>
			</td>
		</tr>
		<tr>
			<td colspan="2" align="center">
				<spring:hasBindErrors name="userBean.userid">
					<font color="red">
					  <c:out value="UID Error"/><br>
						
					</font>
				</spring:hasBindErrors>
			</td>
		</tr>
		<tr>
			<td width="33%" align="right">Password: </td>
			<td width="66%" align="left">
				<spring:bind path="userBean.passwd">
					<input type="password" name="passwd" />
				</spring:bind>
			</td>
		</tr>
		  <tr>
			<td colspan="2" align="center">
				<spring:hasBindErrors name="userBean.passwd">
					<font color="red">
					Error2: <core:out value="${status.errorMessage}"/>
					</font>
				</spring:hasBindErrors>
			</td>
		</tr>
		
		<tr>
			<td align="center" colspan="2">
				<input type="submit" alignment="center" value="Submit">
			</td>
		</tr>
	</table>
	
	</td>
	</tr>
	</table>
	
	</form>
	 -->
	</center>
	
	</body>
</html>

////////////////////////////////////////////////
///addrbk-servlet.xml
///////////////////////////////////////////
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

  <!-- the application context definition for the springapp DispatcherServlet -->

          <!-- 
                <bean name="/login.htm" class="com.addrbk.web.LoginController"/>
          -->
  
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>        
    </bean>
        
        <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
                <property name="urlMap">
                        <map>
                                <entry key="/logonForm.do"><ref bean="logonController"/></entry>
                        </map>
                </property>
        </bean>
        
        <bean id="logonController" class="com.addrbk.ctrl.LogonController">
                <property name="sessionForm"><value>true</value></property>
                <property name="commandName"><value>userBean</value></property>
                <property name="commandClass"><value>com.addrbk.domain.Login</value></property>
                <property name="validator"><ref bean="logonFormValidator"/></property>
                <property name="formView"><value>logonForm</value></property>
                <property name="successView"><value>success</value></property>
        </bean>
                
        <bean id="propertyConfigurer" 
                  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
                <property name="location">
                   <value>/WEB-INF/jsp.properties</value>
                </property>
        </bean>
                
        <bean id="logonFormValidator" class="com.addrbk.valid.LogonFormValidator"/>  
        
</beans>

Open in new window

0
prsubject
Asked:
prsubject
  • 3
  • 2
  • 2
2 Solutions
 
prsubjectAuthor Commented:
I am also pasting the Controller class below

////////////////////////////
////////LogonController.java
////////////////////////////

package com.addrbk.ctrl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.addrbk.domain.Login;
import com.addrbk.domain.UserBean;


public class LogonController extends SimpleFormController{
	protected final Log logger = LogFactory.getLog(getClass());
	public Object formBackingObject(HttpServletRequest request) throws ServletException
	{
		Login backingObject = new Login();
		logger.info("<<< formBackingObject >>>");
		
		/* The backing object should be set up here, with data for the initial values
		* of the form’s fields. This could either be hard-coded, or retrieved from a
		* database.
		*/
		return backingObject;
	}

	public ModelAndView onSubmit(Object command) throws ServletException 
	{
		
		Login user = (Login)command;
		logger.info("Login form Submitted");
		logger.info("Username >>> :"+user.getUserid());
		logger.info("Password >>> :"+user.getPasswd());
		//Now you can validate to database
		return new ModelAndView("success");
	}



}

Open in new window

0
 
prsubjectAuthor Commented:
I have also placed messageSource bean in the addrbk-servlet.xml as below and myMessages.properties file in classes directory. Still no result



<bean id="messageSource"   
    class="org.springframework.context.support.ResourceBundleMessageSource">  
    <property name="basenames">  
        <list>
            <value>myMessages</value>
        </list>
    </property>
</bean>

Open in new window

0
 
Sathish David Kumar NArchitectCommented:
you<bean id="messageSource"  
    class="org.springframework.context.support.ResourceBundleMessageSource">  
    <property name="basename">  

            <value>myMessages</value>
     
    </property>
</bean>


just try now
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
siddagrlCommented:
Define your bean in applicationContext.xml (instead of addrbk-servlet.xml)

[
If that does not work, though it should, try defining the value as follows:
<value>/WEB-INF/classes/myMessages</value>
]
0
 
siddagrlCommented:
From bean, I meant "messageSource" bean...
0
 
Sathish David Kumar NArchitectCommented:
@siddagrl: for your information  no need to mention class path in xml file .

that mean >/WEB-INF/classes/ this is no need ..

if we mention  <value>myMessages</value> like this means it will check ur /WEB-INF/classes/ path only
so no need for mention ..
0
 
Sathish David Kumar NArchitectCommented:
Have got the solution ??
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now