Why doesn't bean name of /home.jsp not work for BeanNameUrlHandlerMapping? - spring

In the following program, if I replace "/home.htm" with "/home.jsp" in dispatcher-servlet.xml and index.jsp, then the server is not able to find home.jsp. Are strings ending with ".jsp" invalid as bean name?
dispatcher-servlet.xml
<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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean name="/home.htm" class="com.sample.HomePageController" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
index.jsp
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Dynamic Tiles</title>
</head>
<body>
<form action="/home.htm" method="post">
Please enter your name:
<input type="text" name="visitorName" />
<input type="submit" value="Go"/>
</form>
</body>
</html>
HomePageController.java
package com.sample;
import org.apache.log4j.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class HomePageController extends AbstractController {
private static Logger logger = Logger.getLogger(HomePageController.class);
HomePageController () {
logger.info("Constructing HomePageController object");
};
public ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
logger.info("Handling request for home.jsp");
return new ModelAndView("home", "visitorName", request.getParameter("visitorName"));
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
version="2.5">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<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>*.htm</url-pattern>
</servlet-mapping>
</web-app>

There are several pieces of your application that need to be modified for it to work as intended. The first piece is the Dispatcher Servlet mapping in web.xml. In Spring the dispatcher is used to route particular requests from the client to Spring controllers within the application. What requests are routed via the dispatcher is determined by the url-pattern specified in the web.xml file for the servlet: org.springframework.web.servlet.DispatcherServlet.
In your case we want to set the url-pattern to something that does not conflict with accessing your jsp files. Since you have specified a viewResolver in your Spring configuration that prefixes generic names with /WEB-INF/jsp/ and suffixes them with .jsp, we must use a url-pattern other than .jsp.
If the .jsp url-pattern were used, a request would first be processed by the dispatcher servlet and routed to any mapped controller. If the controller attempts to use a view name to navigate to another page, for example, home, the view name is prefixed/suffixed resulting in a url: /WEB-INF/jsp/home.jsp. This constructed URL matches the .jsp url-pattern specified for the dispatcher and an attempt to route /WEB-INF/jsp/home.jsp is made, most likely failing because no mapping is found.
To remedy this problem we setup your web.xml as follows:
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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<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>
</web-app>
As you can see, we map the dispatcher to the url-pattern *.do. This allows the dispatcher to be used to route requests without interference with common file prefixes.
Changing the url-pattern to *.do requires us to change the action on your form. I modified your form to the following:
Index.jsp
<%# page contentType="text/html;charset=UTF-8" language="java"%>
<html>
<head>
<title>Dynamic Tiles</title>
</head>
<body>
<form action="home.do" method="post">
Please enter your name: <input type="text" name="visitorName" />
<input type="submit" value="Go" />
</form>
</body>
</html>
I also assumed that all of your .jsp files are located within the WEB-INF directory. This requires us to use a controller to access each of these .jsp files. Mappings to the controllers must be created in your Spring configuration file for the dispatcher and the appropriate controller for index.jsp must be created. Notice that I am using the .do suffix for the mappings.
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="handlerMapping"
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<bean name="/home.do" class="com.sample.HomePageController" />
<bean name="/index.do" class="com.sample.IndexPageController" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
IndexPageController.java
package com.sample;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class IndexPageController extends AbstractController {
#Override
protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
return new ModelAndView("index");
}
}
Just to be thorough, I created a home.jsp file to test the application since one was not provided. Here is my home.jsp file:
Home.jsp
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%# page session="false" %>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>
Hello, ${visitorName}
</h1>
<P> The time on the server is ${serverTime}. </P>
</body>
</html>

You decided that dispatcher would only handle .htm-ended urls but then you want it to handle .jsp-ended url. For the latter to work just change url-pattern element's content in web.xml to, for example, something like this:
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>

Related

Can't use freemarker with Spring MVC 3

