初学SpringBoot+Mybatis+Web项目(二)

上一篇文章将数据表中数据输出到浏览器上,这篇介绍如何将数据表数据在前台展示[使用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>&nbsp;</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>&nbsp;</th>
        </tr>
        </tbody>
    </table>
</div>
</body>
</html>

最后,在流量器中输入地址:http://127.0.0.1:90/person?uName=%E5%BC%A0%E4%B8%89 即可

阅读终点,创作起航,您可以撰写心得或摘录文章要点写篇博文。去创作
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

暴走提莫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值