基于注释的弹簧映射不起作用。 404

时间:2011-12-25 15:42:03

标签: spring spring-mvc

当尝试使用基于注释的控制器访问我的页面时,我总是从tomcat获得“请求的资源不可用”。

控制器:

 @Controller
 public class HelloWorldController {

     @RequestMapping("/hello")
     public ModelAndView helloWorld() {

        String message = "Hello World, Spring 3.0!";
        return new ModelAndView("hello", "message", message);
    }
}     

我尝试访问/hello.htm并获取404.mainpage.htm工作正常,但其控制器不是带注释的类型。

dipsatcher-servlet.xml中:

<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    
    <property name="location" value="classpath:/blog.properties"/>  


<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>


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

<!--ViewResolver määrab kontrollerklassist tagastatud parameetri järgi vaate. Hetkel jsp lehekülg -->
<bean id="viewResolver"  class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
    <property name="prefix">
        <value>/WEB-INF/jsp/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>


<bean name="/mainpage.htm" class="com.mycomp.controller.MainPage">
    <property name="message" value="${message}"/>
    <property name="maxposts" value="${maxposts}"/>
</bean>

2 个答案:

答案 0 :(得分:0)

您是否已将相应的视图添加到jsp文件夹中?由于您使用的是ModelAndView(Object view, String modelName, Object modelObject)构造函数,我希望看到 hello.jsp 实现(可能使用message模型对象)。

答案 1 :(得分:0)

让我们尝试指定请求方法,并指出注释中变量的名称(也是第一个斜杠不是强制性的):

@RequestMapping(value="hello", method = RequestMethod.GET)
相关问题