SSM框架上传文件和普通参数一起 提交 该怎么 *** 作啊 有没有能给一个简单的demo 就给个controller层和jsp层

SSM框架上传文件和普通参数一起 提交 该怎么 *** 作啊 有没有能给一个简单的demo 就给个controller层和jsp层,第1张

你看要ajax上传还是表单提交上传,itjob为你解答。
1、如果是表单提交上传,在controller层中定义多个参数,或者定义一个对象,对象中有参数字段和文件字段
2、如果是ajax上传,你就要看你采用哪个ajax js插件,如果用jquery的ajaxfrom的ajax上传插件,就根据里面给你定义的传参方式即可。

后台工具:eclipse,myeclipse,springboot, 语言:java
前台工具:hbuilder, 语言:html5,jquery,css3
数据库:mysql, 语言:sql
服务器:Tomcat,
可以先用在前台做出页面出来,弄些伪数据,做出个效果,然后根据页面所需功能设计后台(先搭好ssm后台框架),根据需要后台可以关联数据库,做完在服务器上一运行就ok了

1、新建Maven项目

File—New—project—Maven Projec

也可以点browse…自定义路径

WEB项目选择webapp

输入包名以及项目名称

错误解决:

这个错误是因为没有设置tomcat的原因

解决办法:

右键项目–Properties–Java Build Path
切换到Libraries—Add Libraries


选择–MyEclipse Server Library 下一步 Tomcat v70
也可以选择自己安装的Tomact

2、搭建Spring+SpringMVC+Mybatis框架

21 Maven引入需要的JAR包
pomxml

<!-- 用来设置版本号 -->
<properties>
<srpingversion>402RELEASE</srpingversion>
<mybatisversion>328</mybatisversion>
<slf4jversion>1712</slf4jversion>
<log4jversion>1217</log4jversion>
</properties>
<!-- 用到的jar包 -->
<dependencies>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>411</version>
<!-- 表示开发的时候引入,发布的时候不会加载此包 -->
<scope>test</scope>
</dependency>
<!-- java ee包 -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>70</version>
</dependency>
<!-- spring框架包 start -->
<dependency>
<groupId>orgspringframework</groupId>
<artifactId>spring-test</artifactId>
<version>${srpingversion}</version>
</dependency>
<dependency>
<groupId>orgspringframework</groupId>
<artifactId>spring-core</artifactId>
<version>${srpingversion}</version>
</dependency>
<dependency>
<groupId>orgspringframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${srpingversion}</version>
</dependency>
<dependency>
<groupId>orgspringframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${srpingversion}</version>
</dependency>
<dependency>
<groupId>orgspringframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${srpingversion}</version>
</dependency>
<dependency>
<groupId>orgspringframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${srpingversion}</version>
</dependency>
<dependency>
<groupId>orgspringframework</groupId>
<artifactId>spring-context</artifactId>
<version>${srpingversion}</version>
</dependency>
<dependency>
<groupId>orgspringframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${srpingversion}</version>
</dependency>
<dependency>
<groupId>orgspringframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${srpingversion}</version>
</dependency>
<dependency>
<groupId>orgspringframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${srpingversion}</version>
</dependency>
<dependency>
<groupId>orgspringframework</groupId>
<artifactId>spring-web</artifactId>
<version>${srpingversion}</version>
</dependency>
<dependency>
<groupId>orgspringframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${srpingversion}</version>
</dependency>
<!-- spring框架包   end -->
<!-- mybatis框架包 start -->
<dependency>
<groupId>orgmybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatisversion}</version>
</dependency>
<dependency>
<groupId>orgmybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>122</version>
</dependency>
<!-- mybatis框架包 end -->
<!-- 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5135</version>
</dependency>
<!-- 导入dbcp的jar包,用来在applicationContextxml中配置数据库 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>14</version>
</dependency>
<!-- jstl标签类 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>12</version>
</dependency>
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4jversion}</version>
</dependency>
<dependency>
<groupId>orgslf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4jversion}</version>
</dependency>
<dependency>
<groupId>orgslf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4jversion}</version>
</dependency>
<!-- log END -->
<!-- Json  -->
<!-- 格式化对象,方便输出日志 -->
<dependency>
<groupId>comalibaba</groupId>
<artifactId>fastjson</artifactId>
<version>126</version>
</dependency>
<dependency>
<groupId>orgcodehausjackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1913</version>
</dependency>
<!-- 上传组件包 start -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>131</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>24</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>110</version>
</dependency>
<!-- 上传组件包 end -->
</dependencies>
<build>
<finalName>newnie</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>/xml</include>
</includes>
</resource>
</resources>
</build>

22  在src/main/resources下建立如下配置文件
jdbcproperties (文件编码修改为 utf-8 )

