控制器向视图层传送数据(MVC模式)

一、从数据库中获取数据,用以赋值给类对象的属性,并将对象存入数组

在这里插入图片描述在这里插入图片描述在这里插入图片描述


二、MVC模式各模块的代码

注:这里的Model是实体类

1、实体类Servlet:存放从数据库中获取的数据

package com.fk.it.entity;

public class Attention {
	private int id;
	private int useri;
	private int marstid;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getUseri() {
		return useri;
	}

	public void setUseri(int useri) {
		this.useri = useri;
	}

	public int getMarstid() {
		return marstid;
	}

	public void setMarstid(int marstid) {
		this.marstid = marstid;
	}

}


2、控制器servlet:将实体类对象传递给jsp页面

package com.fk.it.ui;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fk.it.entity.Attention;

@WebServlet("/AttList")
public class AttListServlet extends HttpServlet{

	protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
			throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		List<Attention> list=new ArrayList<Attention>();
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");//初始化驱动
			String uri = "jdbc:mysql://localhost:3306/fk_weibo?"+
					"useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT";
			String user = "root";
			String password = "abc123";
			/*连接数据库*/
			Connection conn = DriverManager.getConnection(uri,user,password);
			
			PreparedStatement pstmt= conn.prepareStatement(
			"select * from attentions");

			ResultSet rs=pstmt.executeQuery();
			
			while(rs.next()){
				//把数据塞到一个集合
				//wb是一个压缩包,实质是一个类的对象
				Attention at=new Attention();
				at.setId(rs.getInt(1));
				at.setUseri(rs.getInt(2));
				at.setMarstid(rs.getInt(3));
				list.add(at);
			}
			
			rs.close();
			pstmt.close();
			conn.close();
		}catch(Exception ex){
			ex.printStackTrace();
			throw new RuntimeException(ex);
		}
		
		//把数据库的数据放在储物柜,交给jsp处理
		//request.setAttribute():请求域,一进一出,用完即止
		//session.setAttribute():会话,保留一段时间
		req.setAttribute("attention",list);
		//转发给jsp
		req.getRequestDispatcher("/att_list.jsp").forward(req, resp);
		
	}
	
}


3、视图jsp:将数据呈现给用户

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.util.*,com.fk.it.entity.*"%>
<!DOCTYPE html>
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style>
     th{border-bottom:2px solid #ccc;}
     td{border-bottom:1px solid #ccc; text-align:center}
	 h1{text-align:center}
</style>
<script>
   
</script>

</head>
<body>
   <h1>关注列表</h1>
   <%@include file="nav.jsp" %>
   <table align="center" width="60%" cellspacing="0px">
       
       <tbody>
       <tr>
              <th>编号</th>
              <th>博主</th>
              <th>粉丝</td>
              <th>操作</th>
       </tr>
 
        <%
        	List<Attention> attention=(List<Attention>)request.getAttribute("attention");	
        	for(Attention att:attention){
        %>
        <tr>
            
             <td> <%=att.getId() %></td>
             <td> <%=att.getUseri() %></td>
             <td> <%=att.getMarstid() %></td>
             <td>
             <a href="#">看博主</a>
			 <a href="#">取消关注</a>
             </td>
       </tr>
         <% }%>	
       
   </tbody>
   </table>
     

</body></html>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值