Home  >  Q&A  >  body text

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 days ago427

reply all(1)I'll reply

  • 黄舟

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

    You still need to post the directory structure for this.
    Judging from your code, there are many problems with the directory structure of your directory vms layer.
    Your possible directory structure is:

    /phones
        myphone.vm
        

    Then the path returned by your controller should be:

    return new ModelAndView("myphone", m);
    

    It should be noted that @RequestMapping(value="/phones") maps the user's access path on the browser side.

    reply
    0
  • Cancelreply