ftl页面下的forEach循环问题

来源:9-2 项目作业

importSharp

2021-04-02 11:10:56

在训练素材中给的category.html中,我需要把数据库中查询得到的数据返回到前端页面。我想到的是用request.setAttribute方法存到属性里,然后进一步在前端页面去显示。


问题一:原先的前后端作业中是用的jstl中的<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>用 <c:forEach>方法进行循环,但现在mybatis中该用什么方法实现循环


问题二:老师有没有比较好的思路做数据返回,因为对ajax不太理解,是不是也可以用ajax,然后后台使用response.getWriter().println(json);去做前后交互

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>分类列表</title>
<link rel="stylesheet" href="/css/index.css">
<link rel="stylesheet" href="/css/bootstrap.min.css">
</head>

<body>
<header>
<div class="container">

<nav>
<a href="/" >Java</a>
</nav>
<nav>
<a href="/forward/first" >前端</a>
</nav>
<nav>
<a href="/category" >分类</a>
</nav>

</div>
</header>
<section class="banner">
<div class="container">
<div>
<h1>图书</h1>
<p>图书分类列表</p>
</div>
</div>
</section>
<section class="main">
<div class="container">
<table class="table table-striped">
<thead>
<tr>
<th>名称</th>
<th>创建时间</th>
<th>最后修改时间</th>
</tr>
</thead>
<tbody>
<tr>
<td>Java</td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
</div>
</section>
<section class="page">
<div class="container">
<div id="fatie">
<a href="/forward/add_category"><button>新建</button></a>
</div>
</div>
</section>
<footer>
copy@慕课网
</footer>
</body>
</html>

servlet

public class CategoryServlet extends HttpServlet {
private CategoryService categoryService=new CategoryService();
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
List<Category> categoryList=categoryService.selectAll();
request.setAttribute("categoryList",categoryList);
request.getRequestDispatcher("/category.ftl").forward(request,response);
}
}

dao

package library.dao;

import library.entity.Category;
import library.utils.MybatisUtils;

import java.util.List;

public class CategoryDao {

/**
* 无参数
* 查询数据库中所有的类别
* @return 数据库中的category
*/
public List<Category> selectAll(){
List<Category> categoryList=(List<Category>)MybatisUtils.executeQuery(sqlSession->{
return sqlSession.selectList("categoryMapper.selectAll");
});
System.out.println(categoryList);
return categoryList;
}
}

工具类

package library.utils;


import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;
import java.util.function.Function;

public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory=null;

static {
Reader reader=null;

try {
reader=Resources.getResourceAsReader("mybatis-config.xml");
sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
throw new ExceptionInInitializerError(e);
}
}

public static Object executeQuery(Function<SqlSession,Object> func){
SqlSession sqlSession=sqlSessionFactory.openSession();

try{
Object obj = func.apply(sqlSession);
return obj;
}finally {
sqlSession.close();
}

}

public static Object executeUpdate(Function<SqlSession,Object> func){
SqlSession sqlSession=sqlSessionFactory.openSession(false);

try{
Object obj=func.apply(sqlSession);
sqlSession.commit();
return obj;
}catch (RuntimeException e){
sqlSession.rollback();
throw e;
}finally {
sqlSession.close();
}
}
}

xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="categoryMapper">
   <select id="selectAll" resultType="library.entity.Category">
       select * from category
</select>
</mapper>


写回答

1回答

好帮手慕阿莹

2021-04-02

同学你好,

1、同学第一问是不是想问,在ftl中如何展示循环呢?看同学写的mybatis?

如果是想问在ftl总如何使用循环,可以参考本课程中的代码,使用<#list></#list>进行遍历

http://img.mukewang.com/climg/6066b89509ecb84011230912.jpg

http://img.mukewang.com/climg/6066b8bf09e1e7db10930512.jpg

同学可以点击看一下本课6-2 的视频,大概11分50秒左右

https://class.imooc.com/lesson/1403#mid=34776&time=700

2、

同学是想用ajax去完成前后台的交互是吗?可以使用ajax的

祝学习愉快。


0

0 学习 · 16556 问题

查看课程