Java框架_SpringBoot整合Freemarker

1、创建项目

Java框架_SpringBoot整合Freemarker_第1张图片


2、修改pom文件,添加坐标

Java框架_SpringBoot整合Freemarker_第2张图片

3、编写视图

注意:springBoot要求模板形式的视图层技术的文件必须要放到src/main/resources目录下必须要一个名称为templates

Java框架_SpringBoot整合Freemarker_第3张图片




Insert title here


	
		<#list list as user>
		
ID Name Age
${user.userId} ${user.userName} ${user.age}

4、创建Controller

package com.lxp.controller;

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

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

import com.lxp.pojo.User;

@Controller
public class ShowUserController {

	@RequestMapping("/showUser")
	public String showUser(Model model) {

		List list = new ArrayList();
		list.add(new User(1, "张三", 20));
		list.add(new User(2, "李四", 22));
		list.add(new User(3, "王五", 24));

		model.addAttribute("list", list);
		return "userList";
	}
}
pojo类
package com.lxp.pojo;

public class User {
	private Integer userId;
	private String userName;
	private Integer age;

	public Integer getUserId() {
		return userId;
	}

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

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public User(int id, String name, int age) {
		this.userId = id;
		this.userName = name;
		this.age = age;
	}

}

6,创建启动类

@SpringBootApplication
public class App {

	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

你可能感兴趣的:(后端开发)