I’m new bee to spring. Just started my sample application in sprinv mvc. But, I can’t able to view the page since it is showing “The requested resource () is not available.” Cannot figure out where is the problem. I’m pasting the code below.
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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" >
<servlet>
<servlet-name>my</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>my</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>
</web-app>
**
my-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-2.5.xsd">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean name="/index.html" class="mypackage.web.myController"/>
</beans>
**
MyController.java
**
package mypackage.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class myController implements Controller{
public ModelAndView handleRequest(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
String msg="Hello!!! I'm coming from Controller. You Catched me ";
ModelAndView mv = new ModelAndView("index");
mv.addObject("message",msg);
return mv;
}
}
index.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" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>My First Application in Spring</title>
</head>
<body>
<p>Check Below</p>
<p>
<em>${message}</em>
</p>
</body>
</html>
It’s almost configured correctly, so well done so far 🙂 There are a couple of minor problems here that are causing the problems you see. Firstly, the bean is currently defined with a lowercase
m:Although this is allowed, it is not conventional, so Spring will not be able to find the correct bean without some additional configuration.
Also, it was not clear from the question which URL you are using, but it should be something of the form
http://localhost:8080/<project>/myIndex.htmlThere is a good summary of the convention here.
So we have 2 options… either rename the class to
MyControllerand save asMyController.javaor modify theControllerClassNameHandlerMappingbean to be case sensitive like so:Furthermore, it is not the cause of the problem but if you use the
ControllerClassNameHandlerMappingyou can omit bean name, so you can just use:I guess the most annoying part is that the web application deploys without error. However if you examine the log, there is a marked difference:
Deployment of incorrectly configured webapp:
Deployment of correctly configured webapp:
Secondly, once the mapping is fixed, you may discover that the JSP is not found. In the sample I created, I added the views under
/WEB-INF/jspso I needed to update the prefix property inmy-servlet.xmlto<property name="prefix" value="/WEB-INF/jsp/"/>. However depending on the location of your views, you may not need to do this.Personally I find the annotation based approach for MVC in Spring much easier to configure and follow, so I will recommend that you read REST in Spring 3: @MVC as you might find that easier to implement.