Spring MVC - Getting constant 404 errors on Tomcat - java

I've created a basic Spring MVC web app with Maven. However, I've been struggling with getting constant 404 errors when I'm accessing the /login URL.
The /login URL should be mapped to the AuthController servlet and the createLoginForm() method, but unfortunately it fails and I'm ending up with a 404 error.
I'm trying to figure out what is issue that is causing 404 errors and why the /login URL cannot be mapped to the servlet.
Is the problem located in bad configuration of the web.xml or spring-dispatcher-servlet.xml files that prevent the URL to be mapped?
When I'm accessing the / URL, then the index.jsp file is mapped and it's is working fine
The login.jsp file is put outside the WEB-INF directory, in the webapp one as well as the index.jsp file.
Thanks in advance.AuthController.java
package com.github.wjoz.springmvcreview.auth;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class AuthController {
#RequestMapping(value="/login", method=RequestMethod.GET)
public ModelAndView createLoginForm() {
ModelAndView model = new ModelAndView("login");
return model;
}
}
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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web- app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>springmvcreview</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="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.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">
<mvc:default-servlet-handler/>
<context:component-scan base-package="main.java.com.github.wjoz.springmvcreview.auth" />
<mvc:annotation-driven />
<!-- Tells the location of the view in the project -->
<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>
login.jsp
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE>
<html>
<head>
<title>Welcome to our application. Sign in.</title>
</head>
<body>
<form action="/springmvcreview/login" method="post">
<div>
<label for="username">Username</label>
<input type="text" name="username" id="username">
<label for="password">Password</label>
<input type="password" name="password" id="password">
<button type="submit">Sign in</button>
</div>
</form>
</body>
</html>

This is in addition to Shazin's answer
First, put your JSP files inside WEB-INF. There is no reason to have them outside of it, since you're deliberately breaking your view rendering functionality.
Second,
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
means that the URL www.somedomain.com/ will load the index page, and all other URLs will start with that. However, your form
<form action="/springmvcreview/login" method="post">
is hitting the URL www.somedomain.com/springmvcreview/login. This does not match up with the controller URL mapping
#RequestMapping(value="/login", method=RequestMethod.GET)
public ModelAndView createLoginForm() {
does not contain a mapping for the form action. So either remove /springmvcreview from the form action, or modify #RequstMapping value to /springmvcreview/login

You don't have the contextConfigLocation for your DispatcherServlet. You need that to tell context:component-scan to scan the com.github.wjoz.springmvcreview.auth package and load the AuthController.
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value><PATH_TO>/spring-dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
I saw one more issue. You don't need the main.java part in the following;
<context:component-scan base-package="com.github.wjoz.springmvcreview.auth" />

Related

Spring MVC: No mapping found for HTTP request with URI [/hello.jsp]

I am trying to get started with Spring MVC & Tomcat, however I don't seem to be able to create single handler to render a minimalistic page.
The following files have been created:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="springapp" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd
http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>springapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springapp-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springapp</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
<display-name>Spring MVC Framework Test</display-name>
</web-app>
springapp-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/cache"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:cache="http://www.springframework.org/schema/c"
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/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<!--<mvc:annotation-driven />-->
<context:component-scan base-package="springapp.web.*"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean name="/hello.jsp" class="springapp.web.HelloController"/>
</beans>
hello.jsp
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>JSP Title</title>
</head>
<body>
${message}
</body>
</html>
HelloController.java
package springapp.web;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
#Controller
public class HelloController {
#RequestMapping(value="/hello", method=RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
}
Folder structure:
My attempts at finding the handler:
16-Aug-2016 16:31:18.867 WARNING [http-nio-8080-exec-8] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/springapp/hello.jsp] in DispatcherServlet with name 'springapp'
16-Aug-2016 16:31:22.991 WARNING [http-nio-8080-exec-9] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/springapp/web/hello.jsp] in DispatcherServlet with name 'springapp'
16-Aug-2016 16:31:23.023 WARNING [http-nio-8080-exec-10] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/springapp/web/hello.jsp] in DispatcherServlet with name 'springapp'
Can someone please tell me what I am doing wrong?
Your code should be like this :
Also you should run it using /springapp/hello instead of /springapp/hello.jsp
Controller :
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;
#Controller
#RequestMapping("/hello")
public class HelloController{
#RequestMapping(method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
}
web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>springapp</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springapp</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
HelloWeb-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="springapp.web" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
your jsp
<%# page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
As I can see your folder structure and the springapp-servlet.xml, I think you have to put hello.jsp file inside of WEB-INF folder because it could be the reason why can't find the page (Maybe show this type of error 404). According with springapp-servlet.xml you are indicating your jsp file should be inside WEB-INF folder.

DispatcherServlet is not getting called?

