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/200开发者_高级运维1/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
:
<bean name="/index.html" class="mypackage.web.myController"/>
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.html
There is a good summary of the convention here.
So we have 2 options… either rename the class to MyController
and save as MyController.java
or modify the ControllerClassNameHandlerMapping
bean to be case sensitive like so:
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
<property name="caseSensitive" value="true" />
</bean>
Furthermore, it is not the cause of the problem but if you use the ControllerClassNameHandlerMapping
you can omit bean name, so you can just use:
<bean class="mypackage.web.MyController"/>
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:
04-Jul-2011 09:13:58 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@5f0e7d: defining beans []; root of factory hierarchy
04-Jul-2011 09:13:58 org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'my': initialization completed in 157 ms
Deployment of correctly configured webapp:
04-Jul-2011 09:15:33 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@de537: defining beans [org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping#0,viewResolver,mypackage.web.MyController#0]; root of factory hierarchy
04-Jul-2011 09:15:49 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/my*] onto handler 'mypackage.web.MyController#0'
04-Jul-2011 09:15:49 org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'my': initialization completed in 296 ms
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/jsp
so I needed to update the prefix property in my-servlet.xml
to <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.
i was having the same problem because i was following step by step a tutorials thats up on the Netbeans official website, there it says and i quote "note that the JSTL (JavaServer Pages Standard Tag Library) library is added to the classpath during project creation by default. Deselect this option (as in the above screenshot), since you do not require JSTL for this tutorial. " once i tried leaving it checked i could be able to run my sample project, i really dont know anything more, hope this helps someone!
精彩评论