MVC简介
MVC是一个架构,或者说是一个设计模式,它就是强制性使应用程序的输入,处理和输出分开。将一个应用程序分为三个部分:Model,View,Controller。
Java Web
JSP开发模式
jsp开发模式的发展
1.模式1:(适合小型项目的技术的开发)
a.第一版本号。纯jsp(封装数据。处理数据,显示数据)
b.第二版本号,Jsp+JavaBean.
jsp:收集数据,显示数据
JavaBean:封装、处理
2.模式2:servlet+Jsp+JavaBean(是mvc在java中的详细的实现,是java技术实现的详细的内容)
a.servlet:负责协调jsp和javabean,获得数据,处理数据(业务逻辑),封装到javabean中,选择jsp去显示数据。
b.jsp:显示
c.JavaBean:封装和简单处理
思想:业务处理与显示数据相分离。
对于javaweb开发的mvc模式:
servlet(controller)+jsp(view)+javabean(model)
JavaBean+JSP+Servlet+Filter MVC架构实现购物车
1.JavaBean构建Product类封装实体商品信息
package com.bean;
import java.io.Serializable;
public class Product implements Serializable {
private Integer pid;
private String pname;
private Float price;
private Integer num;
private Float account;
public Product() {
}
public Product(String pname, Float price, Integer num) {
this.pname = pname;
this.price = price;
this.num = num;
}
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
public Integer getNum() {
return num;
}
public void setNum(Integer num) {
this.num = num;
}
public Float getAccount() {
account = price*num;
return account;
}
public void setAccount(Float account) {
this.account = account;
}
}
2.构建商品控制器ProductServlet类来处理数据
package com.servlet;
import com.bean.Product;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
@WebServlet(name="ProductServlet", urlPatterns = {"/servlet/ProductServlet"})
public class ProductServlet extends javax.servlet.http.HttpServlet{
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
/** 获取session对象 */
HttpSession session = req.getSession();
//从session中获取商品集合对象
List<Product> lspd = (List<Product>)session.getAttribute("lspd");
//获取用户传递的操作码
int op = Integer.parseInt(req.getParameter("op"));
Product pd=null;
switch (op){
case 1://add
if (lspd==null){
lspd = new ArrayList<>();
}
//从request属性中获取对象
pd = (Product) req.getAttribute("pd");
lspd.add(pd);
session.setAttribute("lspd",lspd);
resp.sendRedirect(req.getContextPath()+"/servlet/ProductServlet?op=5");
break;
case 2://updata
// if (lspd==null){
// out.println("没有任何商品的存在,请先<a href='"+req.getContextPath()+"/add.jsp'>添加</a>!");
// return;
// }
// //从request中获取商品属性
// pd = (Product) req.getAttribute("pd");
// //修改索引位置
// lspd.set(pd.getPid(),pd);
// //修改记忆中的session对象
// session.setAttribute("lspd",lspd);
// resp.sendRedirect(req.getContextPath()+"/servlet/ProductServlet?op=5");
// break;
if(lspd==null){
out.println("没有任何商品存在,请先<a href='"+req.getContextPath()+"/add.jsp'>添加</a>!");
return;
}
//从request属性中获取商品对象
pd= (Product)req.getAttribute("pd");
lspd.set(pd.getPid(),pd);//修改索引位置的商品对象
session.setAttribute("lspd",lspd);//修改记忆中的集合对象
resp.sendRedirect(req.getContextPath()+"/servlet/ProductServlet?op=5");
break;
case 3://delete
//获取删除的商品序号 对应在集合中的索引位置
int pid = Integer.parseInt(req.getParameter("index"));
//判断lspd是否为空,空就跳回添加,不空则移除
if (lspd==null){
out.println("没有任何商品,请先<a href='"+req.getContextPath()+"/add.jsp'>添加</a>!");
return;
}
lspd.remove(pid);
session.setAttribute("lspd",lspd);
resp.sendRedirect(req.getContextPath()+"/servlet/ProductServlet?op=5");
break;
case 4://select
pid=Integer.parseInt(req.getParameter("index"));
if (lspd==null){
out.println("没有商品,请先<a href='"+req.getContextPath()+"/add.jsp'>添加</a>");
return;
}
Product oldpd = lspd.get(pid);
oldpd.setPid(pid);//设置商品编号到商品对象,准备修改操作
req.setAttribute("oldpd",oldpd);
//转发到updata.jsp
req.getRequestDispatcher("/updata.jsp").forward(req,resp);
break;
case 5://show
if (lspd==null){
out.println("没有商品,请先<a href='"+req.getContextPath()+"/add.jsp'>添加</a>");
return;
}
float sum=0;
for (Product opd:lspd){
sum+= opd.getAccount();
}
session.setAttribute("sum",sum);
resp.sendRedirect(req.getContextPath()+"/list.jsp");
break;
}
out.flush();
out.close();
}
}
3.构建过滤器类CharacterFilter类来做字符处理
package com.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter(filterName = "CharacterFilter",urlPatterns = "/*")
public class CharacterFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
//Filter.super.init(filterConfig);
System.out.println("正在初始化过滤器信息......");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("正在处理数据字符编码......");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
chain.doFilter(request,response);
}
@Override
public void destroy() {
//Filter.super.destroy();
System.out.println("正在摧毁过滤器对象......");
}
}
4.JSP做页面的呈现
add.jsp:收集网页端输入的数据
<%--
Created by IntelliJ IDEA.
User: 顾念思成
Date: 2021/5/7
Time: 16:39
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加商品信息</title>
<script src="../SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>
<link href="../SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css">
</head>
<body>
<center>
<h3>添加商品信息</h3>
<form name="form1" method="post" action="doservice.jsp?op=1">
<table width="538" border="1">
<tr>
<td width="136">商品:</td>
<td width="386"><label for="textfield"></label>
<input type="text" name="pname" id="textfield"></td>
</tr>
<tr>
<td>价格:</td>
<td><label for="textfield2"></label>
<input type="text" name="price" id="textfield2"></td>
</tr>
<tr>
<td>数量:</td>
<td><label for="textfield3"></label>
<span id="sprytextfield1">
<input type="text" name="num" id="textfield3">
<span class="textfieldRequiredMsg">需要提供一个值。</span><span class="textfieldInvalidFormatMsg">格式无效。</span></span></td>
</tr>
<tr bgcolor="#00FFFF">
<td colspan="2" align="center" valign="middle"><input type="submit" name="button" id="button" value="提交">
<input type="reset" name="button2" id="button2" value="重置"></td>
</tr>
</table>
</form>
<p> </p>
</center>
<script type="text/javascript">
var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1", "integer");
</script>
</body>
</html>
doservice.jsp:封装收集的商品信息,收集的商品信息会保存在request属性中,并转发给ProductServlet进行处理
<%--
Created by IntelliJ IDEA.
User: 顾念思成
Date: 2021/5/7
Time: 16:39
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:useBean id="pd" class="com.bean.Product" scope="request"/>
<jsp:setProperty name="pd" property="*"/>
<%
String op = request.getParameter("op");
%>
<jsp:forward page="servlet/ProductServlet">
<jsp:param name="op" value="<%=op%>"/>
</jsp:forward>
<html>
<head>
<title>Title</title>
</head>
<body>
</body>
</html>
list.jsp:网页端显示商品信息列表
<%--
Created by IntelliJ IDEA.
User: 顾念思成
Date: 2021/5/7
Time: 16:39
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>显示商品信息</title>
</head>
<c:if test="${lspd==null}">
<c:redirect url="add.jsp"></c:redirect>
</c:if>
<body>
<center>
<h3>显示商品信息</h3>
<table width="900" border="1" align="center">
<tr align="center" valign="middle">
<td>编号</td>
<td>商品</td>
<td>价格</td>
<td>数量</td>
<td>小计</td>
<td>操作</td>
</tr>
<c:forEach items="${lspd}" var="pd" varStatus="vs">
<tr align="center" valign="middle">
<td>${vs.index+1}</td>
<td>${pd.pname}</td>
<td>${pd.price}</td>
<td>${pd.num}</td>
<td>${pd.account}</td>
<td><a href="servlet/ProductServlet?op=3&index=${vs.index}">删除</a> <a href="servlet/ProductServlet?op=4&index=${vs.index}">修改</a></td> </tr>
</c:forEach>
<tr align="center" valign="middle">
<td>总计:</td>
<td colspan="4">${sum}元</td>
</tr>
</table>
<br>
<h4>返回<a href="add.jsp">添加</a>!</h4>
</center>
</body>
</html>
updata.jsp:网页端更新商品信息
<%--
Created by IntelliJ IDEA.
User: 顾念思成
Date: 2021/5/7
Time: 16:39
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:if test="${oldpd==null}">
<c:redirect url="servlet/ProductServlet?op=5"/>
</c:if>
<html>
<head>
<title>修改商品信息</title>
<script src="../SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>
<link href="../SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css">
</head>
<body>
<center>
<h3>修改商品信息</h3>
<form name="form1" method="post" action="/prjMVC购物车/doservice.jsp?op=2">
<table width="538" border="1">
<tr>
<td width="136">商品:</td>
<td width="386"><label for="textfield"></label>
<input type="text" name="pname" id="textfield" value="${oldpd.pname}">
<input type="hidden" name="pid" id="pid" value="${oldpd.pid}">
</td>
</tr>
<tr>
<td>价格:</td>
<td><label for="textfield2"></label>
<input type="text" name="price" id="textfield2" value="${oldpd.price}"></td>
</tr>
<tr>
<td>数量:</td>
<td><label for="textfield3"></label>
<span id="sprytextfield1">
<input type="text" name="num" id="textfield3" value="${oldpd.num}">
<span class="textfieldRequiredMsg">需要提供一个值。</span><span class="textfieldInvalidFormatMsg">格式无效。</span></span></td>
</tr>
<tr bgcolor="#00FFFF">
<td colspan="2" align="center" bgcolor="#FFFF99"><input type="submit" name="button" id="button" value="提交" />
<input type="reset" name="button2" id="button2" value="重置" /></td>
</tr>
</table>
<br>
<hr>
显示<a href="list.jsp">列表</a>!
</form>
<p> </p>
</center>
<script type="text/javascript">
var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1", "integer");
</script>
</body>
</html>