<?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/j2ee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID"
version="2.4">
<servlet>
<servlet-name>welcome</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>welcome</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
index.jsp
<html>
<body>
<form action="add">
<input type="text" name="one"/><br>
<input type="text" name="two"/><br>
<input type="submit"/>
</form>
</body>
</html>
welcome-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.ashish.springMVC" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
controller
package com.ashish.springMVC;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class Java4sController {
#RequestMapping("/*")
public ModelAndView helloWorld() {
System.out.println("hello....");
String message = "Welcome to Java4s.com Spring MVC 3.2.x Sessions";
message += "<br>You Did it....!";
return new ModelAndView("welcomePage.jsp", "welcomeMessage", message);
}//ModelAndView closed
}
welcomePage
<html>
<body>
<font face="verdana" size="2">
${welcomeMessage}
</font>
</body>
</html>
I want to run this but the code is accessing DispatcherServlet, behaving like JSP.
I tried executing this but it gives 404- no resource /add found error. Please suggests some edit to make it run correctly.
The way you about it is to analyze the problem scenario and try to minimize the problem area
First check if the request is reaching the controller.
if the request is not reaching the controller then you need to figure out what could be the reason like is the welcome-servlet.xml placed in the correct location
If the request is reaching the controller then check if there is something going wrong inside the helloWorld() for things like you returning welcomePage.jsp in
new ModelAndView("welcomePage.jsp", "welcomeMessage", message); which should be new ModelAndView("welcomePage", "welcomeMessage", message); as the internalResourceViewResolver just needs the name.

Resource Not found getting error while making simple project?

I am making simple hello world project in spring using MVC .Hi Found this error resource not found .I take help from this link .
http://crunchify.com/hello-world-example-spring-mvc-3-2-1/
can copy call file .It open the first index.jsp contend But when I click the link it say resource not found .
Here is my error!
I copy all code .here is file code .
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>FirstWebProject</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>crunchify</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>crunchify</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
</web-app>
crunchify-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:context="http://www.springframework.org/schema/context"
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">
<context:component-scan
base-package="com.crunchify.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
controller
package com.crunchify.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class CrunchifyHelloWorld {
#RequestMapping("/welcome")
public ModelAndView helloWorld() {
String message = "<br><div align='center'>" + "<h1>Hello World, Spring 3.2.1 Example by Crunchify.com<h1> <br>";
message += "<a href='http://crunchify.com/category/java-web-development-tutorial/'>More Examples</a>";
return new ModelAndView("welcome", "message", message);
}
}
index.jsp
<html>
<head>
<title>Spring 3.2.1 MVC Series: Index - Crunchify.com</title>
</head>
<body>
<br>
<div align='center'>
<h2>
Your 1st Spring MCV Example <br> <br>
Click here to See Welcome Message...
</h2>
<br> by Crunchify.com
</div>
</body>
</html>
welcome.jsp
<html>
<head>
<title>Spring 3.2.1 MVC Example: Hello World - Crunchify.com</title>
</head>
<body>
${message}
</body>
</html>
The url is checking for "welcome.html" instead of "welcome.jsp"
<servlet-mapping>
<servlet-name>crunchify</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
index.jsp
<html>
<head>
<title>Spring 3.2.1 MVC Series: Index - Crunchify.com</title>
</head>
<body>
<br>
<div align='center'>
<h2>
Your 1st Spring MCV Example <br> <br>
Click here to See Welcome Message...
</h2>
<br> by Crunchify.com
</div>
</body>
</html>
Whenever you use to create a web application, make an entry for welcome file list in your web.xml file
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
follow given tutorial carefully and make following changes
web.xml
<url-pattern>*.html</url-pattern>
read about this here
index.jsp
Click here to See Welcome Message...
when you click on this link new GET request will be generated and redirected to mapped controller
Controller
#Controller
#RequestMapping("/welcome")
public class CrunchifyHelloWorld {
#RequestMapping(method = RequestMethod.GET)
public String helloWorld(ModelMap model, HttpServletRequest request) {
String message = "<br><div align='center'>" + "<h1>Hello World, Spring 3.2.1 Example by Crunchify.com<h1> <br>";
message += "<a href='http://crunchify.com/category/java-web-development-tutorial/'>More Examples</a>";
//add message into module
model.addAttribute("message", message);
//name of jsp to show
return "welcome";
}
}
then your helloWorld() method will get called which will add your message into ModelMap and send request to welcome.jsp file.
hope this will help you!

HTTP Status 404 - /LoginAuth

