实验15 MVC

ops/2024/10/21 6:32:13/

二、实验项目内容(实验题目)

编写代码,掌握MVC的用法。

三、源代码以及执行结果截图:

inputMenu.jsp:

<%@ page contentType="text/html" %>

<%@ page pageEncoding = "utf-8" %>

<jsp:useBean id="menu" class ="save.data.MenuBean" scope="session"/>

<style>

   #textStyle{

      font-family:宋体;font-size:36;color:blue

   }

</style>

<HTML><body bgcolor=#ffccff>

<form action="addMenu" id= textStyle method=post>

输入餐单(每次输入一个消费项目)<br>

名称:<input type=text name='foodName' id =textStyle value ='剁椒鱼头' size = 8 />

价格:<input type=text name='price' id =textStyle value ='26.9' size = 8 />

<br><input type=submit id=textStyle value="添加到餐单">

</form>

</body></HTML>

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>

web.xml:

<?xml version="1.0" encoding="utf-8"?>

<web-app>

    <!--  以下是web.xml文件新添加的内容 -->

    <servlet>

        <servlet-name>addMenu</servlet-name>

        <servlet-class>handle.data.HandleMenu_Servlet</servlet-class>

    </servlet>

    <servlet-mapping>

        <servlet-name>addMenu</servlet-name>

        <url-pattern>/addMenu</url-pattern>

    </servlet-mapping>

</web-app>

Food:

package save.data;

public class Food implements Comparable<Food>{

    String foodName ;  

    double price;     

    public void setFoodName(String name){

        foodName = name;

    }

    public String getFoodName(){

        return foodName;

    }

    public void setPrice(double d){

        price = d;

    }

    public double getPrice(){

        return price;

    }

    public int compareTo(Food food){

        return (int)(food.getPrice()*1000-price*1000);

    }

}

MenuBean:

package save.data;

import java.util.ArrayList;

import java.util.Collections;

public class MenuBean {

    String time ;

    String totalPrice;

    ArrayList<Food> foodList;

    public MenuBean(){

        foodList = new ArrayList<Food>();

    }

    public void addFood(Food food){

        foodList.add(food);

        Collections.sort(foodList);

    }

    public void removeFood(int index){

        if(index>=0){

           foodList.remove(index);

           Collections.sort(foodList);

        }

    }

    public String getFoodName(int index) {

        return foodList.get(index).getFoodName();

    }

    public double getPrice(int index) {  

        return foodList.get(index).getPrice();

    }

    public int size() {

        return foodList.size();

    }

    public void setTime(String time){

        this.time = time

    }

    public String getTime(){

        return time

    }

    public String getTotalPrice(){

        double sum = 0;

        for(Food food:foodList){

          sum += food.getPrice();

        }

        totalPrice = String.format("%.2f",sum);

        return totalPrice;

    }

}

HandleMenu_Servlet:

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 = new MenuBean();

           session.setAttribute("menu",menu);

       }

       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");

   }

}

运行结果图:

点餐:

增加菜单:

删除:


http://www.ppmy.cn/ops/31751.html

相关文章

SPRING-CLOUD从入门到精通

第一章> 1、微服务零基础 2、从X和H版本说起 3、Cloud组件 4、微服务架构 5、Eureka服务注册与发现 第二章> 6、Zookeeper 7、Consul 8、Ribbon均衡 9、OpenFeign 10、Hystrix断路器 第三章> 11、…

深度学习中损失函数和激活函数的选择

文章目录 前言 你需要解决什么问题&#xff1f; 你想预测数值吗&#xff1f; 你想预测分类结果吗&#xff1f; 回归&#xff1a;预测数值 分类&#xff1a;预测二元结果 分类&#xff1a;从多个类别中预测单个标签 分类&#xff1a;从多个类别中预测多个标签 总结表 前言 本篇…

【ZYNQ】Zynq 开发流程

Zynq 芯片架构由嵌入式处理器&#xff08;Processing System, PS&#xff09;与可编程逻辑&#xff08;Programmable Logic, PL&#xff09;&#xff0c;以及 PS 与 PL 之间的互联总线组成。本文主要介绍 Xilinx Zynq 芯片开发所使用的软件&#xff0c;包括 Vivado IDE 与 Xili…

基于Spring Boot的火车订票管理系统设计与实现

基于Spring Boot的火车订票管理系统设计与实现 开发语言&#xff1a;Java框架&#xff1a;springbootJDK版本&#xff1a;JDK1.8数据库工具&#xff1a;Navicat11开发软件&#xff1a;eclipse/myeclipse/idea 系统部分展示 前台首页功能界面图&#xff0c;在系统首页可以查看…

mac如何打开exe文件?如何mac运行exe文件 如何在Mac上打开/修复/恢复DMG文件

在macOS系统中&#xff0c;无法直接运行Windows系统中的.exe文件&#xff0c;因为macOS和Windows使用的是不同的操作系统。然而&#xff0c;有时我们仍然需要运行.exe文件&#xff0c;比如某些软件只有Windows版本&#xff0c;或者我们需要在macOS系统中运行Windows程序。 虽然…

ffmpeg 转换es流成为ps流

目的是将es流转换成为ps流 写入到文件中 #include <libavformat/avformat.h> #include <libavcodec/avcodec.h> #include <libavutil/avutil.h> #include <libavutil/timestamp.h>int main(int argc, char** argv) {const char* input_filename &qu…

Java项目:基于SSM框架实现的在线医疗服务系统(ssm+B/S架构+源码+数据库+毕业论文+开题报告)

一、项目简介 本项目是一套基于SSM框架实现的在线医疗服务系统 包含&#xff1a;项目源码、数据库脚本等&#xff0c;该项目附带全部源码可作为毕设使用。 项目都经过严格调试&#xff0c;eclipse或者idea 确保可以运行&#xff01; 该系统功能完善、界面美观、操作简单、功能…

node.js中path模块-路径处理,语法讲解

node中的path 模块是node.js的基础语法&#xff0c;实际开发中&#xff0c;我们通过使用 path 模块来得到绝对路径&#xff0c;避免因为相对路径带来的找不到资源的问题。 具体来说&#xff1a;Node.js 执行 JS 代码时&#xff0c;代码中的路径都是以终端所在文件夹出发查找相…