Spring security - resource j_spring_security_check not avaiable - spring-mvc

I am trying to build a Spring based web application and I would like to start with configuring a simple authentication system based on username & password tuples stored in a database table.
It is my understanding that this can be easily achieved using Spring security, but I cannot get it to work.
The following is my web.xml file.
<?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">
<servlet>
<servlet-name>Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/Servlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Follows the servlet-context.xml file. The bob and sam users are there for testing purposes. After I get this right I will switch to a JDBC based user service.
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:sec="http://www.springframework.org/schema/security"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
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/security http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<sec:http use-expressions="true">
<sec:intercept-url pattern="/**" access="permitAll" />
<sec:form-login
login-page="/home.html"
login-processing-url="/j_spring_security_check"
authentication-failure-url="/login-error.html"
default-target-url="/welcome.html" />
<sec:logout logout-success-url="/home.html" />
</sec:http>
<sec:authentication-manager>
<sec:authentication-provider>
<sec:password-encoder hash="md5"/>
<sec:user-service>
<sec:user name="bob" password="12b141f35d58b8b3a46eea65e6ac179e" authorities="ROLE_SUPERVISOR, ROLE_USER" />
<sec:user name="sam" password="d1a5e26d0558c455d386085fad77d427" authorities="ROLE_USER" />
</sec:user-service>
</sec:authentication-provider>
</sec:authentication-manager>
<context:component-scan base-package="cz.dusanrychnovsky.whattoreadnext" />
<mvc:annotation-driven />
</beans>
This is my Home controller.
#Controller
public class HomeController
{
#RequestMapping(value = "/home.html")
public String home() {
return "home";
}
#RequestMapping(value = "/login-error.html")
public String loginError(Model model) {
model.addAttribute("loginError", true);
return "home";
}
}
And this is my thymeleaf based view.
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring3-3.dtd">
<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>Contacts</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<div id="content">
<h1>Welcome to the site!</h1>
<p th:if="${loginError}">Wrong user or password</p>
<form th:action="#{/j_spring_security_check}" method="post">
<label for="j_username">Email address</label>:
<input type="text" id="j_username" name="j_username" /> <br />
<label for="j_password">Password</label>:
<input type="password" id="j_password" name="j_password" /> <br />
<input type="submit" value="Log in" />
</form>
</div>
</body>
</html>
When I deploy the WAR file to my local Tomcat installation and visit the http://localhost:8080/test/home.html URL, the home page opens fine. When I fill in the form, though, which gets submitted to http://localhost:8080/test/j_spring_security_check, I get a 404 - The requested resource () is not available. error.
What am I doing wrong? Please bear with me as I'm a newcomer to both Spring MVC/Security and Thymeleaf.

You need to configure Spring Security filter in web.xml
You cannot configure Spring Security in servlet-context.xml, because servlet-context.xml belongs to specific DispatcherServlet, but Spring Security filter works before request reaches any servlet.
You need to create a root application context using ContextLoaderListener and put Spring Security configuration there.
Actually, as long as you don't need separate servlet-context.xml and applicationContext.xml, I'd suggest you to move everything from servlet-context.xml to applicationContext.xml and leave servlet-context.xml effectively empty (that is, leave its <beans> element empty).

Related

find error in code below spring mvc code

1.The origin server did not find a current representation for the target resource or is not willing to disclose that one exists." I am completely new to SpringMVC and not able to solve this 404 please help me so that i can move forward to learn the subject . I tried everything that i got on internet
index.jsp
<%response.setHeader("Cache-Control","no-cache"); //HTTP 1.1
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader ("Expires", 0);
%>
<html>
<body>
<form action="add">
<input type="text" name="t1"><br>
<input type="text" name="t2"><br>
<input type="submit">
</form>
</body>
</html>
first-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:ctx="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
<ctx:annotation-config></ctx:annotation-config>
<ctx:component-scan base-package="com.Tutorial"></ctx:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
web.xml
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>first</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>first</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
AddControllerTYpe.java
package com.Tutorial.FirstWebProject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class AddControllerType
{
#RequestMapping("/add")
public void add()
{
System.out.println("-----its here-----");
}
}

Tomcat 404 Http Not Found

I am learning Spring MVC and I build the first "hello world" example. But I can not reach the page. It says "The origin server did not find a current representation for the target resource or is not willing to disclose that one exists." I am using Tocat 9.0 (I also tried Tomcat 8 still not working). So here is my code below
Controller:
package com.emin.springdemo.mvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class HomeController {
#RequestMapping("/")
public String showPage() {
return "main-menu";
}
}
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>Insert title here</title>
</head>
<body>
<h2>Spring MVC Demo</h2>
</body>
</html>
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/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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- Step 3: Add support for component scanning -->
<context:component-scan base-package="com.emin.springdemo.mvc" />
<!-- Step 4: Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>
<!-- Step 5: Define Spring MVC view resolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
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>spring-mvc-demo</display-name>
<!-- Spring MVC Configs -->
<!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc-demo-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
I know there is a duplicate subjects in the site. I tried them also didn't reach the solution.
The problem might be the dash since it's not a valid a Java identifier Try renaming jsp and return fileName.
The page you're requesting from browser is not found, so you need to add URL in below pattern:
localhost:(Port number) /project name/(request mapping at controller) /(request mapping at method)
Try mapping URL and restart server.

error while loading bean with jsp pages loaded from <a:href>

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)

how to read form data in spring?

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

Spring MVC request not reaching Controller class

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.

Resources