上一篇文章将数据表中数据输出到浏览器上,这篇介绍如何将数据表数据在前台展示[使用freemarker模板引擎]
使用SpringMVC中设置上下文的方法:ModelAndView,重新编写控制类返回,其中appResouces是从配置文件获取的
1.从配置文件中读取参数
package com.xz.spring.springbootmybatis.utils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
//表示这个类是一个读取配置文件的类
@Configuration
//指定配置的一些属性,其中的prefix表示前缀
@ConfigurationProperties(prefix = "app.xz.spring")
//指定所读取的配置文件的路径
@PropertySource(value = "classpath:resource.properties")
public class AppResouces {
private String name;
private String website;
private String language;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website = website;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
}
2.在resources/resource.properties
app.xz.spring.name=SpringBoot和freemarker
app.xz.spring.website=www.xz.com
app.xz.spring.language=chinese
3. PersonsController文件
package com.xz.spring.springbootmybatis.controller;
import com.xz.spring.springbootmybatis.domain.Persons;
import com.xz.spring.springbootmybatis.service.PersonsService;
import com.xz.spring.springbootmybatis.utils.AppResouces;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class PersonsController {
@Autowired
private PersonsService personsService;
@Autowired
private AppResouces appResouces;
// @RequestMapping(value = "/person", method = RequestMethod.GET)
// @ResponseBody
// public Persons getPersonByName(@RequestParam(value = "uName", required = true)String name){
// return personsService.findPersonByName(name);
// }
@RequestMapping(value = "/person", method = RequestMethod.GET)
@ResponseBody
public ModelAndView getPersonByName(@RequestParam(value = "uName", required = true)String name){
ModelAndView modelAndView = new ModelAndView("index");
Persons person = personsService.findPersonByName(name);
modelAndView.addObject("appResouces",appResouces);
modelAndView.addObject("person",person);
return modelAndView;
}
}
4.创建index.ftl文件,位于resources/templates/index.ftl
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8"/>
<title></title>
</head>
<body>
FreeMarker模板引擎
<br>
<span>${appResouces.name}</span>
<span>${appResouces.website}</span>
<span>${appResouces.language}</span>
<br>
<div class="panel panel-default">
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>序号</th>
<th>姓名</th>
<th>生日</th>
<th>性别</th>
<th>职业</th>
<th>地址</th>
<th>电话</th>
<th> </th>
</tr>
</thead>
<tbody>
<tr>
<th>${person.id}</th>
<th>${person.uName}</th>
<th>${person.birthday}</th>
<th>${person.gender}</th>
<th>${person.zhiye}</th>
<th>${person.address}</th>
<th>${person.phone}</th>
<th> </th>
</tr>
</tbody>
</table>
</div>
</body>
</html>
最后,在流量器中输入地址:http://127.0.0.1:90/person?uName=%E5%BC%A0%E4%B8%89 即可