本文章向大家介绍最简单使用easypoi导入导出Excel的 *** 作手册(回炉),主要内容包括easypoi 简介、特性、常用注解、@ExcelCollection 注解、@ExcelEntity注解、@ExcelIgnore 注解、@ExcelTarget 注解、1引入依赖、注解方式导出Excel、导出对应的Bean、导出结果、关于日期格式化的说明、ImportParams参数介绍、导入情形一:有标题有表头、导入情形二:有表头没有标题、导入实体Bean配置、Excel导入校验、定制化修改、总结、参考代码、参考、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。
概况
今天做Excel导出时,发现了一款非常好用的POI框架EasyPoi,其 使用起来简洁明了。现在我们就来介绍下EasyPoi,首先感谢EasyPoi 的开发者 Lemur开源
easypoi 简介
easypoi 是为了让开发者快速的实现excel,word,pdf的导入导出,基于Apache poi基础上的一个工具包。
特性
基于注解的导入导出,修改注解就可以修改Excel
支持常用的样式自定义
基于map可以灵活定义的表头字段
支持一对多的导出,导入
支持模板的导出,一些常见的标签,自定义标签
支持HTML/Excel转换
支持word的导出,支持,Excel
常用注解
@Excel注解
@Excel 注解是作用到Filed 上面,是对Excel一列的一个描述,这个注解是必须要的注解,其部分属性如下:
在这里插入描述 其使用如下,其中orderNum是指定该字段在Excel中的位置,name与Excel中对应的表头单元格的名称
@Excel(name = "主讲老师", orderNum = "1")
private String name;
@ExcelCollection 注解
@ExcelCollection 注解表示一个集合,主要针对一对多的导出 比如一个老师对应多个科目,科目就可以用集合表示,作用在一个类型是List的属性上面,属性如下:
在这里插入描述 其使用如下所示。
@ExcelCollection(name = "学生", orderNum = "4")
private List<StudentEntity> students;
@ExcelEntity注解
@ExcelEntity注解表示一个继续深入导出的实体,是作用一个类型为实体的属性上面,其属性如下:
在这里插入描述 其使用如下所示。
@ExcelEntity(id = "major")
private TeacherEntity chineseTeacher;
@ExcelIgnore 注解
@ExcelIgnore 注解修饰的字段,表示在导出的时候,被忽略。
@ExcelTarget 注解
@ExcelTarget注解作用于最外层的对象,描述这个对象的id,以便支持一个对象,可以针对不同导出做出不同处理,其作用在实体类的上,属性如下:
在这里插入描述 其使用如下:
@ExcelTarget("scoreIssueReqPOJO")
public class ScoreIssueReqPOJO implements javaioSerializable{}
# EasyPOI的使用
1引入依赖
SSM 项目,引入依赖 如果spring的版本是4x的话引入的easypoi的版本是`301`,如果spring是5x的话引入easypoi的版本是`400`
<dependency>
<groupId>cnafterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>400</version>
</dependency>
<dependency>
<groupId>cnafterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>400</version>
</dependency>
<dependency>
<groupId>cnafterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>400</version>
</dependency>
Spring Boot 项目(2x以上的版本,我demo的版本是213RELEASE),引入依赖
<dependency>
<groupId>cnafterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>400</version>
</dependency>
需要注意的是由于easypoi的依赖内部依赖原生的poi,所以,引入了easypoi的依赖之后,需要把原生的poi的依赖删掉
注解方式导出Excel
导出测试的demo
@Test
public void testExportExcel() throws Exception {
List<CourseEntity> courseEntityList = new ArrayList<>();
CourseEntity courseEntity = new CourseEntity();
courseEntitysetId("1");
courseEntitysetName("测试课程");
TeacherEntity teacherEntity = new TeacherEntity();
teacherEntitysetName("张老师");
teacherEntitysetSex(1);
courseEntitysetMathTeacher(teacherEntity);
List<StudentEntity> studentEntities = new ArrayList<>();
for (int i = 1; i <= 2; i++) {
StudentEntity studentEntity = new StudentEntity();
studentEntitysetName("学生" + i);
studentEntitysetSex(i);
studentEntitysetBirthday(new Date());
studentEntitiesadd(studentEntity);
}
courseEntitysetStudents(studentEntities);
courseEntityListadd(courseEntity);
Date start = new Date();
Workbook workbook = ExcelExportUtilexportExcel( new ExportParams("导出测试", null, "测试"),
CourseEntityclass, courseEntityList);
Systemoutprintln(new Date()getTime() - startgetTime());
File savefile = new File("D:/excel/");
if (!savefileexists()) {
savefilemkdirs();
}
FileOutputStream fos = new FileOutputStream("D:/excel/教师课程学生导出测试xls");
workbookwrite(fos);
fosclose();
}
导出对应的Bean
CourseEntity 类。
@ExcelTarget("courseEntity")
public class CourseEntity implements javaioSerializable {
/ 主键 /
private String id;
/ 课程名称 /
@Excel(name = "课程名称", orderNum = "1", width = 25,needMerge = true)
private String name;
/ 老师/
@ExcelEntity(id = "absent")
private TeacherEntity mathTeacher;
@ExcelCollection(name = "学生", orderNum = "4")
private List<StudentEntity> students;
TeacherEntity 类
@Data
public class TeacherEntity {
/
学生姓名
/
@Excel(name = "教师姓名", height = 20, width = 30, isImportField = "true_st")
private String name;
/
学生性别
/
@Excel(name = "教师性别", replace = {"男_1", "女_2"}, suffix = "生", isImportField = "true_st")
private int sex;
}
StudentEntity 类。
public class StudentEntity implements javaioSerializable {
/
id
/
private String id;
/
学生姓名
/
@Excel(name = "学生姓名", height = 20, width = 30, isImportField = "true_st")
private String name;
/
学生性别
/
@Excel(name = "学生性别", replace = { "男_1", "女_2" }, suffix = "生", isImportField = "true_st")
private int sex;
@Excel(name = "出生日期", exportFormat = "yyyyMMddHHmmss", format = "yyyy-MM-dd", isImportField = "true_st", width = 20)
private Date birthday;
@Excel(name = "进校日期", exportFormat = "yyyyMMddHHmmss", format = "yyyy-MM-dd")
private Date registrationDate;
导出结果
在这里插入描述
关于日期格式化的说明
如果是导出的实体类(就是说这个实体类是对应导出的Excel的),那么用@Excel注解的exportFormat属性来格式化日期。如下所示:
@Excel(name = "出生日期", exportFormat = "yyyy-MM-dd HH:mm:ss", width = 20)
如果是导入的实体类(就是说这个实体类是对应导入的Excel的),那么用@Excel注解的importFormat属性来格式化日期。如下所示:
@Excel(name = "添加时间",importFormat = "yyyy-MM-dd HH:mm:ss",orderNum = "14")
private Date createTime;
@Excel注解的databaseFormat 属性是用于数据库的格式不是日期类型,如datetime类型时使用。 注解方式导入Excel 基于注解的导入导出,配置配置上是一样的,只是方式反过来而已。首先让我们来看看 ImportParams类,这个类主要是设置导入参数,例如表格行数,表头行数等等。
ImportParams参数介绍
在这里插入描述 需要说明的是 1、titleRows表示的是表格标题行数,如果没有就是0,如果有一个标题就是1,如果是两个标题就2 2 headRows表示的是表头行数,默认是1,如果有两个表头则需要设置2。
导入情形一:有标题有表头
我们有如下格式的Excel需要导入:
在这里插入描述 这个Excel有一个标题行,有一个表头行,所以我们有了如下设置:
ImportParams params = new ImportParams();
//设置标题的行数,有标题时一定要有
paramssetTitleRows(1);
//设置表头的行数
paramssetHeadRows(1);
导入的demo
@Test
public void haveTitleTest() {
ImportParams params = new ImportParams();
//设置标题的行数,有标题时一定要有
paramssetTitleRows(1);
//设置表头的行数
paramssetHeadRows(1);
String file = ThreadcurrentThread()getContextClassLoader()getResource("haveTitlexlsx")getFile();
List<ScoreIssueReqPOJO> list = ExcelImportUtilimportExcel(
new File(file),
ScoreIssueReqPOJOclass, params);
Systemoutprintln("解析到的数据长度是:" + listsize());
for (ScoreIssueReqPOJO scoreIssueReqPOJO : list) {
Systemoutprintln("有标题有表头导入的数据是=" + scoreIssueReqPOJOtoString());
}
}
导入测试的结果是:
在这里插入描述

首先了解一下这两个是什么东西

SSM(Spring+SpringMVC+MyBatis)框架集由Spring、MyBatis两个开源框架整合而成(SpringMVC是Spring中的部分内容),常作为数据源较简单的web项目的框架。

Maven是项目管理工具, 提供方便的包依赖和打包工具。

举例: 一个项目是SSM搭建的WEB框架,它使用Maven进行依赖管理。

区别SSM

Spring:是否使用Spring配置或者注解对类进行注入

SpringMVC:是否在项目中拦截用户请求,根据Mapping去匹配Controller。 (struts是使用xml文件映射Action和请求)

mybatis: 是否引入mybatis依赖, 并且配置文件关联到各实体类的Mapper, 以及进行 *** 作的sql语句映射

区别Maven

项目中是否包含 pomxml 文件, 并且以改文件中声明依赖进行搭建


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/13323508.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-14
下一篇 2023-07-14

发表评论

登录后才能评论

评论列表(0条)

保存