标签:pat dtd mvc ota lang 需求 程序 cep tco
案例需求:使用MVC模式编写一个程序,当发起一个deptList.do请求时,在servlet中准备一个部门列表对象,把这个列表对象放入request作用域中转发到deptlist.jsp,使用JSTL+EL的语法来显示这个部门列表。部门类型是XdlDept成员变量有int id、String name、String loc。
案例实现:
● XdlDept类:
package com.xdl.bean;
import java.io.Serializable;
public class XdlDept implements Serializable{
private static final long serialVersionUID = 1L;
private int deptno;
private String dname;
private String loc;
@Override
public String toString() {
return "XdlDept [deptno=" + deptno + ", dname=" + dname
+ ", loc=" + loc + "]\n";
}
public XdlDept(int deptno, String dname, String loc) {
super();
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
}
public XdlDept() {
super();
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
}
● XdlDeptController类:
package com.xdl.controller;
import java.io.IOException;
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.xdl.bean.XdlDept;
import com.xdl.dao.XdlDeptDAO;
import com.xdl.dao.impl.XdlDeptDAOOracleImp;
@WebServlet("/deptList.do")
public class XdlDeptController extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
XdlDeptDAO dao = new XdlDeptDAOOracleImp();
// 通过M层获取数据
List<XdlDept> depts = dao.getDeptList();
//放入对应的域对象
request.setAttribute("depts", depts);
// 跳转对应的页面
request.getRequestDispatcher("deptlist2.jsp").
forward(request, response);
}
}
● deptList.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>
<style type="text/css">
table {
width:600px;
text-align: center;
border-collapse: collapse;
}
table,tr,td{
border:1px solid red;
}
</style>
</head>
<body>
<table>
<tr> <td>DEPTNO</td> <td> DNAME</td> <td> LOC</td> </tr>
<c:forEach items="${depts}" var="dept" >
<tr> <td>${dept.deptno }</td> <td> ${dept.dname }</td>
<td> ${dept.loc }</td> </tr>
</c:forEach>
</table>
</body>
</html>
IT兄弟连 JavaWeb教程 EL与JSTL表达式经典案例
标签:pat dtd mvc ota lang 需求 程序 cep tco
原文地址:https://www.cnblogs.com/itxdl/p/10867337.html