I am using spring contoller annotation.
but when running my application getting HTTP Status 404 - /LoginAuth error
my files are
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID"
version="2.5"
xmlns="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" >
<display-name>SpringTest</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<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>*.do</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
</web-app>
index.jsp
in this file after clicking on LogIn button it will call LoginController.
<%# 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>
<form action="LoginAuth" method="get">
<label>Enter name :</label>
<input type="text" name="uname"><br>
<label>Enter pass :</label>
<input type="password" name="pass"><br>
<input type="submit" value="LogIn">
</form>
</body>
</html>
dispacher-servlet.xml
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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">
<bean
id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property
name="prefix"
value="/WEB-INF/JSP/" />
<property
name="suffix"
value=".jsp" />
</bean>
<context:component-scan base-package="com.controller" />
</beans>
LoginController.java
package com.controller;
import org.apache.catalina.connector.Request;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class LoginController {
#RequestMapping(value="/LoginAuth",method=RequestMethod.GET)
public ModelAndView loginAuth(Request req){
String uname=req.getParameter("uname");
String pass=req.getParameter("pass");
ModelAndView mav=new ModelAndView();
if(uname.equals("Alk") && pass.equals("Alk1234")){
mav.setViewName("success");
}else{
mav.setViewName("error");
}
return mav;
}
}
please help me.
There are a few issues with your setup.
First, the dispatcher servlet will automatically load the web application context - as defined by dispatcher-servlet.xml. You do not need to specify this in the contextConfigLocation for the ContextLoaderListener.
Since it doesn't look as though you have a root application context (which would normally be loaded by the ContextLoaderListener), then you can remove the following from your web.xml:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
and
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
Second, your dispatcher servlet is mapped to *.do but your controller and form action are both omitting the .do. Change your form and controller mapping to LoginAuth.do
Third, you should add <mvc:annotation-driven/> to your dispatcher-servlet.xml which activates the #RequestMapping annotations and tells Spring to register a bunch of other fairly useful beans behind the scenes.
For this, the mvc namespace should be added to the top of your dispatcher-servlet.xml:
xmlns:mvc="http://www.springframework.org/schema/mvc"
with a schema location of:
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
Give that a try and see how you get on.

Spring MVC why is data from my model not displaying in output.jsp?

I'm using Spring MVC. Why is data from my model not displaying in output.jsp?. I am working on a very easy example of data coming out on one page and being display on the 2nd back. I checked to see if the data is in the model and it is but I don't know why the 2nd JSP is not displaying it.
Here is my control:
#Controller
public class RequestController {
private static final Logger LOGGER = getLogger(RequestController.class);
#RequestMapping(value = "/request" , method = RequestMethod.GET)
public ModelAndView displayRequestPage(#ModelAttribute("inputForm") InputForm inputForm) {
return new ModelAndView("input");
}
#RequestMapping(value = "/request" , method = RequestMethod.POST)
public ModelAndView displayOutputPage(#ModelAttribute("inputForm") InputForm inputForm) {
Map<String, Object> model = new HashMap<String, Object>();
model.put("inputForm", inputForm);
return new ModelAndView("display", model);
}
}
here is my output JSP:
<%# taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%# taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>JQuery Validation Engine</title>
</head>
<body>
<center>
<h2>JQuery Examples</h2>
</center>
<p>Name: <c:out value="${inputForm.name}"/>
<p>Phone(xxx)xxx-xxxxx: <c:out value="${inputForm.phone}"/>
<p>Email: <c:out value="${inputForm.email}"/>
<p>
<b>Please click here to restart</b>
</p>
</body>
</html>
this is what I am getting as output:
Name: ${inputForm.name}
Phone(xxx)xxx-xxxxx: ${inputForm.phone}
Email: ${inputForm.email}
Please click here to restart
Here is my Spring MVC.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.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.0.xsd">
<mvc:annotation-driven />
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"
p:basenames="messages" />
<!-- Declare the Interceptor -->
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"
p:paramName="locale" />
</mvc:interceptors>
<mvc:resources mapping="/static/**" location="/WEB-INF/static/"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
I got it working but changing my web.xml.. Below is the working 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"
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>JQuery Example Web Application</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
and here is the web.xml that did not work:
<!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 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="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>JQuery Example Web Application</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Can someone please tell me why change the web.xml made it work.
Your model is only populated for POST requests (in displayOutputPage), but nowhere in your JSP do you submit a form. Therefore, you only ever call displayRequestPage and never provide a model (a <a href creates a GET request).
Furthermore, because you are not submitting a <form>, your inputForm will not have any data in it.
To check this, you can always set breakpoints and see which method is called.
EDIT:
I made the above assumption because you have the following link to restart the process:
<b>Please click here to restart</b>
And as your mapping for displayRequestPage is /request... As to the EL not being evaluated, it may just be that because you don't have an inputForm as you have no model and therefore the EL doesn't evaluate it just prints the text as is.
EDIT2:
The EL expressions did not work for you previously because Servlet Spec 2.3 does not support EL (was introduced with 2.4). As you had declared your web.xml to be using spec 2.3, it simply switched off the overhead of parsing for EL.
EDIT3:
Just to clarify the line
The line
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
told your servlet container to use spec 2.3
Try adding a model parameter to your controller methods and try to populate that model rather tha creating a new one.
See also: Basic Spring MVC Data Binding
A couple of things that could be wrong:
Make sure InputForm has getters/setters and a no-arg constructor
Try using Spring's bind tag or form tag.
Try just manually adding InputForm to the model (ie model.put()).
If your expecting InputForm to be data bound to the request you might need to use #Valid... you shouldn't but it would not hurt.
Finally on a successful POST you almost always want to redirect and not serve the response directly. If its an error you should serve the response.

Categories