【JAVA基础】springMVC 整合freemarker(maven)

当今最流行的java模板引擎,莫过于freemarker和velocity。这里我弄个springMVC整合freemark的例子分享给大家。


基本的就不说了,直接入正题。

第一步:pom文件的依赖包

  
      org.springframework
      spring-webmvc
      3.2.4.RELEASE
    
    
    
	        org.freemarker
		freemarker
		2.3.20
	
	
	
	    org.springframework
	    spring-context-support
	    3.2.4.RELEASE
	
第二步:web.xml配置

  
	        demo
	        org.springframework.web.servlet.DispatcherServlet
	        1
	
	
	
	        demo
	        *.html
	
第三步:在WEN-INF下面 再建一个demo-servlet.xml





    


    

第四步: 在java/main/resource目录下新增freemarker.properties配置文件

#\u8BBE\u7F6E\u6807\u7B7E\u7C7B\u578B\uFF1Asquare_bracket:[]     auto_detect:[]<>
tag_syntax=auto_detect
#\u6A21\u7248\u7F13\u5B58\u65F6\u95F4\uFF0C\u5355\u4F4D\uFF1A\u79D2
template_update_delay=0
default_encoding=UTF-8
output_encoding=UTF-8
locale=zh_CN
#\u8BBE\u7F6E\u6570\u5B57\u683C\u5F0F \uFF0C\u9632\u6B62\u51FA\u73B0 000.00
number_format=#
#\u53D8\u91CF\u4E3A\u7A7A\u65F6\uFF0C\u4E0D\u4F1A\u62A5\u9519
classic_compatible=true
#auto_import="/WEB-INF/templates/index.ftl" as do
第五步: 在demo-servlet.xml中增加freemarker视图配置

 
		
		    
		
		
		
		
			
		    
		        /WEB-INF/ftl/
		    
		
		
		
		
		    
		    
		    
		
第六部:controller

package com.testFreeMarker.controller;

import java.util.ArrayList;
import java.util.List;

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

import com.testFreeMarker.model.User;

@Controller
public class FreemarkerController {
	
	@RequestMapping("/hi")
	public String sayHello(ModelMap map){
        System.out.println("哈哈哈");
        map.put("name", "测试一下这个freeMarker");

        return "/hi.ftl";
	}
	
	@RequestMapping("/user")
    public String helloUser(ModelMap modelMap) {
		User user1=new User(1, "罗宾", "123456");
		User user2=new User(2, "老施", "123456");
		User user3=new User(3, "老大", "123456");
		User user4=new User(4, "老板", "123456");
		List list=new ArrayList();
		list.add(user1);
		list.add(user2);
		list.add(user3);
		list.add(user4);
        modelMap.addAttribute("userDo", list) ;
        return "/userList.ftl";
    }
}
User

package com.testFreeMarker.model;

public class User {
	private int id;
	private String username;
	private String password;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public User(int id, String username, String password) {
		super();
		this.id = id;
		this.username = username;
		this.password = password;
	}

}
hi.ftl



    

say hello ${name}


${(1 != 1)?string("yes", "no")}
userList.ftl

<#setting classic_compatible=true>




User List




 
User List
 
  <#list userDo as user>
ID Username Password
${user.id} ${user.username} ${user.password}
 
     

最后看一下目录结构:














你可能感兴趣的:(【JAVA基础】)