Unit05: WEB项目的开发模式 、转发 和 Unit09: EL、JSTL
dao
package dao; import java.io.Serializable;
import java.util.ArrayList;
import java.util.List; import entity.Emp; public class EmpDao implements Serializable { public List<Emp> findAll() {
List<Emp> list = new ArrayList<Emp>(); Emp e1 = new Emp();
e1.setEmpno(1);
e1.setEname("唐僧");
e1.setJob("师父");
e1.setSal(9000.0);
list.add(e1); Emp e2 = new Emp();
e2.setEmpno(2);
e2.setEname("悟空");
e2.setJob("徒弟");
e2.setSal(5000.0);
list.add(e2); Emp e3 = new Emp();
e3.setEmpno(3);
e3.setEname("八戒");
e3.setJob("徒弟");
e3.setSal(6000.0);
list.add(e3); return list;
} public void save(Emp e) {
System.out.println(
"增加员工:" + e.getEname());
} }
EmpDao.java
entity
package entity; import java.io.Serializable; public class Course implements Serializable { //对象的属性
private Integer courseId;
private String name;
private Integer days; //Bean的属性
//1.通过get/set方法所观察出来的属性
//2.将get/set去掉后得到的单词首字母小写
public Integer getId() {
return courseId;
}
public void setId(Integer courseId) {
this.courseId = courseId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getDays() {
return days;
}
public void setDays(Integer days) {
this.days = days;
} }
Course.java
package entity; import java.io.Serializable; public class Emp implements Serializable { private Integer empno;
private String ename;
private String job;
private Double sal; public Integer getEmpno() {
return empno;
}
public void setEmpno(Integer empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public Double getSal() {
return sal;
}
public void setSal(Double sal) {
this.sal = sal;
} }
Emp.java
package entity; import java.io.Serializable; public class Student implements Serializable { private String name;
private Integer age;
private String sex;
private String[] interests;
private Course course; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String[] getInterests() {
return interests;
}
public void setInterests(String[] interests) {
this.interests = interests;
}
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
} }
Student.java
Model 1模式
WEB组件:Servlet/JSP
使用一个组件处理请求,处理业务,返回响应信息,耦合度过高,不利于代码维护
<%@page pageEncoding="utf-8"
import="dao.*,entity.*,java.util.*"%>
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title>查询员工</title>
<style>
table {
border: 1px solid red;
border-collapse: collapse;
width: 30%;
}
td {
border: 1px solid red;
}
</style>
</head>
<body>
<table>
<tr>
<td>编号</td>
<td>姓名</td>
<td>职位</td>
<td>薪资</td>
</tr>
<%
EmpDao dao = new EmpDao();
List<Emp> list = dao.findAll();
if(list != null) {
for(Emp e : list) {
%>
<tr>
<td><%=e.getEmpno() %></td>
<td><%=e.getEname() %></td>
<td><%=e.getJob() %></td>
<td><%=e.getSal() %></td>
</tr>
<%
}
}
%>
</table>
</body>
</html>
find_emp.jsp
Model 2模式
Model2引用了MVC模式对代码解耦。(重要)
MVC模式是经典的设计模式,是代码的分层思想:
1.M:model,即业务层,用来处理业务;
2.V:View,即视图层,用来显示数据;
3.C:controller,即控制层,负责调度,是M和V的桥梁,其作用是对代码解耦,便于团队开发及维护;
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>jsp2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list> <servlet>
<servlet-name>findEmp</servlet-name>
<servlet-class>web.FindEmpServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>findEmp</servlet-name>
<url-pattern>/findEmp</url-pattern>
</servlet-mapping> <servlet>
<servlet-name>findStudent</servlet-name>
<servlet-class>web.FindStudentServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>findStudent</servlet-name>
<url-pattern>/findStudent</url-pattern>
</servlet-mapping> </web-app>
web.xml
package web; import java.io.IOException;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import dao.EmpDao;
import entity.Emp; public class FindEmpServlet extends HttpServlet { @Override
protected void service(
HttpServletRequest req,
HttpServletResponse res) throws ServletException, IOException {
//查询所有员工
EmpDao dao = new EmpDao();
List<Emp> list = dao.findAll();
//转发到JSP
//1)将数据绑定到request上
req.setAttribute("emps", list);
//2)将请求转发给JSP
//当前:/jsp2/findEmp
//目标:/jsp2/emps.jsp
req.getRequestDispatcher("emps.jsp")
.forward(req, res);
} }
FindEmpServlet.java
<%@page pageEncoding="utf-8"
import="entity.*,java.util.*"%>
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title>查询员工</title>
<style>
table {
border: 1px solid red;
border-collapse: collapse;
width: 30%;
}
td {
border: 1px solid red;
}
</style>
</head>
<body>
<table>
<tr>
<td>编号</td>
<td>姓名</td>
<td>职位</td>
<td>薪资</td>
</tr>
<%
List<Emp> list = (List<Emp>)
request.getAttribute("emps");
if(list != null) {
for(Emp e : list) {
%>
<tr>
<td><%=e.getEmpno() %></td>
<td><%=e.getEname() %></td>
<td><%=e.getJob() %></td>
<td><%=e.getSal() %></td>
</tr>
<%
}
}
%>
</table>
</body>
</html>
emps.jsp
转发和重定向的对比(*)
1.它们的相同点
- 它们都是用来解决2个WEB组件之间的跳转问题
2.它们的区别(*)
- 依赖用转发
- 不依赖用重定向
3.建议
- 一般查询时用转发
- 一般增加、修改、删除后,要重定向到查询
Unit09: EL、JSTL
3.JSTL导包
- 使用maven搜索jstl
- 结果中选择1.2[jar]
4.JSTL原理
package web; import java.io.IOException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import entity.Course;
import entity.Student; public class FindStudentServlet extends HttpServlet { @Override
protected void service(
HttpServletRequest req,
HttpServletResponse res) throws ServletException, IOException {
//模拟查询一个学生
Student stu = new Student();
stu.setName("zhangsan");
stu.setAge(23);
stu.setSex("M");
stu.setInterests(
new String[]{"篮球","足球","排球"});
Course c = new Course();
c.setId(1);
c.setName("Java");
c.setDays(82);
stu.setCourse(c);
//转发
req.setAttribute("stu", stu);
req.getRequestDispatcher("student.jsp")
.forward(req, res);
} }
FindStudentServlet.java
<%@page pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title>查询学生</title>
</head>
<body>
<!-- 2.JSTL标签 -->
<!-- 2.1 if标签 -->
<p>
<c:if test="${stu.sex=='M' }">男</c:if>
<c:if test="${stu.sex=='F' }">女</c:if>
</p>
<!-- 2.2 choose标签 -->
<p>
<c:choose>
<c:when test="${stu.sex=='M' }">男</c:when>
<c:otherwise>女</c:otherwise>
</c:choose>
</p>
<!-- 2.3 forEach标签 -->
<p>
<c:forEach items="${stu.interests }" var="i">
${i }
</c:forEach>
</p> <!-- 1.EL表达式 -->
<!-- 1.1访问Bean属性 -->
<!-- request.getAttribute("stu").getName() -->
<p>姓名:${stu.name }</p>
<p>年龄:${stu.age }</p>
<!-- 等价于stu.sex -->
<p>性别:${stu["sex"] }</p>
<!-- request.getAttribute("stu")
.getCourse().getId() -->
<p>课程ID:${stu.course.id }</p>
<!--
EL表达式的取值范围:
1)EL默认从4个隐含对象中依次取值
page->request->session->application
2)默认依次取值的规则,其意义在于简化EL
表达式,在取值时可以不用写对象名.
3)若希望打破默认规则,明确从某对象中取值,
则需要在表达式前面增加对象名.
requestScope.stu.name
sessionScope.stu.name
-->
<p>兴趣:${requestScope.stu.interests }</p>
<!-- 1.2访问时可以直接运算 -->
<p>年龄+5:${stu.age+5 }</p>
<p>介于20-30间:${stu.age>20 && stu.age<30 }</p>
<p>是否为空:${empty stu.interests }</p>
<!-- 1.3直接获取请求参数 -->
<p>参数:${param.user }</p>
</body>
</html>
student.jsp