吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2567|回复: 6
收起左侧

[会员申请] 申请会员ID:诗仙不再

[复制链接]
吾爱游客  发表于 2020-8-21 11:23
1、申 请 I D:诗仙不再
2、个人邮箱:429638138@qq.com
3、原创技术文章:
如何在springboot框架中使用jsp文件以使用session?编辑 [url=]版权[/url]开发过程中,后端代码编写完毕后,需要与前端进行连接,连接时,由于要使用session中的值,我们需要使用jsp来完成session中的数据获取,jsp中获取session值的代码:<%=session.getAttribute(“test”)%>
我们需要在与java、resources同级目录下建一个webapp/WEB-INF/jsp文件夹来存放jsp代码,需要配置pom.xml<dependency>           <groupId>org.apache.tomcat.embed</groupId>           <artifactId>tomcat-embed-jasper</artifactId>           <scope>provided</scope>       </dependency>                <dependency>           <groupId>org.springframework.boot</groupId>           <artifactId>spring-boot-starter-tomcat</artifactId>           <scope>provided</scope>       </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
然后还需要配置application.propertiesspring.mvc.view.prefix=/WEB-INF/jsp/spring.mvc.view.suffix=.jsp
  • 1
  • 2
配置完后在controller中直接return “info”;就会返回jsp文件夹中的info.jsp页面

SpringBoot中如何进行Excel的导入和导出
首先,需要配置pox.xml文件中的依赖:        <dependency>                <groupId>commons-fileupload</groupId>                <artifactId>commons-fileupload</artifactId>                <version>1.3.1</version>        </dependency>                <!-- 集成lombok 框架 -->        <dependency>                <groupId>org.projectlombok</groupId>                <artifactId>lombok</artifactId>        </dependency>                <!--阿里巴巴 fastjson -->        <dependency>                <groupId>com.alibaba</groupId>                <artifactId>fastjson</artifactId>                <version>1.2.30</version>        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
        <dependency>        <groupId>cn.afterturn</groupId>        <artifactId>easypoi-base</artifactId>        <version>3.0.3</version>    </dependency>    <dependency>        <groupId>cn.afterturn</groupId>        <artifactId>easypoi-web</artifactId>        <version>3.0.3</version> </dependency>    <dependency>        <groupId>cn.afterturn</groupId>        <artifactId>easypoi-annotation</artifactId>        <version>3.0.3</version>    </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
然后,导入ExcelUtiles.java工具类(自建):package com.hfut.utiles;import cn.afterturn.easypoi.excel.ExcelExportUtil;import cn.afterturn.easypoi.excel.ExcelImportUtil;import cn.afterturn.easypoi.excel.entity.ExportParams;import cn.afterturn.easypoi.excel.entity.ImportParams;import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;import org.apache.commons.lang3.StringUtils;import org.apache.poi.ss.usermodel.Workbook;import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse;import java.io.File;import java.io.IOException;import java.net.URLEncoder;import java.util.List;import java.util.Map;import java.util.NoSuchElementException;public class ExcelUtiles {        public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass,            String fileName, boolean isCreateHeader, HttpServletResponse response){                                        ExportParams exportParams = new ExportParams(title, sheetName);                                        exportParams.setCreateHeadRows(isCreateHeader);                                        defaultExport(list, pojoClass, fileName, response, exportParams);                                        }                                                                                public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName,                                                    HttpServletResponse response){                                        defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));                                        }                                                                                public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response){                                        defaultExport(list, fileName, response);                                        }                                                                                private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName,                                                       HttpServletResponse response, ExportParams exportParams) {                                        Workbook workbook = ExcelExportUtil.exportExcel(exportParams,pojoClass,list);                                        if (workbook != null); downLoadExcel(fileName, response, workbook);                                        }                                                                                private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {                                        try {                                        response.setCharacterEncoding("UTF-8");                                        response.setHeader("content-Type", "application/vnd.ms-excel");                                        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));                                        workbook.write(response.getOutputStream());                                        } catch (IOException e) {                                        //throw new NormalException(e.getMessage());                                        }                                        }                                                                                private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {                                        Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);                                        if (workbook != null);                                        downLoadExcel(fileName, response, workbook);                                        }                                                                                public static <T> List<T> importExcel(String filePath,Integer titleRows,Integer headerRows, Class<T> pojoClass){                                        if (StringUtils.isBlank(filePath)){                                        return null;                                        }                                        ImportParams params = new ImportParams();                                        params.setTitleRows(titleRows);                                        params.setHeadRows(headerRows);                                        List<T> list = null;                                        try {                                        list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);                                        }catch (NoSuchElementException e){                                        //throw new NormalException("模板不能为空");                                        } catch (Exception e) {                                        e.printStackTrace();                                        //throw new NormalException(e.getMessage());                                        } return list;                                        }                                                                                public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass){                                        if (file == null){ return null;                                        }                                        ImportParams params = new ImportParams();                                        params.setTitleRows(titleRows);                                        params.setHeadRows(headerRows);                                        List<T> list = null;                                        try {                                        list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);                                        }catch (NoSuchElementException e){                                        // throw new NormalException("excel文件不能为空");                                        } catch (Exception e) {                                        //throw new NormalException(e.getMessage());                                        System.out.println(e.getMessage());                                        }                                        return list;}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
之后,配置Entity中的类:package com.hfut.entity;import cn.afterturn.easypoi.excel.annotation.Excel;import lombok.Data;import org.springframework.format.annotation.DateTimeFormat;import javax.persistence.*;import java.util.Date;@SuppressWarnings("unused")@Entity@Table(name = "seckill")public class User {                @Id        @GeneratedValue(strategy = GenerationType.IDENTITY)        @Excel(name="编号",orderNum="0")        private int id;                @Column(name="name")        @Excel(name="姓名",orderNum="1")        private String name;                @Column(name="age")        @Excel(name="年龄",orderNum="2")        private int age;                @Column(name="hobby")        @Excel(name="爱好",orderNum="3")        private String hobby;                public User(){                        }        public User(int id, String name, int age, String hobby) {                super();                this.id = id;                this.name = name;                this.age = age;                this.hobby = hobby;        }        public int getId() {                return id;        }        public void setId(int id) {                this.id = id;        }        public String getName() {                return name;        }        public void setName(String name) {                this.name = name;        }        public int getAge() {                return age;        }        public void setAge(int age) {                this.age = age;        }        public String getHobby() {                return hobby;        }        public void setHobby(String hobby) {                this.hobby = hobby;        }                 @Override            public String toString() {                return "userExcel{" +                        "id='" + id + '\'' +                        ", name=" + name +                        ", age=" + age +                        ", hobby=" + hobby +                        '}';            }        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
配置完成后,就是编写controller中的导入导出函数:@GetMapping("/exportInfo")    public void export(HttpServletResponse response) {        System.out.println(1+"*/*/*/*/*/*/*/*/*/*/*/*");//        模拟从数据库获取需要导出的数据        List<User> list = (List<User>) userService.showAll();//         导出操作                ExcelUtiles.exportExcel(list, "测试名", "什么名字", User.class, "测试.xls", response);     }                @PostMapping("/importInfo")    public String importInfo(@RequestParam("file") MultipartFile file) {                System.out.println("开始导入人员信息");        ImportParams importParams = new ImportParams();        // 数据处理        importParams.setHeadRows(1);        importParams.setTitleRows(1);         // 需要验证        importParams.setNeedVerfiy(true);         try {            ExcelImportResult<User> result = ExcelImportUtil.importExcelMore(file.getInputStream(), User.class,                    importParams);             List<User> successList = result.getList();            for (User user : successList) {              System.out.println(user);              boolean result1 = userService.selectById(user);              if(result1) {                      userService.updateUserInfo(user);              }else {                      userService.insertUserInfo(user);              }              System.out.println(result1);            }        } catch (IOException e) {        } catch (Exception e) {        }        return "showIn";    }   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
然后就可以在jsp文件中调用这两个函数进行执行,导入Excel的函数中,首先会在数据库中查找该id是否存在,若存在则进行数据库的更新,若不存在则插入该id的user进入数据库。

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Hmily 发表于 2020-8-21 16:01
抱歉,未能达到申请要求,申请不通过,可以关注论坛官方微信(吾爱破解论坛),等待开放注册通知。
black70 发表于 2020-8-21 16:39
扎克伯格 发表于 2020-8-22 14:00
旋风中的小聪明 发表于 2020-8-23 11:55
加油吧小伙子,期待你有新的表现
ggogg928 发表于 2020-9-16 16:00
好久没见过被拒绝的了
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-19 21:35

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表