This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

spring mvc problem while doing Validation

 
murwath ali
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using the Spring "form" tag library. That is what I use and never seen this issue come up.

Mark
 
murwath ali
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
Sree Kumari
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for this post I was stuck up with this error and now it is solved.
 
Ramesh Sengani
Ranch Hand
Posts: 36
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic