当前位置: 首页 > news >正文

网站架构包含哪几个部分一个网站如何做cdn加速

网站架构包含哪几个部分,一个网站如何做cdn加速,百度产品,最新新闻热点事件素材目录一. 前提条件1.1 需求1.2 分析二. 准备2.1 自定义注解2.2 封装Excel的实体类三. 前台四. Controller层五. Service层#x1f4aa;#x1f4aa;#x1f4aa;六. 效果一. 前提条件 1.1 需求 从指定的Excel模板中读取数据#xff0c;将读取到的数据存储到数据库中。 1.2… 目录一. 前提条件1.1 需求1.2 分析二. 准备2.1 自定义注解2.2 封装Excel的实体类三. 前台四. Controller层五. Service层六. 效果一. 前提条件 1.1 需求 从指定的Excel模板中读取数据将读取到的数据存储到数据库中。 1.2 分析 需要用到 poi 读取Excel使用自定义注解标记Excel单元格的行列数据类型方便读取数据需要使用 hibernate validation 校验数据前台需要使用 FormData() 对象向后台传入文件需要指定只能上传Excel类型的文件读取到的数据依次的get和set到entity中很麻烦需要用到 反射 进行封装 二. 准备 2.1 自定义注解 校验项目不为空 import javax.validation.Constraint; import javax.validation.constraints.NotEmpty; import javax.validation.Payload; import javax.validation.ReportAsSingleViolation; import java.lang.annotation.*;Documented Target({ ElementType.FIELD }) Retention(RetentionPolicy.RUNTIME) Constraint(validatedBy {}) NotEmpty ReportAsSingleViolation public interface ValidateNotEmpty {String msgArgs() default ;// 1001E请输入{msgArgs}。String message() default {1001E};Class?[] groups() default {};Class? extends Payload[] payload() default {}; }标记Excel单元格的行列属性信息的注解 import java.lang.annotation.*;Documented Retention(RetentionPolicy.RUNTIME) Target({ElementType.FIELD}) public interface ExcelCellAnnotation {// Excel单元格行的indexint rowIndex() default 0;// Excel单元格列的indexint columnIndex() default 0;// Excel单元格列的类型,默认是字符串类型java.lang.Class type() default String.class; }2.2 封装Excel的实体类 以「契約者申請日」这个项目为例说明 在Excel模板中的位置为第2行在Excel模板中的位置为第22列在Excel模板中的数据类型为 Date 类型 import lombok.Data;import java.util.Date;Data public class ExcelEntity {/*** 契約者申請日*/ValidateNotEmpty(msgArgs 契約者申請日)ExcelCellAnnotation(rowIndex 1, columnIndex 21, type Date.class)private String keiyakushaShinseibi;/*** フリガナ*/ValidateNotEmpty(msgArgs フリガナ)ExcelCellAnnotation(rowIndex 4, columnIndex 5)private String furikana;/*** 契約者氏名*/ValidateNotEmpty(msgArgs 契約者氏名)ExcelCellAnnotation(rowIndex 5, columnIndex 5)private String keiyakuShaName;/*** 性別_男*/ExcelCellAnnotation(rowIndex 5, columnIndex 20)private String sexMan;/*** 性別_女*/ExcelCellAnnotation(rowIndex 5, columnIndex 22)private String sexWoman;/*** 契約者住所*/ValidateNotEmpty(msgArgs 契約者住所)ExcelCellAnnotation(rowIndex 8, columnIndex 5)private String keiyakushaJyusho;/*** 契約者連絡先_携帯*/ValidateNotEmpty(msgArgs 契約者連絡先_携帯)ExcelCellAnnotation(rowIndex 10, columnIndex 8)private String keiyakushaPhone;/*** 契約者連絡先_メール*/ValidateNotEmpty(msgArgs 契約者連絡先_メール)ExcelCellAnnotation(rowIndex 10, columnIndex 16)private String keiyakushaMail; }三. 前台 通过acceptapplication/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet来实现只能上传Excel类型的数据上传成功或者失败都需要把文件上传input中的value置为空保证同一个文件可以上传多次 !DOCTYPE html html langen xmlns:thhttp://www.thymeleaf.org headmeta charsetUTF-8titleTitle/title /head bodyinput typefile idexcel acceptapplication/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet/button idbtn上传/button /body script srchttps://code.jquery.com/jquery-3.6.3.js/script script$(function () {$(#btn).click(function () {const formData new FormData();formData.append(excelFile, $(#excel).get(0).files[0]);$.ajax({url: /poi/excel,type: POST,data: formData,processData: false,contentType: false,success: function (data, status, xhr) {console.log(data);},error(xhr, textStatus, errorMessage) {console.log(textStatus);},complete(jqXHR, textStatus) {// 清空上传的文件,保证可以多次上传同一个文件$(#excel).val();}});})}) /script /html四. Controller层 通过MultipartHttpServletRequest来获取前台上传到后台的文件 import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.servlet.ModelAndView;import java.util.List;Controller RequestMapping(/poi) public class PoiController {Autowiredprivate PoiService service;GetMapping(/init)public ModelAndView init() {ModelAndView modelAndView new ModelAndView();modelAndView.setViewName(poiTest);return modelAndView;}// 处理上传到后台的文件PostMapping(/excel)public ResponseEntityVoid handleExcel(MultipartHttpServletRequest request) throws Exception {// 获取前台传入的文件ListMultipartFile file request.getMultiFileMap().get(excelFile);MultipartFile multipartFile file.get(0);// 将Excel中的文件读取到Entity中ExcelEntity excelEntity new ExcelEntity();service.readExcel(multipartFile, excelEntity);// 对Excel中的数据进行校验service.validateExcelData(excelEntity);// 告知操作成功return ResponseEntity.noContent().build();} }五. Service层 import org.apache.poi.ss.usermodel.DataFormatter; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.ObjectUtils; import org.springframework.util.ReflectionUtils; import org.springframework.util.StringUtils; import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; import org.springframework.web.multipart.MultipartFile;import javax.validation.ConstraintViolation; import java.io.InputStream; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Set;Service public class PoiService implements InitializingBean {// set方法private final static String methodAction set;// Excel单元格格式化对象private final static DataFormatter formatter new DataFormatter();// 日期格式化对象private static DateFormat dateformat null;// 输入校验对象Autowiredprivate LocalValidatorFactoryBean validator;// 进行一些初始化操作Overridepublic void afterPropertiesSet() {// 指定日期的格式化dateformat new SimpleDateFormat(yyyy-MM-dd);}// 读取Excel中的数据public void readExcel(MultipartFile multipartFile, ExcelEntity excelEntity) throws Exception {// 获取sheet页对象InputStream inputStream multipartFile.getInputStream();XSSFWorkbook sheets new XSSFWorkbook(inputStream);XSSFSheet sheet sheets.getSheetAt(0);// 单元格中的值String cellValue ;// 获取类上的所有属性(public和private都可以获取到)Field[] fields excelEntity.getClass().getDeclaredFields();for (Field field : fields) {// 如果该属性中没有ExcelCellAnnotation注解的话,跳过ExcelCellAnnotation annotation field.getAnnotation(ExcelCellAnnotation.class);if (ObjectUtils.isEmpty(annotation)) {continue;}// 根据行列的index,获取当前的单元格对象XSSFCell cell sheet// 获取属性上注解标记的单元格的行index.getRow(annotation.rowIndex())// 获取属性上注解标记的单元格的列index.getCell(annotation.columnIndex());// 获取属性上注解标记的单元格的类型Class valueType annotation.type();// 根据当前单元格的类型获取单元格的值if (Date.class valueType) {cellValue dateformat.format(cell.getDateCellValue());} else if (String.class valueType) {cellValue formatter.formatCellValue(cell);}// 通过反射将单元格的值动态封装到实体类中String methodName methodAction StringUtils.capitalize(field.getName());Method setMethod ReflectionUtils.findMethod(excelEntity.getClass(), methodName, cellValue.getClass());ReflectionUtils.invokeMethod(setMethod, excelEntity, cellValue);}}// 对Excel中的数据进行校验public void validateExcelData(ExcelEntity excelEntity) {// 使用自定义注解对excel数据进行校验并打印SetConstraintViolationExcelEntity validateResults validator.validate(excelEntity);for (ConstraintViolationExcelEntity validateResult : validateResults) {System.out.println(validateResult.getMessage());}// 打印excel中获取到的数据System.out.println(excelEntity);} }六. 效果
http://www.w-s-a.com/news/526067/

