写一个Freemarker demo

项目开始前准备

此项目是在ssm进行,直接对SSM项目进行修改,因此略去其他步骤,只对修改的内容进行描述

  • 首先,pom文件需要添加的内容有


   org.springframework
   spring-context-support
   ${spring.version}




  org.freemarker
  freemarker
  ${freemarker.version}

  • springmvc-servlet.xml需要添加的设置

    
        
        
            
                0
                UTF-8
                0.##########
                yyyy-MM-dd HH:mm:ss
                true
                ignore
            
        
    

    
    
        
        
        
        
        
        
        
    

目前测试 没有对原有的jsp 视图解析器设置造成不兼容
JSP视图解析器原有配置为

    
    
        
        
        
    

先看一个demo

  • 我们先定义一个entity

import java.util.Date;

public class SysUser {
    private Integer userid;

    private String usercode;

    private String username;

    private String password;

    private Integer status;

    private String phonenumber;

    private String email;

    private String creator;

    private Date createtime;

    private String description;

    public Integer getUserid() {
        return userid;
    }

    public void setUserid(Integer userid) {
        this.userid = userid;
    }

    public String getUsercode() {
        return usercode;
    }

    public void setUsercode(String usercode) {
        this.usercode = usercode == null ? null : usercode.trim();
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username == null ? null : username.trim();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getPhonenumber() {
        return phonenumber;
    }

    public void setPhonenumber(String phonenumber) {
        this.phonenumber = phonenumber == null ? null : phonenumber.trim();
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email == null ? null : email.trim();
    }

    public String getCreator() {
        return creator;
    }

    public void setCreator(String creator) {
        this.creator = creator == null ? null : creator.trim();
    }

    public Date getCreatetime() {
        return createtime;
    }

    public void setCreatetime(Date createtime) {
        this.createtime = createtime;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description == null ? null : description.trim();
    }
}

· 然后定义一个Controller



import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.xqchen.ssm.domain.SysUser;

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

@Controller

public class DemoController {

    @RequestMapping("/demo")
    public ModelAndView demo() {
        ModelAndView mv = new ModelAndView("demo");
        mv.addObject("username", "demo ceshi");
        mv.addObject("loglist", this.logList());
        return mv;
    }

    public List logList() {
        List list = new ArrayList();
        for (int i = 0; i < 5; i++) {
            SysUser sysUser = new SysUser();
            sysUser.setCreatetime(new Date());

            sysUser.setUsername("laobiao" + i);

            list.add(sysUser);
        }
        return list;
    }
}

以上 为了从简 我把数据库操作省略了 直接给对象赋值
我在ModelAndView 传了两个对象,分别是 usernameloglist
我需要新建一个demo.html文件 通过Freemarker引擎取值

  • demo.html



   
   Title


${username!}
    <#list loglist as loglist>
  • ${loglist.username}
  • ${loglist.createtime?string('yyyy-MM-dd')}

我们可以从下图看到 我们取到了 username 单个值 以及loglist这个集合。

写一个Freemarker demo_第1张图片
image.png

后面,我们再来讲解Freemarker的语法。

你可能感兴趣的:(写一个Freemarker demo)