springmvc with velocity:无法解析名称为' index'的视图在名为'调度员&{39;

时间:2015-06-06 08:24:21

标签: java spring spring-mvc velocity

我运行一个springmvc和velocity演示,并遇到无法解析视图的问题' index'(controll to index.vm)以及以下是我的项目配置:



└─WEB-INF
    │  applicationContent-servlet.xml
    │  velocity.properties
    │  web.xml
    │
    ├─fragments
    │    _footer.vm
    │    _header.vm
    │
    ├─layouts
    │    layout.vm
    │
    └─views
         index.vm




下面的

是applicationContext-servlet.xml中的速度配置



   <bean id="velocityConfig"  
        class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">  
        <property name="resourceLoaderPath" value="/WEB-INF/views/" />  
        <property name="velocityProperties">      
        <props>      
            <prop  key="input.encoding">UTF-8</prop>      
            <prop  key="output.encoding">UTF-8</prop>        
         </props>      
     </property>   
    </bean>  
  
    <bean id="viewResolver"  
        class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">  
        <property name="cache" value="true" />  
        <property name="prefix" value="/WEB-INF/views/" />  
        <property name="layoutUrl" value="/WEB-INF/layouts/layout.vm" />  
        <property name="suffix" value=".vm" />  
        <property name="contentType"><value>text/html;charset=UTF-8</value></property>    
    </bean>  
&#13;
&#13;
&#13;

以下是我的web.xml

&#13;
&#13;
	<servlet>
		<servlet-name>dispatcher</servlet-name>
		<servlet-class>
			org.springframework.web.servlet.DispatcherServlet
		</servlet-class>
		<init-param> 
           <param-name>contextConfigLocation</param-name> 
           <param-value>
           /WEB-INF/applicationContext-servlet.xml 
           </param-value> 
          </init-param> 
         <load-on-startup>1</load-on-startup>
	</servlet>
 
	<servlet-mapping>
		<servlet-name>dispatcher</servlet-name>
		<url-pattern>/*</url-pattern>
	</servlet-mapping>
&#13;
&#13;
&#13;

我的控制代码是这样的:

&#13;
&#13;
@Controller
public class ArticleController {

	@Autowired
	private ArticleService articleService;

	@RequestMapping(value = { "/", "/welcome" }, method = RequestMethod.GET)
	public String welcomePage(Model model) {
		List<Article> list = articleService.getArticles();
		model.addAttribute("departments", list);
		return "index";
	}
}
&#13;
&#13;
&#13;

当我在tomcat上运行时遇到无法访问index.vm的问题,xml配置有什么问题,谢谢!

2 个答案:

答案 0 :(得分:0)

在applicationContext-servlet.xml中更改

    <property name="prefix" value="/WEB-INF/views/" />  

   <property name="prefix" value="/WEB-INF/velocity/views/" />  

答案 1 :(得分:0)

我改变了

<property name="resourceLoaderPath" value="/WEB-INF/views/" /> <property name="resourceLoaderPath" value="/" /> 并且它的工作原理