Thymeleaf(2)一个实现页面crud的小项目(展示数据)

一步一步做下去。

 

实体类,是根据数据库的一张表的数据 创建的,后面getset就不放了。

 

 

封装类

package cn.tedu.utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

import org.apache.commons.dbcp.BasicDataSource;

public class DbUtils {
	private static BasicDataSource ds;
	static{
		Properties p = new Properties();
		InputStream in = DbUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
		try {
			p.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		}
		String driver = p.getProperty("driver");
		String url = p.getProperty("url");
		String username = p.getProperty("username");
		String password = p.getProperty("password");

		// 创建数据库连接池对象
		ds = new BasicDataSource();
		// 连接信息
		ds.setDriverClassName(driver);
		ds.setUrl(url);
		ds.setUsername(username);
		ds.setPassword(password);
		ds.setInitialSize(3);
		ds.setMaxActive(5);
	}
	
	public static Connection getConn() throws Exception {
		return ds.getConnection();
	}
}

 

package cn.tedu.utils;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;

public class ThUtils {

	public static void print(String fileName, Context context, HttpServletResponse resp) throws IOException {
		// 创建模板引擎对象
		TemplateEngine te = new TemplateEngine();
		// 创建配置对象(解析器 负责解析HTML页面)
		ClassLoaderTemplateResolver r = new ClassLoaderTemplateResolver();
		// 设置字符集
		r.setCharacterEncoding("UTF-8");
		// 配置对象和模板引擎关联
		te.setTemplateResolver(r);
		// 把容器中的数据和页面整合到一起
		String html = te.process(fileName, context);//将得到的html返回给浏览器
		//设置响应类型
		resp.setContentType("text/html;charset=utf-8");
		PrintWriter pw = resp.getWriter();
		pw.print(html);
		pw.close();
	}
}

 

以及jdbc的

 

 

再创建dao,里面什么都没有

 

配置完成,我们先写一个展示所有数据的Servlet,取名为:ListS,放在controller包里面

public class ListS extends HttpServlet {
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		SubjectDao dao = new SubjectDao();
		List<Subject> list = dao.findAll();
		
		Context context = new Context();
		//左边的"list"是接收页面的数据,页面的和这个名字要一样
		context.setVariable("list", list);
		ThUtils.print("list.html", context, resp);
	}
}

 

dao是没有findAll的,所以直接创建

public class SubjectDao {

	public List<Subject> findAll() {
		List<Subject> list = new ArrayList<Subject>();
		try (Connection conn = DbUtils.getConn();) {
			String sql = "select * from subject";
			Statement s = conn.createStatement();
			ResultSet rs = s.executeQuery(sql);
			while(rs.next()){
				int subjectId = rs.getInt(1);
				String subjectName = rs.getString(2);
				int hour = rs.getInt(3);
				int gradeId = rs.getInt(4);
				list.add(new Subject(subjectId, subjectName, hour, gradeId));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

}

 

 

下面是list.html 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>课程列表</title>
</head>
<body>
	<table border="1" align="center">
		<tr>
			<td>课程id</td>
			<td>课程名称</td>
			<td>课时</td>
			<td>阶段id</td>
			<td>操作</td>
		</tr>
		<tr th:each="sub:${list}">
			<td th:text=${sub.subjectId}></td>
			<td><a th:text=${sub.subjectName} th:href="'ShowOneServlet?id='+${sub.subjectId}"></a></td>
			<td th:text=${sub.hour}></td>
			<td th:text=${sub.gradeId}></td>
			<td><a th:href="'DelServlet?subjectId='+${sub.subjectId}">删除</a></td>
		</tr>
	</table>
</body>
</html>

这里使用th:的时候一定要注意这几点:

1.sub是类似于对象名的,是用来获取list里面的值得,each就是类似foreach

2.th:text可以改变文本,后面的subjectName之类的,一定要和在Dao中获取值的变量名一模一样

3.<td><a th:text=${sub.subjectName} th:href="'ShowOneServlet?id='+${sub.subjectId}"></a></td>

这一条是把th放在了a标签里,这是点击了就会转发到href设置的页面的

 

我的页面成功把数据库中的数据取了出来。

 

原本想一个文章写完,但是考虑到可能太乱,所以分多几个页面写

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值