首頁  >  問答  >  主體

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

黄舟黄舟2715 天前424

全部回覆(1)我來回復

  • 黄舟

    黄舟2017-04-17 17:42:54

    這個你還要貼目錄結構才行。
    從你程式碼看的話,你的目錄vms那一層的目錄結構出問題的多。
    你可能的目錄結構為:

    /phones
        myphone.vm
        

    那你控制器那裡回傳的路徑應該是:

    return new ModelAndView("myphone", m);
    

    要注意的是@RequestMapping(value="/phones"),映射的是使用者在瀏覽器端的存取路徑。

    回覆
    0
  • 取消回覆