简体   繁体   中英

springmvc with velocity:Could not resolve view with name 'index' in servlet with name 'dispatcher'

I run a springmvc and velocity demo and meet the problem that Could not resolve view with name 'index'(controll to index.vm) and below is my project configuration:

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

below is my velocity configuration in 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> 

and below is my web.xml

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

my controll code is like that:

 @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"; } } 

when I run on tomcat I meet the problem that can't reach index.vm,is there any thing wrong in xml configuration ,thanks!

In applicationContext-servlet.xml change

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

to

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

I changed

<property name="resourceLoaderPath" value="/WEB-INF/views/" /> to <property name="resourceLoaderPath" value="/" /> adn it works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM