首页 > 其他分享 >水果系统项目分析pro09-fruit1.4-thymelea

水果系统项目分析pro09-fruit1.4-thymelea

时间:2023-10-03 22:11:33浏览次数:35  
标签:thymelea Integer 项目分析 fruit import fruit1.4 atguigu com public

水果系统项目分析pro09-fruit1.4-thymelea

基本架构

image-20231003213648595

  • IndexServlet为继承的Servlet容器类

  • ViewBaseServlet为复制的thymelea源代码用来渲染页面

  • index.html为前端界面

index.html

<html xmlns:th="http://www.thymeleaf.org">
	<head>
		<meta charset="utf-8">
		<link rel="stylesheet" href="css/index.css">
	</head>
	<body>
		<div id="div_container">
			<div id="div_fruit_list">
				<p class="center f30">欢迎使用水果库存后台管理系统</p>
				<table id="tbl_fruit">
					<tr>
						<th class="w20">名称</th>
						<th class="w20">单价</th>
						<th class="w20">库存</th>
						<th>操作</th>
					</tr>
					<tr th:if="${#lists.isEmpty(session.fruitList)}">
						<td colspan="4">对不起,库存为空!</td>
					</tr>
					<tr th:unless="${#lists.isEmpty(session.fruitList)}" th:each="fruit : ${session.fruitList}">
						<td th:text="${fruit.fname}">苹果</td>
						<td th:text="${fruit.price}">5</td>
						<td th:text="${fruit.fcount}">20</td>
						<td><img src="imgs/del.jpg" class="delImg"/></td>
					</tr>
				</table>
			</div>
		</div>
	</body>
</html>

上面是一个前端界面用来显示从session会话域里面获取过来的东西.并且使用thymelea渲染出来.

index.css

*{
	color: threeddarkshadow;
}
body{
	margin:0;
	padding:0;
	background-color:#808080;
}
div{
	position:relative;
	float:left;
}

#div_container{
	width:80%;
	height:100%;
	border:0px solid blue;
	margin-left:10%;
	float:left;
	background-color: honeydew;
	border-radius:8px;
}
#div_fruit_list{
	width:100%;
	border:0px solid red;
}
#tbl_fruit{
	width:60%;
	line-height:28px;
	margin-top:16px;
	margin-left:20%;
}
#tbl_fruit , #tbl_fruit tr , #tbl_fruit th , #tbl_fruit td{
	border:1px solid gray;
	border-collapse:collapse;
	text-align:center;
	font-size:16px;
	font-family:"黑体";
	font-weight:lighter;
	
}
.w20{
	width:20%;
}
.delImg{
	width:24px;
	height:24px;
}
.btn{
	border:1px solid lightgray;
	width:80px;
	height:24px;
}

.center{
	text-align:center;
}
.f30{
	font-size: 30px;
}

web.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <!-- 配置上下文参数 -->
    <context-param>
        <param-name>view-prefix</param-name>
        <param-value>/</param-value>
    </context-param>
    <context-param>
        <param-name>view-suffix</param-name>
        <param-value>.html</param-value>
    </context-param>
</web-app>

这个web.xml中的配置上下文参数就是说通过这个配置文件的逻辑视图和物理视图让Servlet通过thymelea技术来渲染前端界面.

IndexServlet.java

package com.atguigu.fruit.servlets;

import com.atguigu.fruit.dao.FruitDAO;
import com.atguigu.fruit.dao.impl.FruitDAOImpl;
import com.atguigu.fruit.pojo.Fruit;
import com.atguigu.myssm.myspringmvc.ViewBaseServlet;

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 javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

//Servlet从3.0版本开始支持注解方式的注册
@WebServlet("/index")
public class IndexServlet extends ViewBaseServlet {
    @Override
    public void doGet(HttpServletRequest request , HttpServletResponse response)throws IOException, ServletException {
        FruitDAO fruitDAO = new FruitDAOImpl();
        List<Fruit> fruitList = fruitDAO.getFruitList();
        //保存到session作用域
        HttpSession session = request.getSession() ;
        session.setAttribute("fruitList",fruitList);
        //此处的视图名称是 index
        //那么thymeleaf会将这个 逻辑视图名称 对应到 物理视图 名称上去
        //逻辑视图名称 :   index
        //物理视图名称 :   view-prefix + 逻辑视图名称 + view-suffix
        //所以真实的视图名称是:      /       index       .html
        super.processTemplate("index",request,response);
    }
}
  1. 先使用fruitDAO获取了数据库中的全部数据
  2. 然后将fruitlist保存到session作用域.
  3. 然后使用父类的processTemplate方法来渲染index.html界面.

下面是几个附加的类

Fruit.java

package com.atguigu.fruit.pojo;

public class Fruit {
    private Integer fid ;
    private String fname ;
    private Integer price ;
    private Integer fcount ;
    private String remark ;

    public Fruit(){}

