Spring MVC Velocity模板引擎

Spring  MVC  Velocity模板引擎

项目完成时,结构如下:

Spring MVC Velocity模板引擎_第1张图片

各个文件的源码

webtest\src\main\webapp\WEB-INF\web.xml:




    
            dispatcher
            
                   org.springframework.web.servlet.DispatcherServlet
            
            1
    

    
        dispatcher
        /
    

    
            contextConfigLocation
            /WEB-INF/dispatcher-servlet.xml
    

    
            
                  org.springframework.web.context.ContextLoaderListener
            
    


webtest\src\main\webapp\WEB-INF\dispatcher-servlet.xml:




    

    
        
            /WEB-INF/template/
        
          
    

    
        
            .vm
        
        
            text/html;charset=UTF-8  
        
    


webtest\src\main\webapp\WEB-INF\template\velocity.properties:

input.encoding=UTF-8
output.encoding=UTF-8

webtest\src\main\webapp\WEB-INF\template\index.vm:


    
    
        My first example using Spring 3 MVC
    
    
        

hello

webtest\src\main\webapp\WEB-INF\template\hello.vm:


    
        Velocity
    
    
        

${message}


webtest/src/main/java/letian/spring/controller/HelloController.java:

package letian.spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloController {

    @RequestMapping(value = "/",method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {

        model.addAttribute("message", "Hi, I am Velocity");
        return "hello";

    }   

    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    public ModelAndView printHi(ModelMap model) {

        return new ModelAndView("index");  

    }   

}
webtest\pom.xml


  4.0.0
  springmvc
  webtest
  war
  0.0.1-SNAPSHOT
  webtest Maven Webapp
  http://maven.apache.org
   
4.1.4.RELEASE
         
         
  
    
      junit
      junit
      3.8.1
      test
    
 
         org.springframework
         spring-web
           ${spring.version}
     
     
     
         org.springframework
         spring-webmvc
          ${spring.version}
     
          
org.springframework
      spring-orm
      ${spring.version}
     
         
      org.springframework
      spring-test
      ${spring.version}
    
        
      org.springframework
      spring-context
  ${spring.version}
    
    
        
      org.springframework
      spring-context-support
  ${spring.version}
    
    

   org.apache.velocity

   velocity

   1.7

  

  

   org.apache.velocity

   velocity-tools

   2.0

  
       

		org.apache.commons
	commons-collections4
	4.0

	
	oro
	oro
	2.0.8
	
  
  
    webtest
  



打开浏览器,访问 http://localhost:8080/webtest/,可以看到

Spring MVC Velocity模板引擎_第2张图片

访问 http://localhost:8080/HelloSpring/hi,可以看到:

Spring MVC Velocity模板引擎_第3张图片

转载:http://www.codeweblog.com/spring-3-mvc-%E4%BD%BF%E7%94%A8velocity%E6%A8%A1%E6%9D%BF%E5%BC%95%E6%93%8E/



你可能感兴趣的:(Spring MVC Velocity模板引擎)