I created a simple Spring MVC 3 application and want to use freemarker template engine. I cofigure *-context.xml as describes in off Spring's docs, but in browser I get 404 Page not found error. this is my code:
HelloWorldController.java
#Controller
#RequestMapping("/hello")
public class HelloWorldController {
private static final Logger log = Logger.getLogger(HelloWorldController.class);
#RequestMapping(value="/{name}", method = RequestMethod.GET)
public String hello(#PathVariable String name, Model model) {
String result = "Hello, " + name;
model.addAttribute("result", result);
return "hello";
}
}
this is my hello.ftl in WEB-INF/freemarker folder
<!doctype html>
<html>
<head>
<title>Hello</title>
</head>
<body>
<h1>
Hello world!
</h1>
<P> The time on the server is ${result}. </P>
</body>
</html>
and 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/mvc/spring-mvc.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.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<!-- freemarker config -->
<beans:bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<beans:property name="templateLoaderPath" value="/WEB-INF/freemarker/"/>
</beans:bean>
<!-- 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.freemarker.FreeMarkerViewResolver">
<beans:property name="prefix" value="" />
<beans:property name="suffix" value=".ftl" />
<beans:property name="cache" value="false" />
</beans:bean>
<context:component-scan base-package="org.example.simple" />
</beans:beans>
what is wrong and why I get 404 when I go to localhost:8080/simple/hello/username ?
please, help
EDIT:
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>
</web-app>
its probably wrong configuration for spring mvc, not freemarker. Seems that spring cant find your Controller. Did you add dispatcherServlet in your web.xml?

Not able to run spring application using XML configuration

I am new to spring, trying to run the Spring application through XML configuration, but i am not getting any error in console. But the application is not running and i am getting the 404 error. I didn't add the servlet-api jar in WEB-INF/lib. Can anyone please help me? Thanks in advance.
package com.raistudies.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class HelloWorldAction extends AbstractController {
#Override
protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
System.out.println(" helloworld... ");
ModelAndView mav = new ModelAndView();
mav.setViewName("hello");
mav.addObject("helloMessage", "Hello World from My First Spring 3 mvc application with xml configuration...");
return mav;
}
}
hello.jsp - WEB-INF/jsp/
<html>
<head>
<title>Hello World with spring 3 MVC XML configuration</title>
</head>
<body>
<h1>Welcome! Spring MVC XML configuration is working well</h1>
${helloMessage}
</body>
</html>
index.jsp - WEB-INF
<html>
<head>
<title>rai studies</title>
</head>
<body>
Welcome...
<br>Click here to check the output :-)
</body>
</html>
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_3_0.xsd"
id="WebApp_ID_1" version="3.0">
<display-name>HWEWS3MVCIEA</display-name>
<servlet>
<servlet-name>SpringMVCDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/app-config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVCDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
app-config.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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<bean name="/hello.htm" class="com.raistudies.action.HelloWorldAction" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
libraries under the WEB-INF/lib
commons-logging-1.1.2.jar
jstl-1.2.jar
log4j-1.2.16.jar
spring-aspects-3.2.5.RELEASE.jar
spring-beans-3.2.5.RELEASE.jar
spring-context-3.2.5.RELEASE.jar
spring-core-3.2.5.RELEASE.jar
spring-expression-3.2.5.RELEASE.jar
spring-web-3.2.5.RELEASE.jar
spring-webmvc-3.2.5.RELEASE.jar
supportingLibrary under WEB-INF/supportingLibrary
servlet-api-2.5.jar
Your bean definition of the Action is not correct. You have to define a controller bean with some valid name like:
<bean name="helloWorldController" class="com.raistudies.action.HelloWorldAction" />
Then you need to add the url mapping definition to your configuration to map requests to the defined controller.
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello.htm">helloWorldController</prop>
</props>
</property>
</bean>

Cant able to run spring application?

I am new to spring i am trying to run the spring application, i am getting the DispatcherServlet class not found exception. The below is the code i used can anyone please help me? I tried many solution provided in stackoverflow and google but i didn't get answer for this problem. Thanks in advance.
package com.raistudies.actions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class HelloWorldAction {
#RequestMapping(value = "/hello", method = RequestMethod.GET)
public ModelAndView sayHello(Model model) {
System.out.println(" hellow world ");
ModelAndView mav = new ModelAndView();
mav.setViewName("hello");
model.addAttribute("helloMessage",
"Hello World from my spring 3 mvc application");
return mav;
}
}
hello.jsp - WEB-INF/jsp/
<html>
<head>
<title>Hello world with spring 3 mvc </title>
</head>
<body>
<h1>Welcome! Spring MVC is working well.</h1><br />
${helloMessage}
</body>
</html>
index.jsp - WEB-INF
<html>
<head>
<title>rai studies</title>
</head>
<body>
Welcome...
<br>Click here to check the output :-)
</body>
</html>
app-config.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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="com.raistudies.actions" />
<context:annotation-config />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<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://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_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>HWEWS3MVCIE</display-name>
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springfrmaework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/app-config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
My dependencies:
commons-logging-1.1.2.jar
jstl-1.2.jar
log4j-1.2.16.jar
spring-aspects-3.2.5.RELEASE.jar
spring-beans-3.2.5.RELEASE.jar
spring-context-3.2.5.RELEASE.jar
spring-core-3.2.5.RELEASE.jar
spring-expression-3.2.5.RELEASE.jar
spring-web-3.2.5.RELEASE.jar
spring-webmvc-3.2.5.RELEASE.jar
<servlet-class>org.springfrmaework.web.servlet.DispatcherServlet</servlet-class>
In above line there is spelling mistake. It should be
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

My Spring application does not open another page

I have created a Spring application using netbeans, I did not change any of configuration. In index.jsp I have a link to another page but it does not get to the second page and shows "The requested resource () is not available."
in the server console it shows the following warning
"WARNING: No mapping found for HTTP request with URI [/Myapp/emp.htm] in DispatcherServlet with name 'dispatcher'"
my emp.jsp file is in jsp folder.
index.jsp
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Welcome to Spring Web MVC project</title>
</head>
<body>
emp
</body>
</html>
dispatcher-servlet
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<!--
Most controllers will use the ControllerClassNameHandlerMapping above, but
for the index controller we are using ParameterizableViewController, so we must
define an explicit mapping for it.
-->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<!--
The index controller.
-->
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
<bean name="/emp.htm" class="controller.Employee"/> <<I changed this to "/Myapp/emp.htm" as well but does not work
</beans>
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.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.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
Employee.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package controller;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.IOException;
public class Employee implements Controller {
protected final Log logger = LogFactory.getLog(getClass());
#Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
logger.info("Returning hello view");
return new ModelAndView("emp.jsp");
}
}
Change the <a href> to get the actual url like this
<a href="<c:url value="/emp"/>" />
Also make sure that the Dispatcher servlet is mapped in your web.xml
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
More about Spring MVC implementation can be found here
I've solved it by adding
<prop key="emp.htm">empController</prop>
and
<bean name="empController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="emp" />
#Daniel
Yes, I can see that emp.jsp is a simple message jsp. The thing is this is how MVC pattern works(no thing to do with Spring). MVC is all about having a dispatcher sitting in front of all request and route the request to proper resources(e.g. controller,servlet, etc)
If the message in the jsp is dynamic generated, I reckon the request should go through a controller and forwarded to emp.jsp. If the whole point of emp.jsp is just some kinda of static page, you can put emp.js under webapp/static/emp.jsp and in your spring servlet configuration file indicate that everything under /static should by pass Spring ServletDispatcher.
The configuration file will look like this
<mvc:resources mapping="/static/**" location="/static/" />
By doing so, the request /static/emp.jsp will work and do not go through Spring DispatcherServlet.

The requested resource not available in spring mvc

I am trying to learn spring mvc and facing a problem (which seems to be a common one). I have searched a lot of solutions but nothing is helping me out...
My web.xml is:
<?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>Spring Hello World</display-name>
<welcome-file-list>
<welcome-file>hello.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>chatbooster</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>chatbooster</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
My chatbooster-servlet.xml is :
<?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"
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.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/" />
<property name="suffix" value=".jsp" />
</bean>
When I try to run hello.jsp, the error is the requested resource is not available.
Hello.jsp:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#taglib prefix="i" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Hello World!</h1>
<hr/>
<form action="hi">
Name: <input type="text" name="name"> <input type="submit" value="Submit">
</form>
</body>
</html>
HelloWorldController.ava
#Controller
public class HelloWorldController {
#RequestMapping("/")
public String hello() {
return "hello";
}
#RequestMapping(value = "/hi", method = RequestMethod.GET)
public String hi(#RequestParam("name") String name, Model model) {
String message = "Hi " + name + "!";
model.addAttribute("message", message);
return "hi";
}
}
Edit1:
The problem is occurring because of tomcat server as my simple html page is also not running and it is throwing the same exception. I am using tomcat server version 7. Can anyone hint me out the cause of this exception?
Tomcat doesn't know about hello.jsp since it is inside WEB-INF.
Change
<welcome-file-list>
<welcome-file>hello.jsp</welcome-file>
</welcome-file-list>
to
<welcome-file-list>
<welcome-file>/</welcome-file>
</welcome-file-list>
It will work.
This could be a the case of Hello.jsp. The first letter of Hello.jsp is capitalized yet in your controller you are returning a lower case hello. If you change your controller to return the string "Hello" it should work.
Thank you everyone for your answers. Actually I was able to solve my issue by just changing the location of html and jsp pages from web inf folder to web content folder. I dont know why it worked but pages are being run by the server now.

Resources