    public Fruit(Integer fid, String fname, Integer price, Integer fcount, String remark) {
        this.fid = fid;
        this.fname = fname;
        this.price = price;
        this.fcount = fcount;
        this.remark = remark;
    }

    public Integer getFid() {
        return fid;
    }

    public void setFid(Integer fid) {
        this.fid = fid;
    }

    public String getFname() {
        return fname;
    }

    public void setFname(String fname) {
        this.fname = fname;
    }

    public Integer getPrice() {
        return price;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }

    public Integer getFcount() {
        return fcount;
    }

    public void setFcount(Integer fcount) {
        this.fcount = fcount;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    @Override
    public String toString() {
        return fid + "\t\t" + fname + "\t\t" + price +"\t\t" + fcount +"\t\t" + remark ;
    }
}

FruitDAO.java

package com.atguigu.fruit.dao;

import com.atguigu.fruit.pojo.Fruit;

import java.util.List;

public interface FruitDAO {
    //获取所有的库存列表信息
    List<Fruit> getFruitList();
}

这里面是存放的Servlet里面的抽象方法

FruitDAOImpl.java

package com.atguigu.fruit.dao.impl;

import com.atguigu.fruit.dao.FruitDAO;
import com.atguigu.fruit.pojo.Fruit;
import com.atguigu.myssm.basedao.BaseDAO;

import java.util.List;

public class FruitDAOImpl extends BaseDAO<Fruit> implements FruitDAO {
    @Override
    public List<Fruit> getFruitList() {
        return super.executeQuery("select * from t_fruit");
    }
}

标签:thymelea,Integer,项目分析,fruit,import,fruit1.4,atguigu,com,public
From: https://www.cnblogs.com/harper886/p/17741728.html

相关文章

  • 个人项目分析互评
    目录:简介项目要求代码测试代码分析总结   正文:一、简介   本博客是对结对编程同学所完成的个人项目的分析与总结,他所完成的项目是中小学生数学试卷自动生成系统。对方编写所用语言是java,经过测试均能正常运行。 二、项目要求本次个人项目的需求如下:用户:......
  • HNU个人项目分析——
    概要•一.简介• 二.项目要求• 三.功能测试• 四.代码分析• 五.总结值得学习的地方需要改进的地方• 六.期待与展望 一.  简介1.项目名称:中小学数学卷子自动生成程序2.编程语言:JAVA3.基本情况:本博客是对许晋源同学的个人项目......
  • HNU个人项目分析互评
    笔者:万宇龙项目作者:梁钰项目目标个人项目:中小学数学卷子自动生成程序用户:小学、初中和高中数学老师。功能:命令行输入用户名和密码,两者之间用空格隔开(程序预设小学、初中和高中各三个账号,具体见附表),如果用户名和密码都正确,将根据账户类型显示“当前选择为XX出题”,XX为小......
  • 个人项目分析——中小学生数学卷子自动生成程序
    目录一.简介二.项目要求三.代码分析整体架构核心代码四.功能测试五.优缺点总结 一.简介本篇博客为对廖心怡同学的个人编程项目“中小学数学卷子自动生成程序”的分析与总结,在阅读代码的过程中学习到了许多优点,也发现了一些代码书写、代码结构等方......
  • thymeleaf获取作用域的值
    参考:https://blog.csdn.net/qq_43634655/article/details/126424130从session取值后端代码 Map<String,Object>map=newHashMap<>(); map.put("id","1001"); map.put("name","小虎"); map.put("age","18......
  • 【Spring Boot】Thymeleaf 模板引擎
     Thymeleaf组成:标签+表达式,标签是Thymeleaf的语法结构,而表达式就是语法里的内容实现  pom.xml添加依赖包<!--模板引擎Thymeleaf依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-b......
  • springBoot spring6 无法加载 thymeleaf的,在html页面中无法智能感知 th:这些
    网上所有的坑我都试过了,还是无法解决问题,@ControllerpublicclassSellController{@RequestMapping("/test01")/*@ResponseBody*/publicStringindex(){return"test01";}}”test01“下面永远是波浪线,无论把网上查出来的解决问题的方案我......
  • SpringBoot创建Thymeleaf
    1.pom.xml导入依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency> 2.thymelef的默认配置文件springboot工程默认有一个templates文件夹,所有的html页面都放这个文件夹里。......
  • springboot 框架国际化 + thymeleaf
    项目目录结构注意:导入thymeleaf,web的pom依赖<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency><dependency> <groupId>org.springframework.boot&l......
  • Jrasp开源项目分析报告(一)
    安全现状传统的防火墙(WAF)部署在Web应用前线,通过HTTP/HTTPS策略对Web应用保护,容易被绕过,并且难以对其进行优化,不能成为应用安全计划的唯一支柱。近些年常见的漏洞多为远程代码执行(RCE),扫描探测,各类命令注入。随着Web应用攻击手段变得复杂,基于请求特征的防护手段,已经不能满足企......