相关文章:

  • 内容营销价值wordpress博客优化插件
  • 最优惠的郑州网站建设淘宝网商城
  • 做封面网站企业网站优化服务商
  • 电子商务网站设计是什么蚌埠铁路建设监理公司网站
  • .name后缀的网站做房产网站多少钱
  • 手机上传网站源码网站app封装怎么做
  • 做的网站放在阿里云网站建设投标书范本
  • 做文化传播公司网站wordpress仿简书
  • 什么网站有题目做西宁网站制作哪里好
  • 网站上添加图片的原则优易主机 wordpress
  • 用php做的网站源代码那里有做像美团的网站的
  • 网站建设百科有什么做兼职的网站
  • 创造网站电商网站建设方案道客巴巴
  • 南通设计网站建设wordpress时光轴
  • 郑州做网站企起网站建设 风险
  • 北京市保障性住房建设投资中心网站6大连广告设计与制作公司
  • 建站之星网站模板国内f型网页布局的网站
  • 怎么做网站关键词优化外贸网站 开源
  • 广东公司响应式网站建设设计seo系统是什么
  • 清丰网站建设费用网站建设的前途
  • 网站上那些兼职网页怎么做的北京网页
  • 桂林建站平台哪家好品牌设计公司宣传文案
  • 平面设计和建设网站的区别公司官网静态
  • h5网站建设+案例住房住房和城乡建设部网站
  • 建设股公司网站东莞建设网网上平台
  • 湖州吴兴建设局网站加强网站建设的
  • 茌平做网站公司专业商城网站建设报价
  • 网站结构图怎么画wordpress注册不发送件
  • 个人备案网站可以做论坛吗电商推广方式有哪些
  • 网站建设 自适应国内最近的新闻