2011-06-18 68 views
1

我正在用Java構建Web應用程序。試圖整合Spring Web MVC 3.0框架。但我甚至無法簡單地展示頁面。我正在使用NetBeans 7.0和Tomcat 6.0。 這是我的本錢: 在web.xml無法強制Spring Web MVC 3.0框架正常工作

<?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> 

調度-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="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> 

控制器:

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頁面:

<tr id="log_reg"> 
     <td> 
      <a href="login.htm"> Log in </a> 
      </td> 
     <td></td> 
     <td> 
     <a href="registration.htm"> Register </a> 
     </td> 
    </tr> 

運行這個命令我看到索引頁面:

http://localhost:8088/Forum/index.htm 

在頁面上我點擊鏈接,假設打開註冊頁面。但相反,我看到HTTP 404錯誤。 鏈接:

http://localhost:8088/Forum/registration.htm 

的NetBeans發現錯誤:

18-Jun-2011 14:45:19 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler 
INFO: Mapped URL path [/index.htm] onto handler [or[email protected]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' 

我不能釋放這些錯誤的原因。代碼看起來不錯,在我看來。這次我嘗試了很多東西。 有什麼幫助嗎? 致以問候

回答

1

可能是因爲您同時使用了註釋映射和SimpleUrlHandlerMapping。

嘗試添加豆:

<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotat ion.DefaultAnnotationHandlerMapping"> 
</bean> 

,將其作爲處理程序添加到您的urlMapping Bean的:

<property name="interceptors"> 
    <list> 
    <ref bean="handlerMapping"/> 
    </list> 
</property> 

它更容易使用的只有一個人,尤其是基於註解控制器。在這種情況下,你必須拋出你的urlMapping bean,而不是indexController bean使用簡單的類如:

@Controller 
class PagesCtrl { 
    @RequestMapping("/index.htm") 
    ModelAndView index() { 
     ModelAndView mav = new ModelAndView("index") 
     return mav 
    } 
} 
+0

是的,它現在可以工作。我已經實施了第二個建議。 – ucas