2012-09-30 222 views
0

我從Spring Framework開始,希望用annotatios做一個HelloWorld,我已經使它創建了一個控制器和一個視圖,我猜基本的Hello工作正常;但是,我想使用annotatios,因爲我不能再使用SimpleFormController(不建議使用)。春季註釋不起作用

我得到的錯誤是埃斯塔HTTP 404 - /av/index.jsp

我使用NetBeans和我立足的基本模板,提供了範例。我有以下文件,我很確定這是一個配置錯誤,但我找不到任何可以幫助我的東西。提前致謝。

的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>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>*.do</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"> 

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/> 

    <bean id="annotationHandlerMapping" 
     class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> 
     <property name="order" value="1"/> 
     <property name="alwaysUseFullPath" value="true"/> 
    </bean> 

    <bean id="viewResolver" 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix"> 
      <value>/</value> 
     </property> 
     <property name="suffix"> 
      <value>.jsp</value> 
     </property> 
    </bean> 

    <context:component-scan base-package="controller"/> 

</beans> 

indexController.java

package controller; 

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.portlet.ModelAndView; 

@Controller 
public class IndexController { 

    @RequestMapping(value="/index.do", method= RequestMethod.GET) 
    public ModelAndView inicio(){ 
     ModelAndView mv = new ModelAndView("index"); 
     mv.addObject("usuario", "jaxkodex"); 
     return mv; 
    } 
} 
+0

請求映射和servlet映射之間有衝突。在你的情況下,你需要通過'/ index.do.do'訪問你的url。 –

回答

2

你缺少

<mvc:annotation-driven /> 

在你的配置dispatcher-servlet.xml

更多信息

1

你映射到控制器的路徑/index.dohere,所以你必須用下面的URL來訪問它:http://localhost/av/index.do

+0

我不明白。它有用嗎?我的建議是獲得一個可用的Spring MVC應用程序的最簡單方法:安裝[Spring Tool Suite IDE](http://www.springsource.org/sts)並創建一個MVC類型的Spring模板項目。 –

0

請試試這個

<bean 
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> 

在dispatcher-servlet.xml中添加條目

0

在您的disapatcher-servlet.xml中,添加 <mvc:annotation-driven/>以啓用註釋驅動的處理程序映射。

0

試着通過我的教程之一:simple Controller

創作與基於java的配置

我希望我能與它

1

<mvc:annotation-driven/>成功,將解決你的問題。