我是靠谱客的博主 安详蜡烛,最近开发中收集的这篇文章主要介绍freemarker遍历数据freeMarker入坑,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

FreeMarker获取List

freeMarker入坑

第一次使用freemarker,很多都不懂,因为需要,我将user和question 放到了ViewObjet里面,并且是以map的形式存放在ViewObject里面,最后以list的形式传入前端页面,通过freemarker获取list,数据是这样的
[ViewObject{objs={question=Question{id=22, title=‘Title11’, content=‘content11:balabala’, createdDate=Thu Sep 05 11:14:50 CST 2019, userId=11, commentCount=10}, user=User{id=11, name=‘User10’, password=’’, salt=’’, headUrl=’。。}}}, ViewObject{objs={question=Question{id=21, title=‘Title10’, content=‘content10:balabala’, createdDate=Thu Sep 05 11:08:50 CST 2019, userId=10, commentCount=9}, user=User{id=10, name=‘User9’, password=’’, salt=’’, headUrl=’。。’}}}, ViewObject{objs={question=Question{id=20, title=‘Title9’, content=‘content9:balabala’, createdDate=Thu Sep 05 11:02:50 CST 2019, userId=9, commentCount=8}, user=User{id=9, name=‘User8’, password=’’, salt=’’, headUrl=’。。’}}}, ViewObject{objs={question=Question{id=19, title=‘Title8’, content=‘content8:balabala’, createdDate=Thu Sep 05 10:56:50 CST 2019, userId=8, commentCount=7}, user=User{id=8, name=‘User7’, password=’’, salt=’’, headUrl=’。。’}}}, ViewObject{objs={question=Question{id=18, title=‘Title7’, content=‘content7:balabala’, createdDate=Thu Sep 05 10:50:50 CST 2019, userId=7, commentCount=6}, user=User{id=7, name=‘User6’, password=’’, salt=’’, headUrl=’。。’}}}, ViewObject{objs={question=Question{id=17, title=‘Title6’, content=‘content6:balabala’, createdDate=Thu Sep 05 10:44:50 CST 2019, userId=6, commentCount=5}, user=User{id=6, name=‘User5’, password=’’, salt=’’, headUrl=’。。’}}}, ViewObject{objs={question=Question{id=16, title=‘Title5’, content=‘content5:balabala’, createdDate=Thu Sep 05 10:38:50 CST 2019, userId=5, commentCount=4}, user=User{id=5, name=‘User4’, password=’’, salt=’’, headUrl=。。’}}}, ViewObject{objs={question=Question{id=15, title=‘Title4’, content=‘content4:balabala’, createdDate=Thu Sep 05 10:32:50 CST 2019, userId=4, commentCount=3}, user=User{id=4, name=‘User3’, password=’’, salt=’’, headUrl=’。。’}}}, ViewObject{objs={question=Question{id=14, title=‘Title3’, content=‘content3:balabala’, createdDate=Thu Sep 05 10:26:50 CST 2019, userId=3, commentCount=2}, user=User{id=3, name=‘User2’, password=’’, salt=’’, headUrl=’*’}}}, ViewObject{objs={question=Question{id=13, title=‘Title2’, content=‘content2:balabala’, createdDate=Thu Sep 05 10:20:50 CST 2019, userId=2, commentCount=1}, user=User{id=2, name=‘User1’, password=’’, salt=’’, headUrl=’’}}}]
对于这样的数据 我首先想的是便利list在便利map,可是各种百度,搞了很久很久,都不得行,最后我将数据拿出来分析了一下,才发现只需要遍历list再通过key获取到对象,再通过 . 获取到值
代码如下
<#list vos as vo>
${vo.getObjs(“question”).title}
</#list>

user.java



public class User {
    private int id;
    private String name;
    private String password;
    private String salt;
    private String headUrl;

    public User() {

    }
    public User(String name) {
        this.name = name;
        this.password = "";
        this.salt = "";
        this.headUrl = "";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSalt() {
        return salt;
    }

    public void setSalt(String salt) {
        this.salt = salt;
    }

    public String getHeadUrl() {
        return headUrl;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + ''' +
                ", password='" + password + ''' +
                ", salt='" + salt + ''' +
                ", headUrl='" + headUrl + ''' +
                '}';
    }

    public void setHeadUrl(String headUrl) {
        this.headUrl = headUrl;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

Question




    import java.util.Date;

    public class Question {
        private int id;
        private String title;
        private String content;
        private Date createdDate;
        private int userId;
        private int commentCount;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }

        public Date getCreatedDate() {
            return createdDate;
        }

        public void setCreatedDate(Date createdDate) {
            this.createdDate = createdDate;
        }

        public int getUserId() {
            return userId;
        }

        public void setUserId(int userId) {
            this.userId = userId;
        }

        public int getCommentCount() {
            return commentCount;
        }

        public void setCommentCount(int commentCount) {
            this.commentCount = commentCount;
        }

        @Override
        public String toString() {
            return "Question{" +
                    "id=" + id +
                    ", title='" + title + ''' +
                    ", content='" + content + ''' +
                    ", createdDate=" + createdDate +
                    ", userId=" + userId +
                    ", commentCount=" + commentCount +
                    '}';
        }
    }

ViewObject



import java.util.HashMap;
import java.util.Map;

public class ViewObject {
    private Map<String,Object> objs = new HashMap<>();


    public  void  setObjs(String key,Object value){
     objs.put(key,value);
    }
    public Object getObjs (String key){
        return  objs.get(key);
    }

    @Override
    public String toString() {
        return "ViewObject{" +
                "objs=" + objs +
                '}';
    }
}

将数据发送到前端

@RequestMapping(path = {"/", "/index"}, method = {RequestMethod.GET, RequestMethod.POST})
    public String index(Model model,
                        @RequestParam(value = "pop", defaultValue = "0") int pop) {
       List<Question> questionList = questionService.getLatestQuestions(0, 0, 10);
       List<ViewObject> vos = new ArrayList<>();
       for (Question question : questionList){
           ViewObject viewObject = new ViewObject();
           viewObject.setObjs("question",question);
           System.out.println("标题:"+question.getTitle()+"-----------》时间"+question.getCreatedDate());
           viewObject.setObjs("user",userService.selectById(question.getUserId()));
           vos.add(viewObject);
       }
        model.addAttribute("vos", vos);
        System.out.println(vos.toString());
        return "index";
    }

前端页面

 <#list vos as vo>
 ${vo.getObjs("question").title}
 </#list>

最后

以上就是安详蜡烛为你收集整理的freemarker遍历数据freeMarker入坑的全部内容,希望文章能够帮你解决freemarker遍历数据freeMarker入坑所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部