Neither BindingResult nor plain target object for bean name 'command' available as request attribute
this is my index.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%#taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form:form action="loginform" method="post">
<form:input type="text" path="name" id="userName" />
<form:input type="password" path="pass" id="password" />
<form:button value="submit"></form:button>
</form:form>
</body>
</html>
this is my Bean
package com.ews.usman.controller;
public class loginBean {
private String name;
private String pass;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
}
this is my Controller
package com.ews.usman.controller;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class LoginFormController {
#RequestMapping(value="/loginform", method=RequestMethod.POST)
public ModelAndView formReader(#ModelAttribute("loginBean") loginBean
loginbean, BindingResult result){
ModelAndView model = new ModelAndView();
model.addObject("name", loginbean.getName());
model.addObject("pass", loginbean.getPass());
model.setViewName("result");
return model;
}
}
this is my Servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing
infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up
static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by #Controllers to .jsp resources
in the /WEB-INF/views directory -->
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.ews.usman" />
<bean name="/loginform"
class="com.ews.usman.controller.LoginFormController"></bean>
</beans:beans>
this is my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- The definition of the Root Spring Container shared by all Servlets and
Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-
class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-
class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-
value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>`
You need to add a modelAttribute with a proper name to your form, otherwise the form backing bean name will default to 'command' hence you get your error
<form:form action="loginform" modelAttribute="loginBean" method="post">
Related
I am getting 404 error when i try to launch my hello.jsp page, index.jsp page is working fine. But when i try to change the address from "http://localhost:8080/springmvc" to "http://localhost:8080/springmvc/hello", i get "HTTP Status 404 – Not Found" error on webpage.
I am trying to read the 'hello.jsp' page from separate folder i.e views under "WEB-INF/views/hello.jsp".
I recreated the whole project to find if there was any mistake.
***[MY WEB.XML CODE]***
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Hello Spring MVC</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
***[MY DISPATCH-SERVLET.XML FILE CODE]***
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<context:component-scan base-package="com.thomas.spring.springmvc.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
name="viewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
***[MY CONTROLLER CLASS CODE]***
package com.thomas.spring.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class HelloController {
#RequestMapping("/hello")
public ModelAndView hello()
{
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("hello");
return modelAndView;
}
}
***[MY HELLO.JSP FILE CODE]***
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello</title>
</head>
<body>
<h1>Hello from Spring MVC!</h1>
</body>
</html>
I expect the output to be getting message "Hello from Spring MVC!" from "hello.jsp" file. But i am getting "HTTP Status 404- Not Found".
Console output is " Sep 12, 2019 1:51:38 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/springmvc/hello] in DispatcherServlet with name 'dispatcher' ".
No mapping found for HTTP request with URI [/SaturdayTest/welcome] in DispatcherServlet with name 'spring-dispatcher'
I can't find where the mistake is done.
HelloController.java
package com.prav.hellocontroller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class HelloController {
#RequestMapping("/welcome")
public ModelAndView helloWorld() {
ModelAndView model=new ModelAndView("HelloPage");
model.addObject("msg","hello world");
return model;
} }
HelloPage.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>${msg}<h1>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID"
version="3.1">
<display-name>SaturdayTest</display-name>
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
spring-dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.prav.hellocontroller">
</context:component-scan>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
checked the dispatcher and mapping, but the name is same.Where can be the issue.
Try adding:
#RequestMapping("SaturdayTest")
on top of your controller. Like:
#Controller
#RequestMapping("SaturdayTest")
public class HelloController {
Let me know it this works.
#RequestMapping("/SaturdayTest/welcome")
Try with this annotation on your method
I've seen this post with no solutions. I'm trying to run a Spring MVC tutorial but for some reason I'm not able to get the mapping in my servlet to call my home.jsp found in /WEB-INF/jsps directory. My context root is set to spring in Eclipse Web Project Settings. The error I'm getting is:
INFO 2016-11-06 11:18:59,613 [http-bio-8080-exec-3]
org.springframework.web.servlet.DispatcherServlet -
FrameworkServlet 'offers': initialization completed in 486 ms
WARN 2016-11-06 11:18:59,623 [http-bio-8080-exec-3]
org.springframework.web.servlet.PageNotFound
- No mapping found for HTTP request with URI [/spring/]
in DispatcherServlet with name 'offers'
My code is as follows:
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID"
version="2.5">
<display-name>spring-tutorial-50</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>offers</display-name>
<servlet-name>offers</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<web:load-on-startup>1</web:load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>offers</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
offers-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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-
3.2.xsd">
<context:component-scan base-package="com.mypackage.spring.web.controllers">
</context:component-scan>
<mvc:annotation-driven />
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsps/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
Controller:
#Controller
public class OffersController {
private Logger log = Logger.getLogger(OffersController.class);
#RequestMapping("/")
public String showHome(){
log.info("showHome() called");
return "home";
}
}
home.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Inside jsps</title>
</head>
<body>
Hi Mom!
</body>
</html>
You did NOT map /spring URI to the controller RequestMapping, look at the below code for the correct mapping:
#Controller
public class OffersController {
private Logger log = Logger.getLogger(OffersController.class);
//Map RequestMapping to /spring
#RequestMapping("/spring", method=RequestMethod.GET)
public String showHome(){
log.info("showHome() called");
return "home";
}
}
I just upgraded to Spring 3.2.17 for all of my Maven library references. When I cleaned and redeployed It worked! #RequestMethod("/") by itself will work if the server is configured properly. It is also possible to get more explicit on what a RequestMethod can handle with things like the following: #RequestMethod(value="/spring", method = RequestMethod.GET) as #javaguy lists. value = "/" etc. also works.
I have a jsp page called menu.jsp and I have given link to another page called ClsEdit.jsp by <a href="jsp/cls/ClsEdit.jsp?action=create">
When I give this line in menu.jsp, it loads without any problem. But when I give the same line in ClsEdit.jsp, I get the following exception. Please tell me what is the problem with the usage of links.
<form:hidden path="ukey" />
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/tradelc-servlet.xml
</param-value>
</context-param>
<listener>
<listener-class>com.bankofny.inx.omx.lc.web.util.SessionListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>tradelc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>tradelc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
tradelc-sevlet.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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:annotation-config />
<!-- Scans within the base package of the application for #Components to configure-->
<context:component-scan base-package="com.bankofny.inx.omx.lc.web" />
<!-- Enables the Spring MVC #Controller programming model -->
<mvc:annotation-driven />
<bean name="clsData" class="com.bankofny.inx.omx.lc.web.bean.ClauseData">
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="resources.application" />
</bean>
<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="/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
menu.jsp
<%# page language="java" errorPage="errors/general.jsp" %>
<%# taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%# taglib uri="/WEB-INF/tlc.tld" prefix="tlc" %>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<form:form action="/tradelc/ClsSave" method="POST">
<form:hidden path="ukey" />
<table width=100%>
<tr>
<td>
<a href="jsp/cls/ClsEdit.jsp?action=create">
Test clause link
</a href>
</td>
</tr>
</table>
</form:form>
Inside ClsEdit.jsp
<form:hidden path="ukey" />
ControllerJava.java:
#Controller
#SessionAttributes("clsData")//tried adding to sessions
public class ControllerJava{
#ModelAttribute("clsData")//mapped the model attribute
public ClauseData createBean() {
return new ClauseData();
}
#RequestMapping(value = "jsp/cls/ClsEdit")//tried mapping the link here, so that I can add the ‘bean’ to ‘command’ here, but the control did not come here at all, the SOP did not print
public ModelAndView returnClsEdit()
{
System.out.println("is it coming nside this method");
return new ModelAndView("ClsEdit");
}
#RequestMapping(value = "/informlogin", method = RequestMethod.GET)
public ModelAndView execute( HttpServletRequest request,
HttpServletResponse response,
#ModelAttribute("clsData") ClauseData clauseData,
BindingResult bindingResult)
throws Exception {
.
.
.
.
. return new ModelAndView("menu", "command", new ClauseData());
}
}
Exception:
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute
org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:168)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:188)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:154)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:141)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:132)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:116)
org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:422)
org.springframework.web.servlet.tags.form.HiddenInputTag.writeTagContent(HiddenInputTag.java:79)
org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:84)
org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80)
I know this question has already been asked before, but none of the solutions mentioned there seems to be working for me.
I'm writing a Spring MVC web application. But my request is not reaching the controller class.
Here are the relevant files.
Can anyone please help me by pointing out where I am going wrong? Thanks...
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>booksWorld</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:**/*applicationContext.xml</param-value>
</context-param>
</web-app>
dispatcher-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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- Declare a view resolver -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp"/>
</bean>
</beans>
applicationContext.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" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.booksworld.controllers" />
<context:annotation-config />
<mvc:annotation-driven />
<context:property-placeholder location="classpath:**/*config.properties" />
<!-- <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="resources/Messages" /> -->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" p:sessionFactory- ref="sessionFactory" />
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
<property name="packagesToScan" value="com.booksworld"></property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}"
p:username="${jdbc.username}" p:password="${jdbc.password}" >
</bean>
</beans>
My controller class
package com.booksworld.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class CommonController {
#RequestMapping("/proceed")
public String loadLoginPage() {
System.out.println("we reached here");
return "login";
}
#RequestMapping(value="/login")
public String login() {
return "abc";
}
}
index.jsp (the welcome jsp file)
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO- 8859-1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome All</title>
</head>
<body>
<p>Hello World</p>
<%response.sendRedirect("proceed.do"); %>
</body>
</html>
login.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %>
<%# taglib uri="http://www.springframework.org/tags/form" prefix="sf" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Books World - Login</title>
</head>
<body>
<sf:form method="POST" action="login.do" modelAttribute="Login">
<h3>Please enter your credentials</h3>
Login Name<sf:input path="userName" />
Password<sf:password path="password" />
</br>
</br>
<input type="submit" value="Log In" />
<input type="reset" value="Reset" />
</sf:form>
</body>
</html>
I'm developing in eclipse, and using Tomcat. the server log has no errors. Just the standard start-up log.
Please help me to point out where I am going wrong.
You haven't specified which URL you are hitting and what errors or output you are seeing so I will go by the files you have posted. There seem to be at least a few problems with what you have posted. I am assuming you are trying to access either http://[server]:[port]/index.jsp or http://[server]:[port]/.
Problem 1: When using an MVC framework you usually do not request web templates such as JSP files directly. Therefore, your welcome file list in web.xml should not contain index.jsp. It is more common to omit this section altogether. Instead, you should add a request mapping to the controller class for the application root, such as:
#RequestMapping("/")
public String home() { return "index"; }
Once you do this, you should be able to hit http://[server]:[port]/ and get a response.
Problem 2: The file index.jsp redirects to the URL proceed.do but there is no mapping for this URL in your controller. Instead, the controller has a mapping for the URL proceed. May be there was some confusion with Struts when you were trying to configure the URL mappings.
Problem 3: The file login.jsp submits to login.do but there is no mapping for this URL. Instead there is a mapping for login. The form action should therefore be changed from login.do to login.
Problem 4: The form in login.jsp is bound to a model attribute called Login but the controller does not add any model object with that name.
Problem 5: The form in login.jsp is submitted using an HTTP POST but the controller does not specify this for the login method. The default for controller methods annotated with #RequestMapping is HTTP GET so the login method will not be called when you submit the form.
Problem 6: The file index.jsp seems to be redundant as its sole purpose is to redirect to a different URL on the same server. You will be better off with the following simplified controller code:
#Controller
public class CommonController {
#RequestMapping("/")
public String loadLoginPage(Model model) {
model.addAttribute("Login", new Login()); // Or something like this, which represents the actual model object that should collect the login information.
return "login";
}
#RequestMapping(method = RequestMethod.POST, value="/login")
public String login(Login login) {
// Use the login object to authenticate the user.
return "abc";
}
}
I would also recommend that you keep the lines
<context:component-scan base-package="com.booksworld.controllers" />
<context:annotation-config />
<mvc:annotation-driven />
in the file dispatcher-servlet.xml as these lines are specific to your presentation layer.