- 依赖
导入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中
- 关闭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】
- 渲染一级分类菜单 刚导入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名称空间
xmlns:th=“http://www.thymeleaf.org”
- 因为我们上面引用了热部署的pom
更新页面后,在当前页面按Ctrl+Shift+F9
,进行刷新热部署页面
- 页面遍历菜单数据
-
- 渲染三级分类菜单
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;
}
- 新增本地DNS映射
C:\Windows\System32\drivers\etc\hosts
192.168.109.101 achangmall.com
测试访问之前的9200端口
- nginx配置文件
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/
- nginx配置
upstream achangmall{
server 192.168.153.1:88;
}