前言
前言
本章主要内容是MVC介绍、JSP中的MVC模式、和模型的生命周期与视图更新。
7.1 MVC模式介绍
模型-视图-控制器(Model-View-Controller),简称为MVC。MVC已经成为软件设计者必须熟练使用的开发模式。本章必须理解、掌握在JSP程序设计中怎样具体体现MVC开发模式(其他语言的程序设计是非常类似的,仅仅是具体使用的API不同而已)。
MVC是一种通过三部分构造一个软件或组件的理想办法。
模型(model) 用于存储数据的对象。
视图(view) 向控制器提交所需数据、显示模型中的数据。
控制器(controller) 负责具体的业务逻辑操作,即控制器根据视图提出的要求对数据做出(商业)处理,将有关结果存储到模型中,并负责让模型和视图进行必要的交互,当模型中的数据变化时,让视图更新显示。
7.2 JSP中的MVC模式
模型(Model) 一个或多个Javabean对象,用于存储数据。
视图(View) 一个或多个JSP页面,其作用是向控制器提交必要的数据和显示数据。JSP页面可以使用HTML标记、Javabean标记以及Java程序片或Java表达式来显示数据。视图的主要工作就是显示数据,对数据的逻辑操作由控制器负责。
控制器(Controller) 一个或多个servlet对象,根据视图提交的要求进行数据处理操作,并将有关的结果存储到Javabean中,然后servlet使用转发或重定向的方式请求视图中的某个JSP页面显示数据。比如让某个JSP页面通过使用Javabean标记、Java程序片或Java表达式显示控制器存储在Javabean中的数据。
7.3 模型的生命周期与视图更新
使用MVC模式和前面学习的JSP+JavaBean 模式有很大的不同。在JSP+JavaBean 模式中由JSP页面通过使用 useBean 标记:
<jsp:useBean id="名字”class ="创建 bean 的类”scope ="生命周期/>
创建 bean。而在MVC模式中,由制器 servlet 创建 bean,并将有关数据存储到所创建的 bean中,然后servlet 请求某个JSP页面使用 getProperty 动作标记:
<jsp:getProperty name ="名字" property = "bean 的属性"/>
显示bean中的数据。
在MVC模式中,当用控制器servlet 创建 bean 时,就可以使用 bean 类的带参数的构造方法。类中的方法的命名继续保留 getXxx 规则,但可以不守 setXxx 规则(有关规则细节见5.1.1节)。其理由是:我们不希望JSP 页面修改 JavaBean 中的数据,只需要它显示 bean 中的数据。
在MVC模式中,servlet 创建的 bean 也涉及生命周期。生命周期分为 request beansessionbean 和application bean。
7.3.1 request bean
1.bean的创建
servlet创建request bean的步骤如下:
(1)用BeanClass类的某个构造方法创建bean对象,例如:
BeanClass bean=new BeanClass();
(2)
request.setAttribute("keyWord",bean);
2.视图更新
servlet所请求的JSP页面可以使用相应的标记显示该request bean中的数据。该JSP页面对请求做出响应之后,request bean所占有的内存被释放,结束自己的生命。
RequestDispatcher dispatcher= request.getRequestDispatcher("show.jsp");
dispatcher.forward(request,response);
servlet所请求的JSP页面,比如show.jsp页面可以使用如下标记获得servlet所创建的request bean:
<jsp:useBean id="keyWord" class="save.data.BeanClass" scope="request"/>
该标记中的id是servlet所创建的bean的id。
然后JSP页面可以使用相应的标记或程序片显示该request bean中的数据。
7.3.2 session bean
1.bean的创建
servlet创建session bean的步骤如下:
(1)用BeanClass类的某个构造方法创建bean对象,例如:
BeanClass bean=new BeanClass();
(2)
HttpSession session=request.getSession(true);
session.setAttribute("keyWord",bean);
2.视图更新
Web服务目录的各个JSP都可以使用
<jsp:useBean id="keyWord" class="save.data.BeanClass" scope="session"/>
标记获得servlet所创建的session bean,然后使用相应的标记或程序片显示该request bean中的数据。
如果servlet希望某个JSP显示其中的数据,可以使用RequestDispatcher对象转发到该页面,也可以使用HttpServletResponse类中的重定向方法(sendRedirect)定向到该页面。
7.3.3 application bean
1.bean的创建
ervlet创建application bean的步骤如下:
(1)用BeanClass类的某个构造方法创建bean对象,例如:
BeanClass bean=new BeanClass();
(2)
getServletContext().setAttribute("keyWord",bean);
2.视图更新
servlet建bean的第(2)步骤决定了bean为application bean。
用户在访问Web服务目录的各个JSP中都可以使用
<jsp:useBean id="keyWord" class="save.data.BeanClass" scope="application"/>
标记获得servlet所创建的application bean,然后使用相应的标记或程序片显示该application bean中的数据,例如,使用
<jsp:getProperty name="keyWord" property="bean的变量"/>
标记显示该application bean中的数据。
如果servlet希望某个JSP显示其中的数据,可以使用RequestDispatcher对象向该JSP页面发出请求,也可以使用HttpServletResponse类中的重定向方法(sendRedirect)。
7.4MVC模式的简单例子
例子代码:
package handle.data;
import save.data.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Example7_1_Servlet extends HttpServlet{
public void init(ServletConfig config) throws ServletException{
super.init(config);
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
Example7_1_Bean digitBean = null;
digitBean = new Example7_1_Bean(); //创建Javabean对象.
//digitBean 是request bean:
request.setAttribute("digitBean",digitBean);
String str1 = request.getParameter("numberOne");
String str2 = request.getParameter("numberTwo");
if(str1==null||str2==null)
return;
if(str1.length()==0||str2.length()==0)
return;
double numberOne = Double.parseDouble(str1);
double numberTwo = Double.parseDouble(str2);
String operator = request.getParameter("operator");
double result=0;
if(operator.equals("+"))
result = numberOne+numberTwo;
else if(operator.equals("-"))
result = numberOne-numberTwo;
else if(operator.equals("*"))
result = numberOne*numberTwo;
else if(operator.equals("/"))
result = numberOne/numberTwo;
digitBean.setNumberOne(numberOne); //将数据存储在digitBean中
digitBean.setNumberTwo(numberTwo);
digitBean.setOperator(operator);
digitBean.setResult(result);
//请求example7_1.jsp显示digitBean中的数据:
RequestDispatcher dispatcher= request.getRequestDispatcher("example7_1.jsp");
dispatcher.forward(request,response);
}
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
doPost(request,response);
}
}
<%@ page contentType="text/html" %>
<%@ page pageEncoding = "utf-8" %>
<jsp:useBean id="digitBean" class ="save.data.Example7_1_Bean" scope="request"/>
<style>
#tom{
font-family:宋体;font-size:26;color:blue
}
</style>
<HTML><body bgcolor=#ffccff>
<form action="computer" id =tom method=post>
<table>
<tr><td id =tom> 输入两个数:</td>
<td id =tom>
<input type=text name="numberOne"
value=<%= digitBean.getNumberOne() %> id =tom size=6/></td>
<td><input type=text name="numberTwo"
value=<%=digitBean.getNumberTwo()%> id =tom size=6/></td>
</tr>
<tr><td id =tom>选择运算符号:</td>
<td id =tom>
<select id =tom name="operator">
<option value="+">+(加)
<option value="-">-(减)
<option value="*">*(乘)
<option value="/">/(除)
</select>
</td>
<td><input type="submit" id =tom value="提交" name="sub"/></td>
</tr>
</table></form>
<p id=tom>
运算结果:
<jsp:getProperty name="digitBean" property="numberOne"/>
<jsp:getProperty name="digitBean" property="operator"/>
<jsp:getProperty name="digitBean" property="numberTwo"/> =
<jsp:getProperty name="digitBean" property="result"/>
</p></body></HTML>
package handle.data;
import save.data.ExpressWish;
import save.data.ExpressWish_Bean;
import java.util.*;
import java.io.*;
import java.time.LocalDateTime;
import javax.servlet.*;
import javax.servlet.http.*;
public class ExpressWish_Servlet extends HttpServlet{
int index; //做id。
public void init(ServletConfig config) throws ServletException{
super.init(config);
}
synchronized long getIndex() { //synchronized修饰的方法
index = index+1;
return index;
}
public void service(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
request.setCharacterEncoding("utf-8");
ExpressWish_Bean wishWallBean = null; //wishWallBean存放表白墙内容。
ServletContext application = getServletContext();
wishWallBean = (ExpressWish_Bean)application.getAttribute("wishWallBean");
if(wishWallBean == null ){//wishWallBean不存在就创建wishWallBean。
wishWallBean = new ExpressWish_Bean();
application.setAttribute("wishWallBean",wishWallBean);//appication bean。
}
String peopleName = request.getParameter("peopleName");//表白者。
String title = request.getParameter("title"); //标题。
String content = request.getParameter("contents");//表白内容。
ExpressWish wish = new ExpressWish();
if(peopleName.length()==0||title.length()==0||content.length()==0){
response.sendRedirect("example7_2.jsp");
return;
}
wish.setPeopleName(peopleName);
wish.setTitle(title);
wish.setContent(content);
LocalDateTime dateTime = LocalDateTime.now();
String str = dateTime.toString();
String time =str.substring(0,str.lastIndexOf("."));//不要纳秒。
wish.setDateTime(time);
long number = getIndex();
wish.setId(""+number);
wishWallBean.addExpressWish(""+number,wish);//添加一条表白。
response.sendRedirect("example7_2_show.jsp"); //显示表白墙。
}
}
<%@ page contentType="text/html" %>
<%@ page pageEncoding = "utf-8" %>
<HTML>
<style>
#tom{
font-family:宋体;font-size:18;color:blue
}
</style>
<body bgcolor = #ffccff>
<form action="handleExpress" id="tom" method="post" >
表白者:<input type="text" id = "tom" name="peopleName" size = 28/>
<br>标题:<input type="text" id = "tom" name="title" size = 30/>
<br>内容:<br>
<textArea name="contents" id = "tom" rows="10" cols=36 >
</textArea>
<br><input type="submit" id="tom" value="提交表白" name="submit"/>
</form>
<p id="tom">
<a href="example7_2_show.jsp">查看表白墙</a>
</p></body></HTML>
<%@ page contentType="text/html" %>
<%@ page pageEncoding = "utf-8" %>
<jsp:useBean id="wishWallBean" class ="save.data.ExpressWish_Bean" scope="application"/>
<HTML><body bgcolor = pink>
<p style="font-family:宋体;font-size:18;color:blue">
管理员删除表白的页面。
<form action="" method=post >
输入密码:<input type="password" name="password" size=12 /><br>
输入表白id:<input type="text" name="peopleId" size=6 />
<br><input type="submit" name="submit" value="删除"/>
</form>
<% request.setCharacterEncoding("utf-8");
String password=request.getParameter("password");
String id=request.getParameter("peopleId");
if(password == null ) password = "";
if(id == null ) id = "";
if(password.equals("123456")){
wishWallBean.removeExpressWish(id);
}
%>
<a href="example7_2_show.jsp">查看表白墙</a>
</p></body></HTML>
<%@ page contentType="text/html" %>
<%@ page pageEncoding = "utf-8" %>
<jsp:useBean id="wishWallBean" class ="save.data.ExpressWish_Bean" scope="application"/>
<style>
#tom{
font-family:宋体;font-size:26;color:blue
}
</style>
<HTML><body bgcolor=white>
<table border=1>
<tr><th id=tom>id</th><th id=tom>表白人</th><th id=tom>标题</th>
<th id=tom>时间</th><th id=tom>表白内容</th>
<% for(int i=0;i<wishWallBean.size();i++){
out.print("<tr>");
out.print("<td id=tom>"+wishWallBean.getId(i)+"</td>");
out.print("<td id=tom>"+wishWallBean.getPeopleName(i)+"</td>");
out.print("<td id=tom>"+wishWallBean.getTitle(i)+"</td>");
out.print("<td id=tom>"+wishWallBean.getDateTime(i)+"</td>");
out.print("<td ><textArea rows=5 cols=20 id=tom>"+wishWallBean.getContent(i)+
"</textArea></td>");
out.print("</tr>");
}
%> </table>
<a id =tom href="example7_2.jsp">去表白</a>
</body></HTML>
实验代码:
1.
<%@ page contentType="text/html" %>
<%@ page pageEncoding = "utf-8" %>
<jsp:useBean id="seriesData" class ="save.data.Series_Bean" scope="request"/>
<style>
#tom{
font-family:宋体;font-size:28;color:blue
}
</style>
<HTML><body bgcolor=#FFBBFF>
<form action="computeSum" id = tom method="post" >
等差级数求和:
<br>输入首项:<input type=text id=tom name="firstItem" size=4>
输入公差(公比):<input type=text id=tom name="var" size=4>
求和项数:<input type=text id=tom name="number" size=4>
<input type=submit name = "submit" id = tom value="提交(求等差级数和)" />
<input type=submit name = "submit" id = tom value="提交(求等比级数和)">
</form>
<table border=1 id = tom>
<tr>
<th> 级数的首项</th>
<th> <jsp:getProperty name="seriesData" property="name"/></th>
<th> 所求项数</th>
<th> 求和结果</th>
</tr>
<td><jsp:getProperty name="seriesData" property="firstItem"/></td>
<td><jsp:getProperty name="seriesData" property="var"/></td>
<td><jsp:getProperty name="seriesData" property="number"/></td>
<td><jsp:getProperty name="seriesData" property="sum"/></td>
</tr>
</table>
</body></HTML>
package handle.data;
import save.data.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ComputeSum_Servlet extends HttpServlet{
public void init(ServletConfig config) throws ServletException{
super.init(config);
}
public void service(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
Series_Bean seriesData = new Series_Bean(); //创建bean。
request.setCharacterEncoding("utf-8");
request.setAttribute("seriesData",seriesData);//request bean。
String mess =request.getParameter("submit");
String firstItem = request.getParameter("firstItem");//首项。
String var = request.getParameter("var");//或公比公差。
String number = request.getParameter("number");//求和项数。
if(firstItem.length()==0||var.length()==0||number.length() == 0){
response.sendRedirect("inputNumber.jsp");
return;
}
if(mess.contains("等差")) {
compute(firstItem,var,number,seriesData,0);
}
else if(mess.contains("等比")) {
compute(firstItem,var,number,seriesData,1);
}
//请求inputNumber.jsp显示seriesData中的数据
RequestDispatcher dispatcher=
request.getRequestDispatcher("inputNumber.jsp");
dispatcher.forward(request,response);
}
void compute(String firstItem,String var,String number,
Series_Bean seriesData,int type){
double a=Double.parseDouble(firstItem);
double d=Double.parseDouble(var);
int n=Integer.parseInt(number);
seriesData.setFirstItem(a); //将数据存储在数据模型seriesData中。
seriesData.setVar(d);
seriesData.setNumber(n);
double sum=0,item=a;
int i=1;
if(type == 0) {
seriesData.setName("等差级数的公差");
while(i<=n){ //计算等差数列的和
sum=sum+item;
i++;
item=item+d;
}
seriesData.setSum(sum);
}
else if(type == 1){
seriesData.setName("等比级数的公比");
while(i<=n){ //计算等比数列的和
sum=sum+item;
i++;
item=item*d;
}
seriesData.setSum(sum);
}
}
}
2.
package handle.data;
import save.data.Food;
import save.data.MenuBean;
import java.util.*;
import java.io.*;
import java.time.LocalTime;
import javax.servlet.*;
import javax.servlet.http.*;
public class HandleMenu_Servlet extends HttpServlet{
public void init(ServletConfig config) throws ServletException{
super.init(config);
}
public void service(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
request.setCharacterEncoding("utf-8");
MenuBean menu = null; //餐单。
HttpSession session=request.getSession(true);
menu = (MenuBean)session.getAttribute("menu");
if(menu == null ){//menu不存在就创建menu。
menu = new MenuBean();
session.setAttribute("menu",menu);//sesion bean。
}
String foodName = request.getParameter("foodName");//食物名称。
String price = request.getParameter("price");
Food food = new Food();
if(foodName.length()==0||price.length()==0){
response.sendRedirect("inputMenu.jsp");
return;
}
food.setFoodName(foodName);
food.setPrice(Double.parseDouble(price));
LocalTime dateTime = LocalTime.now();
String str = dateTime.toString();
String time =str.substring(0,str.lastIndexOf("."));//不要纳秒。
menu.setTime(time);
menu.addFood(food);//添加一道食物。
response.sendRedirect("showMenu.jsp"); //显示餐单。
}
}
<%@ page contentType="text/html" %>
<%@ page pageEncoding = "utf-8" %>
<jsp:useBean id="menu" class ="save.data.MenuBean" scope="session"/>
<style>
#tom{
font-family:宋体;font-size:26;color:blue
}
</style>
<HTML><body bgcolor=pink>
<table border=1>
<tr><th id=tom>序号</th><th id=tom>食物名称</th><th id=tom>价格</th>
<% request.setCharacterEncoding("utf-8");
String index = request.getParameter("删除");
if(index!=null){
menu.removeFood(Integer.parseInt(index));
}
for(int i=0;i<menu.size();i++){
out.print("<tr>");
out.print("<td id=tom>"+(i+1)+"</td>");
out.print("<td id=tom>"+menu.getFoodName(i)+"</td>");
out.print("<td id=tom>"+menu.getPrice(i)+"</td>");
out.print("<td ><form action ="+"showMenu.jsp"+" method=post>");
out.print("<input type = hidden name = 删除 value = "+i+" />");
out.print("<input type = submit value = 删除该食物 />");
out.print("</form></td>");
out.print("</tr>");
}
%> </table>
<p id = tom>
餐单总额(共有<%=menu.size()%>道食物):
<jsp:getProperty name="menu" property="totalPrice" /><br>
点餐时间:
<jsp:getProperty name="menu" property="time" /></p>
<a id =tom href="inputMenu.jsp">继续点餐</a>
</body></HTML>
总结
MVC模式的核心思想是有效地组合“视图”“模型”和“控制 器”。在JSP 技术中,
是一个或多个JSP 页面,其作用主要是向控制器提交必要的数据和为模型提供数搬示: 模型是一个或多个JavaBean 对象,用于存储数据;控制器是一个或多个 servlel象,根据视图提交的要求进行数据处理操作,并将有关的结果存储到 JavaBean中,然servlet 使用重定向方式请求视图中的某个JSP页面更新显示。
在 MVC 模式中,模型也可以由控制器负责创建和初始化。
以上就是本章内容。