学习总结------Servlet控制器的简单运用
前言:
今天将简单的模拟 MVC模式 对Servlet控制器运行
若有不好或不对的地方,欢迎各位大神进行指导!
1.MVC模式(图)
MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写
2.建包
3.建实体类
3.1可以先立一个二维表做参考作用
考虑各个字段名字、类型
3.2实体类
代码:
package entity; public class House { //编号 private String id; //房子名字 private String name; //地址 private String address; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
4.建业务逻辑类
4.1命名业务逻辑接口
代码:
package service; import java.util.List; import entity.House; public interface HouseService { public List<House> getAllHouses(); }
4.2实现类
代码:
package service; import java.util.ArrayList; import java.util.List; import entity.House; public class HouseServiceImpl implements HouseService { @Override public List<House> getAllHouses() { List<House> houseList = new ArrayList<House>(); //1 光头强的家 森林188号 //2 熊大的家 森林树洞一期3楼 House h1 = new House(); House h2 = new House(); h1.setId("1"); h1.setName("光头强的家"); h1.setAddress("森林188号"); h2.setId("2"); h2.setName("熊大的家"); h2.setAddress("森林树洞一期3楼"); houseList.add(h1); houseList.add(h2); return houseList; } }
5.创建一个(Servlet)控制器
在action包中新建Servlet: HouseAction_showAllHouses。
调用service获取数据集List
代码:
package action; import java.io.IOException; 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 entity.House; import service.HouseService; import service.HouseServiceImpl; import java.util.List; @WebServlet("/HouseAction_showAllHouses") public class HouseAction_showAllHouses extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HouseService houseService = new HouseServiceImpl(); List<House> houseList = houseService.getAllHouses();
//用于设置标记,在jsp页面可以获取(List)request.getAttribute(); request.setAttribute("houseList_label", houseList); //请求并不会中断,而且继续在服务端接力运行 //并不是发出一个新的请求 //所以对于客户端的地址栏,是不会发生变化 request.getRequestDispatcher("show.jsp").forward(request,response); //通知客户端:你发出一个新的请求:show.jsp //response.sendRedirect("show.jsp"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
request.getRequestDispatcher().forward(request.response);
这个语句意思是将客户端的请求转向(forward)到getRequestDispatcher()方法中参数定义的页面或者链接。
说通俗点就是,当一个客户端的请求到这个页面后,不做处理或者不处理完,将请求转给另一个页面处理,然后再响应给客户端。
6.建jsp页面
建立一个show.jsp,取得request对象中的List。
核心代码:request.getAttribute("houseList_label");
代码:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.List" %> <%@ page import="entity.House" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% //获得Servlet标记的“houseList_label” List<House> houseList = (List)request.getAttribute("houseList_label"); %> 行<br> <table width="70%" border="0" cellspacing="1" cellpadding="1" align="center" bgcolor="#000099"> <tr bgcolor="#FFFFFF"> <td>编号</td> <td>名字</td> <td>地址</td> </tr> <% for(House house:houseList){ %> <tr bgcolor="#FFFFFF"> <td><%=house.getId()%></td> <td><%=house.getName()%></td> <td><%=house.getAddress()%></td> </tr> <% } %> </table> </body> </html>
7.在浏览器(客户端)中查看
地址:
------------------------------------------------