Day410&411

  • 依赖

导入thymeleaf依赖、热部署依赖devtools使页面实时生效

achangmall-product/pom.xml

org.springframework.boot

spring-boot-devtools

true

org.springframework.boot

spring-boot-starter-thymeleaf

html\首页资源\index放到achangmall-product下的static文件夹

把index.html放到templates中

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DzhvIwVW-1633360430233)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211004141636988.png)]

  • 关闭thymeleaf缓存,方便开发实时看到更新

thymeleaf:

cache: false

suffix: .html

prefix: classpath:/templates/

  • web开发放到web包下,原来的controller是前后分离对接手机等访问的,所以可以改成app,对接app应用

com.achang.achangmall.product.web【存放专门用于页面跳转的controller】

com.achang.achangmall.product.app【存放前后端奶粉里的rest风格接口controller】

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tPsk2HYj-1633360430235)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211004142800547.png)]

  • 渲染一级分类菜单 刚导入index.html时,里面的分类菜单都是写死的,我们要访问数据库拿到放到model中,然后在页面foreach填入

@Controller

public class IndexController {

@Autowired

private CategoryService categoryService;

//进入商品首页

@GetMapping({“/”,“/index.html”})

public String indexPage(Model model){

//查出一级分类

List categoryEntityList = categoryService.getLevel1Categorys();

//放入域中

model.addAttribute(“categorys”,categoryEntityList);

return “index”;

}

}

@Override

public List getLevel1Categorys() {

//查询一级分类

return baseMapper.selectList(new QueryWrapper().eq(“cat_level”,1));

}

  • 页面引入thymeleft名称空间

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-aBQnhfHR-1633360430238)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211004144731515.png)]

xmlns:th=“http://www.thymeleaf.org”

  • 因为我们上面引用了热部署的pom

更新页面后,在当前页面按Ctrl+Shift+F9,进行刷新热部署页面

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XSOh43W6-1633360430242)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211004145424575.png)]

  • 页面遍历菜单数据
  • 家用电器111

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-05kYVe1v-1633360430244)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211004150003768.png)]

    • 渲染三级分类菜单

    com.achang.achangmall.product.vo.Catelog2Vo

    //二级分类vo

    @NoArgsConstructor

    @AllArgsConstructor

    @Data

    public class Catelog2Vo {

    private String catalog1Id; //一级父分类id

    private List catalog3List; //三级子分类

    private String id;//二级分类id

    private String name;

    //三级分类vo 静态内部类

    @NoArgsConstructor

    @AllArgsConstructor

    @Data

    public static class Catelog3Vo{

    private String catalog2Id;//二级分类id

    private String id;//三级分类id

    private String name;

    }

    }

    com.achang.achangmall.product.web.IndexController

    //index/catalog.json

    @ResponseBody

    @GetMapping(“/index/catalog.json”)

    public Map<Long, List> getCatalogJson(){

    Map<Long, List> resultMap = categoryService.getCatelogJson();

    return resultMap;

    }

    com.achang.achangmall.product.service.impl.CategoryServiceImpl

    @Override

    public Map<Long, List> getCatelogJson() {

    //查出所有分类

    List level1Categorys = getLevel1Categorys();

    //分装数据

    Map<Long, List> resultMap = level1Categorys.stream().collect(Collectors.toMap(CategoryEntity::getCatId, v -> {

    //每一个的一级分类,查到这个一级分类的二级分类

    List list = baseMapper.selectList(new QueryWrapper().eq(“parent_cid”, v.getCatId()));

    List catelog2VoList = null;

    if (!StringUtils.isEmpty(list)) {

    catelog2VoList = list.stream().map(item -> {

    Catelog2Vo catelog2Vo = new Catelog2Vo(v.getCatId().toString(), null, item.getCatId().toString(), item.getName());

    //封装二级分类的三级分类

    List entityList = baseMapper.selectList(new QueryWrapper().eq(“parent_cid”, item.getCatId()));

    if (!StringUtils.isEmpty(entityList)){

    List<Catelog2Vo.Catelog3Vo> catelog3Vos = entityList.stream().map(m -> {

    Catelog2Vo.Catelog3Vo catelog3Vo = new Catelog2Vo.Catelog3Vo(item.getCatId().toString(),m.getCatId().toString(),m.getName());

    return catelog3Vo;

    }).collect(Collectors.toList());

    catelog2Vo.setCatalog3List(catelog3Vos);

    }

    return catelog2Vo;

    }).collect(Collectors.toList());

    return catelog2VoList;

    }

    return catelog2VoList;

    }));

    return resultMap;

    }


    1、Nginx配置文件


    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-g0qtVtoX-1633360430245)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211004215328514.png)]

    • 新增本地DNS映射

    C:\Windows\System32\drivers\etc\hosts

    192.168.109.101 achangmall.com

    测试访问之前的9200端口

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3nxKHKGr-1633360430247)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211004220252978.png)]

    • nginx配置文件

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-42kIYxX1-1633360430249)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211004222126644.png)]

    server {

    listen 80;

    server_name achangmall.com;

    #charset koi8-r;

    #access_log /var/log/nginx/log/host.access.log main;

    location / {

    #又转回到本机

    proxy_pass http://192.168.153.1:10000;

    }

    #error_page 404 /404.html;

    redirect server error pages to the static page /50x.html

    error_page 500 502 503 504 /50x.html;

    location = /50x.html {

    root /usr/share/nginx/html;

    }

    proxy the PHP scripts to Apache listening on 127.0.0.1:80

    #location ~ .php$ {

    proxy_pass http://127.0.0.1;

    #}

    pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

    #location ~ .php$ {

    root html;

    fastcgi_pass 127.0.0.1:9000;

    fastcgi_index index.php;

    fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;

    include fastcgi_params;

    #}

    deny access to .htaccess files, if Apache’s document root

    concurs with nginx’s one

    #location ~ /.ht {

    deny all;

    #}

    }

    • 测试访问http://achangmall.com/

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fDSzgkRR-1633360430251)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211004222946343.png)]


    2、Nginx+网关


    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qHdTCuEE-1633360430252)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211004223900250.png)]

    • nginx配置

    upstream achangmall{

    server 192.168.153.1:88;

    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值