PDA

View Full Version : Problem with the Spring application: JspTagException



mike.tihonchik
Aug 31st, 2009, 04:23 PM
I am new to Spring MVC, and I can't seem to figure out what is the problem. I get the following exception: Neither BindingResult nor plain target object for bean name 'loginUser' available as request attribute. Here are my files: web.xml, ...-servlet.xml and LoginController.java


<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListe ner</listener-class>
</listener>
<servlet>
<servlet-name>i550imports</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/i550imports-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>i550imports</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>


<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">

<bean name="loginController" class="controller.LoginController">
<property name="sessionForm" value="true" />
<property name="commandName" value="loginUser" />
<property name="commandClass" value="controller.LoginUser" />
<property name="formView" value="login" />
<property name="successView" value="transactions" />
</bean>

<bean name="transactionsController" class="controller.TransactionListController">
<property name="commandName" value="transactionSearchCriteria" />
<property name="commandClass" value="controller.TransactionSearchCriteria" />
<property name="formView" value="transactions" />
<property name="successView" value="transactions" />
<property name="transactionService" ref="transactionService" />
</bean>

<bean id="defaultHandler" class="org.springframework.web.servlet.mvc.Parameterizabl eViewController">
<property name="viewName" value="login" />
</bean>

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlH andlerMapping">
<property name="mappings">
<map>
<entry key="/login" value-ref="loginController" />
<entry key="/transactions" value-ref="transactionsController" />
</map>
</property>
<property name="defaultHandler" ref="defaultHandler"/>
</bean>

<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewR esolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>


package controller;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormCont roller;

public class LoginController extends SimpleFormController {

@Override
protected ModelAndView onSubmit(Object command) throws Exception {
LoginUser login = (LoginUser) command;
ModelAndView results = new ModelAndView(getSuccessView());
results.addObject("loginData", login);
return results;
}
}


I think, I messing up in the web.xml or ...-servlet.xml while defining my urlMapping, because if I change the following line to:


<entry key="/" value-ref="loginController" />
the login page come up. I also tried to something like this in web.xml:

<servlet-mapping>
<servlet-name>i550imports</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
but then I get following exception:
16:13:24,217 ERROR [[i550imports]] Servlet.service() for servlet i550imports threw exception
java.lang.StackOverflowError
at org.apache.catalina.core.ApplicationHttpRequest.ge tAttribute(ApplicationHttpRequest.java:210)
at org.apache.catalina.core.ApplicationHttpRequest.ge tAttribute(ApplicationHttpRequest.java:222)
at org.apache.catalina.core.ApplicationHttpRequest.ge tAttribute(ApplicationHttpRequest.java:222)
at org.apache.catalina.core.ApplicationHttpRequest.ge tAttribute(ApplicationHttpRequest.java:222)
at org.apache.catalina.core.ApplicationHttpRequest.ge tAttribute(ApplicationHttpRequest.java:222)
at org.apache.catalina.core.ApplicationHttpRequest.ge tAttribute(ApplicationHttpRequest.java:222)

Thank you in advance, any help would be appreciated.

jamestastic
Aug 31st, 2009, 06:42 PM
This post (http://earldouglas.com/node/16) will walk you through setting up a simple Spring MVC instance with form handling annotation configuration, and will be a bit more up to date than what you're working with.

mike.tihonchik
Aug 31st, 2009, 10:26 PM
Thank you very much, I will read and try it... :) So, if I understand right, using annotations is a better solution?

jamestastic
Sep 1st, 2009, 12:04 AM
You're welcome, and please let me know if you have any questions, or if there are further details I can add.

I wouldn't say annotations are necessarily "better", but I generally prefer to use them when possible. They tend to make configuration easier, and they feel more intuitive than XML configuration. They also tend to significantly reduce the amount of XML you have to maintain. In general, the trend in the Java world seems to be moving toward annotations, but as with all things it's up to you to decide which is the best solution for the particular problem you are working on.