Can't force Spring Web MVC 3.0 framework to work
I'm building a Web application with Java. Trying to integrate Spring Web MVC 3.0 framework. But I can't even force simply to show the page. I am using NetBeans 7.0 and Tomcat 6.0. This is what I've got: 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">
<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>
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: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:comp开发者_高级运维onent-scan base-package="forum.web" />
<!--
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>
<!-- org.springframework.web.servlet.view.InternalResourceViewResolver -->
<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>
<!--
The index controller.
-->
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
</beans>
Controller:
package forum.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
*
* @author
*/
@Controller
public class RegistrationController
{
@RequestMapping("/registration.htm")
public ModelAndView registrationWindow()
{
return new ModelAndView("registration");
}
}
XHTML page:
<tr id="log_reg">
<td>
<a href="login.htm"> Log in </a>
</td>
<td></td>
<td>
<a href="registration.htm"> Register </a>
</td>
</tr>
Running this command I see index page:
http://localhost:8088/Forum/index.htm
On the page I click the link, what suppose to open registration page. But instead is I see HTTP 404 error. The link:
http://localhost:8088/Forum/registration.htm
Found errors on NetBeans:
18-Jun-2011 14:45:19 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/index.htm] onto handler [org.springframework.web.servlet.mvc.ParameterizableViewController@1fb0fc2]
18-Jun-2011 14:45:20 org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'dispatcher': initialization completed in 1279 ms
18-Jun-2011 14:45:20 org.apache.catalina.core.StandardContext start
INFO: Container org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/Forum] has already been started
18-Jun-2011 14:45:35 org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/Forum/registration.htm] in DispatcherServlet with name 'dispatcher'
18-Jun-2011 14:51:53 org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/Forum/registration.htm] in DispatcherServlet with name 'dispatcher'
I can't release the cause of these errors. Code looks fine, in my opinion. I tried many things by this time. Any help? Best regards
Probably it's because you use both annotation mapping and SimpleUrlHandlerMapping.
Try to add bean:
<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotat ion.DefaultAnnotationHandlerMapping">
</bean>
and add it as a handler to your urlMapping bean:
<property name="interceptors">
<list>
<ref bean="handlerMapping"/>
</list>
</property>
It's easier to use only one of them, especially annotation based controllers. At this case you have to throw out your urlMapping bean, and instead of indexController bean use simple class like:
@Controller
class PagesCtrl {
@RequestMapping("/index.htm")
ModelAndView index() {
ModelAndView mav = new ModelAndView("index")
return mav
}
}
精彩评论