首页 > springMVC结合velocity出现500错误

springMVC结合velocity出现500错误

HTTP Status 500 - Could not resolve view with name 'phones' in servlet with name 'dynamic-velocity'

我给ModelAndView的视图名称为myphone, 为啥报错说我的视图名称是phones呢?

方法:

@RequestMapping(value="/phones")
public ModelAndView showPhones() {
    log.info("showPhones.");
    Map<String, String>  m = new HashMap<String, String>();
    m.put("phone1", "huawei");
    m.put("phone2", "apple");
    return new ModelAndView("myphone", m);
             
}

web.xml:

 <servlet>
        <servlet-name>dynamic-velocity</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
      <servlet-mapping>  
        <servlet-name>dynamic-velocity</servlet-name>  
        <url-pattern>/</url-pattern>  
    </servlet-mapping>  

servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
     
     <context:component-scan base-package="com.lu1"/>
     <mvc:default-servlet-handler />
     <mvc:annotation-driven />
      <bean id="velocityConfigurer"  
       class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">  
       <property name="resourceLoaderPath"  value="/WEB-INF/vms/" /> 
       </bean>
    
   <bean class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"
            id="velocityResourceViewResolver">
          <property name="suffix" value=".vm" /> 
   </bean>


这个你还要贴目录结构才行。
从你代码看的话,你的目录vms那一层的目录结构出问题的多。
你可能的目录结构为:

/phones
    myphone.vm
    

那么你控制器那里返回的路径应该是:

return new ModelAndView("myphone", m);

需要注意的是@RequestMapping(value="/phones"),映射的是用户在浏览器端的访问路径。

【热门文章】
【热门文章】