*
The moose likes Other Application Frameworks and the fly likes spring mvc problem while  doing Validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "spring mvc problem while  doing Validation " Watch "spring mvc problem while  doing Validation " New topic
Author

spring mvc problem while doing Validation

murwath ali
Greenhorn

Joined: Dec 24, 2008
Posts: 4
javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean name 'employee' available as request attribute
________________________________________________________________________

In jsp i have,
<spring:bind path="credentials.propertyName">

In spring xml file i have command name as,
<property name="commandName">credentials</property>
<property name="commandClass">LoginCredentials</property>



In controller class i have code,

protected Object formBackingObject(HttpServletRequest req)
{
LoginCredentials loginCredentials=new LoginCredentials();
HttpSession ses=req.getSession(true);
ses.setAttribute("credentials",loginCredentials);
return loginCredentials;
}
I am getting below issue.

exception

org.apache.jasper.JasperException: javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean name 'credentials' available as request attribute
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:541)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:417)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)



As i konw many had asked this question previously. I checked previous mails ,but the issue not getting soled can any one help me to resolve the issue
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Are you using the Spring "form" tag library. That is what I use and never seen this issue come up.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
murwath ali
Greenhorn

Joined: Dec 24, 2008
Posts: 4
I am putting up my total code here,

loginForm.jsp:
______________

<%@ taglib uri="/spring" prefix="spring" %>

<form method="post" action="login.do">
<spring:bind path="credentials.username">
<input type="text" name="username"/>
</spring:bind>

<br>

<spring:bind path="credentials.password">
<input type="text" name="password"/>
</spring:bind>

<br>
<input type="submit"/>
</form>




web.xml :
_________

<!--web.xml-->

<web-app>

<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>



<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/ds-servlet.xml</param-value>
</context-param>

<servlet>
<servlet-name>ds</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet</servlet-class>

<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>ds</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>


<taglib>
<taglib-uri>/spring</taglib-uri>
<taglib-location>/WEB-INF/spring.tld</taglib-location>
</taglib>
</web-app>



ds-servlet.xml :
_______________


<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>


<bean id="Login" class="LoginController">
<property name="sessionForm">
<value>true</value>
</property>

<property name="validator">
<ref bean="LoginCredentialsValidator"/>
</property>

<property name="commandName">
<value>credentials</value>
</property>


<property name="commandClass">
<value>LoginCredentials</value>
</property>

<property name="formView">
<value>/WEB-INF/jsp/loginForm.jsp</value>
</property>
</bean>


<bean id="LoginCredentialsValidator" class="LoginCredentialValidator"/>

<bean id="suhm" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/login.do">
<ref bean="Login"/>
</entry>
</map>
</property>
</bean>

</beans>



LoginCOntroller.java :
______________________


/* LoginController.java */

import javax.servlet.http.*;
import org.springframework.web.servlet.*;
import org.springframework.web.servlet.mvc.*;

public class LoginController extends SimpleFormController {

public ModelAndView onSubmit( Object o)throws Exception
{
System.out.println("onsubmit *******");
LoginCredentials loginCredentials=(LoginCredentials)o;
System.out.println("LoginCOntroller .... "+loginCredentials.getUsername());
return new ModelAndView("/success.jsp","credentials",loginCredentials);
}
protected Object formBackingObject(HttpServletRequest req)
{
System.out.println("formBackingObject*******");
LoginCredentials credentials=new LoginCredentials();
credentials.setUsername("user");
credentials.setPassword("password");
HttpSession ses=req.getSession(false);
req.setAttribute("credentials",credentials);
ses.setAttribute("credentials",credentials);
return credentials;
}
}


LoginCredentials.java :
________________________

/* LoginCredentials.java */
public class LoginCredentials
{
String username,password;
public void setUsername(String username)
{
this.username=username;
}
public String getUsername()
{
return username;
}

public void setPassword(String password)
{
this.password=password;
}
public String getPassword()
{
return password;
}
}


LoginCredentialValidator.java :
________________________________

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
public class LoginCredentialValidator implements Validator
{
LoginCredentials loginCredentials;

public boolean supports(Class c)
{
return LoginCredentials.class.isAssignableFrom(c);
}

public void validate(Object commandObject,Errors errors)
{
ValidationUtils.rejectIfEmptyOrWhitespace(errors,"userName","Field is required");
ValidationUtils.rejectIfEmptyOrWhitespace(errors,"password","Field is required");

loginCredentials=(LoginCredentials)commandObject;
if((loginCredentials.getPassword()!="password") && (loginCredentials.getUsername()!="xyz"))
{
errors.reject("credentials provided are not correctd");
}

}

}
murwath ali
Greenhorn

Joined: Dec 24, 2008
Posts: 4
When I deploy the application in the server i get an exception ,

exception org.apache.jasper.JasperException: javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean name 'credentials' available as request attribute
ujjwal soni
Ranch Hand

Joined: Mar 28, 2007
Posts: 403
Hi,

The "javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean name 'credentials' available as request attribute" generally pops up when you directly access your jsp page which has binding to the controller.

To solve it, kindly access the URL according to the mapping in your spring confing file, access the controller first then it will certainly map to your jsp page automatically.

--------------------------------------------------------------------------------

Try accessing your application after deploying in tomcat like

http://localhost:8080/Validations/login.do

instead of

http://localhost:8080/Validations/loginForm.jsp

It will surely work !!!

Cheers!!!

Ujjwal B Soni

<baroda, india, gujarat>

<919998971048>


Cheers!!!
Ujjwal B Soni <baroda, gujarat, india> <+919909981973>
"Helping hands are better than praying lips......"
Sree Kumari
Ranch Hand

Joined: Aug 16, 2005
Posts: 50
Thanks for this post I was stuck up with this error and now it is solved.
Ramesh Sengani
Ranch Hand

Joined: Nov 23, 2011
Posts: 36
try this one.

http://javakeexample.blogspot.in/2012/12/spring-mvc-annotation-example.html
 
 
subject: spring mvc problem while doing Validation
 
Similar Threads
how to tell MultiActionController Command Object is Of type com.dss.ems.model.User
SprngFramework Form Binding Error
Problem with bindings
problem while loading the welcome page
Spring exception Neither BindingResult nor plain target object for bean name 'name' a