1、利用代码生成功能制作好单表增删改查的模块
2、修改前段代码,插入‘导入’按钮并且写好导入弹出框界面,加入复制粘贴用户模块的前段代码
3、修改实体类,加上注释
4、在控制器加入下载模板和导入功能的响应方法
第一步:根据前面两篇博文去操作,建立左树右表的单表增删改查,在原有的根据字段查询数据的查询按钮右边,加入导入按钮
第二步:修改前段代码,如下:
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
<title>课程表管理title>
<meta name="decorator" content="default" />
<script type="text/javascript">
$(document).ready(function() {
$("#btnImport").click(function(){
$.jBox($("#importBox").html(), {title:"导入数据", buttons:{"关闭":true},
bottomText:"导入文件不能超过5M,仅允许导入“xls”或“xlsx”格式文件!"});
});
});
function page(n, s) {
$("#pageNo").val(n);
$("#pageSize").val(s);
$("#searchForm").submit();
return false;
}
script>
head>
<body>
<div id="importBox" class="hide">
<form id="importForm" action="${ctx}/jsl/jslCourse/import"
method="post" enctype="multipart/form-data" class="form-search"
style="padding-left: 20px; text-align: center;"
onsubmit="loading('正在导入,请稍等...');">
<br />
<input id="uploadFile" name="file" type="file"
style="width: 330px" />
<br /> <br />
<input id="btnImportSubmit"
class="btn btn-primary" type="submit" value=" 导 入 " /> <a
href="${ctx}/jsl/jslCourse/import/template">下载模板a>
form>
div>
<ul class="nav nav-tabs">
<li class="active"><a href="${ctx}/jsl/jslCourse/">课程表列表a>li>
<shiro:hasPermission name="jsl:jslCourse:edit">
<li><a href="${ctx}/jsl/jslCourse/form">课程表添加a>li>
shiro:hasPermission>
ul>
<form:form id="searchForm" modelAttribute="jslCourse"
action="${ctx}/jsl/jslCourse/" method="post"
class="breadcrumb form-search">
<input id="pageNo" name="pageNo" type="hidden" value="${page.pageNo}" />
<input id="pageSize" name="pageSize" type="hidden"
value="${page.pageSize}" />
<ul class="ul-form">
<li><label>上课教室:label> <form:input path="classroom"
htmlEscape="false" maxlength="64" class="input-medium" />li>
<li><label>任课老师:label> <form:input path="courseteacher"
htmlEscape="false" maxlength="64" class="input-medium" />li>
<li class="btns"><input id="btnSubmit" class="btn btn-primary"
type="submit" value="查询" />li>
<li class="btns"><input id="btnImport" class="btn btn-primary"
type="button" value="导入" />li>
<li class="clearfix">li>
ul>
form:form>
<sys:message content="${message}" />
<table id="contentTable"
class="table table-striped table-bordered table-condensed">
<thead>
<tr>
<th>归属公司th>
<th>归属部门th>
<th>课程名称th>
<th>上课教室th>
<th>听课老师th>
<th>任课老师th>
<th>专业班级th>
<th>听课时间th>
<th>更新时间th>
<th>备注信息th>
<shiro:hasPermission name="jsl:jslCourse:edit">
<th>操作th>
shiro:hasPermission>
tr>
thead>
<tbody>
<c:forEach items="${page.list}" var="jslCourse">
<tr>
<td><a href="${ctx}/jsl/jslCourse/form?id=${jslCourse.id}">
${jslCourse.company.name} a>td>
<td>${jslCourse.office.name}td>
<td>${jslCourse.coursename}td>
<td>${jslCourse.classroom}td>
<td>${jslCourse.listenteacher}td>
<td>${jslCourse.courseteacher}td>
<td>${jslCourse.professionalclass}td>
<td><fmt:formatDate value="${jslCourse.coursetime}"
pattern="yyyy-MM-dd HH:mm:ss" />td>
<td><fmt:formatDate value="${jslCourse.updateDate}"
pattern="yyyy-MM-dd HH:mm:ss" />td>
<td>${jslCourse.remarks}td>
<shiro:hasPermission name="jsl:jslCourse:edit">
<td><a href="${ctx}/jsl/jslCourse/form?id=${jslCourse.id}">修改a>
<a href="${ctx}/jsl/jslCourse/delete?id=${jslCourse.id}"
onclick="return confirmx('确认要删除该课程表吗?', this.href)">删除a>td>
shiro:hasPermission>
tr>
c:forEach>
tbody>
table>
<div class="pagination">${page}div>
body>
html>
第三步:实体类中凡是要通过excel导入的字段都需要在实体类的字段的get()方法添加注释
/**
* Copyright © 2012-2016 JeeSite All rights reserved.
*/
package com.thinkgem.jeesite.modules.jsl.entity;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.Length;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.thinkgem.jeesite.common.persistence.DataEntity;
import com.thinkgem.jeesite.common.utils.excel.annotation.ExcelField;
/**
* 课程表管理Entity
* @author jsl
* @version 2017-10-21
*/
public class JslCourse extends DataEntity {
private static final long serialVersionUID = 1L;
private Office company; // 归属公司
private Office office; // 归属部门
private String coursename; // 课程名称
private String classroom; // 上课教室
private String listenteacher; //听课老师
private String courseteacher; // 任课老师
private String professionalclass; // 专业班级
private Date coursetime; // 听课时间
public JslCourse() {
super();
}
public JslCourse(String id){
super(id);
}
@JsonIgnore
@NotNull(message="归属公司不能为空")
@ExcelField(title="归属公司", align=2, sort=20)
public Office getCompany() {
return company;
}
public void setCompany(Office company) {
this.company = company;
}
@JsonIgnore
@NotNull(message="归属部门不能为空")
@ExcelField(title="归属部门", align=2, sort=25)
public Office getOffice() {
return office;
}
public void setOffice(Office office) {
this.office = office;
}
@Length(min=0, max=64, message="课程名长度必须介于 1 和 64之间")
@ExcelField(title="课程名", align=2, sort=30)
public String getCoursename() {
return coursename;
}
public void setCoursename(String coursename) {
this.coursename = coursename;
}
@ExcelField(title="课室", align=2, sort=35)
@Length(min=0, max=64, message="上课教室长度必须介于 0 和 64 之间")
public String getClassroom() {
return classroom;
}
public void setClassroom(String classroom) {
this.classroom = classroom;
}
@ExcelField(title="听课老师", align=2, sort=40)
@Length(min=0, max=64, message="听课老师长度必须介于 0 和 64 之间")
public String getListenteacher() {
return listenteacher;
}
public void setListenteacher(String listenteacher) {
this.listenteacher = listenteacher;
}
@ExcelField(title="任课老师", align=2, sort=45)
@Length(min=0, max=64, message="任课老师长度必须介于 0 和 64 之间")
public String getCourseteacher() {
return courseteacher;
}
public void setCourseteacher(String courseteacher) {
this.courseteacher = courseteacher;
}
@ExcelField(title="专业班级", align=2, sort=50)
@Length(min=0, max=64, message="专业班级长度必须介于 0 和 64 之间")
public String getProfessionalclass() {
return professionalclass;
}
public void setProfessionalclass(String professionalclass) {
this.professionalclass = professionalclass;
}
@ExcelField(title="听课时间", align=2, sort=55)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
public Date getCoursetime() {
return coursetime;
}
public void setCoursetime(Date coursetime) {
this.coursetime = coursetime;
}
}
第四步:在控制层添加响应导入按钮的操作,主要有两个部分,一个是下载模板、一个是导入响应
/**
* Copyright © 2012-2016 JeeSite All rights reserved.
*/
package com.thinkgem.jeesite.modules.jsl.web;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.google.common.collect.Lists;
import com.thinkgem.jeesite.common.beanvalidator.BeanValidators;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.utils.excel.ExportExcel;
import com.thinkgem.jeesite.common.utils.excel.ImportExcel;
import com.thinkgem.jeesite.modules.jsl.entity.JslCourse;
import com.thinkgem.jeesite.modules.jsl.service.JslCourseService;
/**
* 课程表管理Controller
* @author jsl
* @version 2017-10-21
*/
@Controller
@RequestMapping(value = "${adminPath}/jsl/jslCourse")
public class JslCourseController extends BaseController {
@Autowired
private JslCourseService jslCourseService;
@ModelAttribute
public JslCourse get(@RequestParam(required=false) String id) {
JslCourse entity = null;
if (StringUtils.isNotBlank(id)){
entity = jslCourseService.get(id);
}
if (entity == null){
entity = new JslCourse();
}
return entity;
}
@RequiresPermissions("jsl:jslCourse:view")
@RequestMapping(value = {"index"})
public String index(JslCourse jslCourse, Model model) {
return "modules/jsl/jslCourseIndex";
}
@RequiresPermissions("jsl:jslCourse:edit")
@RequestMapping(value = "import", method=RequestMethod.POST)
public String importFile(MultipartFile file, RedirectAttributes redirectAttributes){
try {
int successNum=0;
int failureNum=0;
StringBuilder failureMsg=new StringBuilder();
ImportExcel ei=new ImportExcel(file, 1, 0);
List list=ei.getDataList(JslCourse.class);
for(JslCourse jslCourse:list){
try {
jslCourseService.save(jslCourse);
successNum++;
addMessage(redirectAttributes, "已成功导入 "+successNum+" 条用户"+failureMsg);
} catch (ConstraintViolationException ex) {
failureMsg.append("
课程名 "+jslCourse.getCoursename()+" 导入失败:");
List messageList = BeanValidators.extractPropertyAndMessageAsList(ex, ": ");
for (String message : messageList){
failureMsg.append(message+"; ");
failureNum++;
}
}catch (Exception ex) {
failureMsg.append("
课程名 "+jslCourse.getCoursename()+" 导入失败:"+ex.getMessage());
}
}
if (failureNum>0){
failureMsg.insert(0, ",失败 "+failureNum+" 条用户,导入信息如下:");
}
addMessage(redirectAttributes, "已成功导入 "+successNum+" 条课程"+failureMsg);
} catch (Exception e) {
addMessage(redirectAttributes, "导入课程表失败!失败信息:"+e.getMessage());
}
return "redirect:" + adminPath + "/jsl/jslCourse?repage";
}
/**
* 下载导入课程表数据模板
* @param response
* @param redirectAttributes
* @return
*/
@RequiresPermissions("jsl:jslCourse:view")
@RequestMapping(value = "import/template")
public String importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
try {
String fileName = "课程表数据导入模板.xlsx";
List list = Lists.newArrayList(); list.add(new JslCourse());
new ExportExcel("课程表数据", JslCourse.class, 2).setDataList(list).write(response, fileName).dispose();
return null;
} catch (Exception e) {
addMessage(redirectAttributes, "导入模板下载失败!失败信息:"+e.getMessage());
}
return "redirect:" + adminPath + "/jsl/jslCourse?repage";
}
@RequiresPermissions("jsl:jslCourse:view")
@RequestMapping(value = {"list", ""})
public String list(JslCourse jslCourse, HttpServletRequest request, HttpServletResponse response, Model model) {
Page page = jslCourseService.findPage(new Page(request, response), jslCourse);
model.addAttribute("page", page);
return "modules/jsl/jslCourseList";
}
@RequiresPermissions("jsl:jslCourse:view")
@RequestMapping(value = "form")
public String form(JslCourse jslCourse, Model model) {
model.addAttribute("jslCourse", jslCourse);
return "modules/jsl/jslCourseForm";
}
@RequiresPermissions("jsl:jslCourse:edit")
@RequestMapping(value = "save")
public String save(JslCourse jslCourse, Model model, RedirectAttributes redirectAttributes) {
if (!beanValidator(model, jslCourse)){
return form(jslCourse, model);
}
jslCourseService.save(jslCourse);
addMessage(redirectAttributes, "保存课程表成功");
return "redirect:"+Global.getAdminPath()+"/jsl/jslCourse/?repage";
}
@RequiresPermissions("jsl:jslCourse:edit")
@RequestMapping(value = "delete")
public String delete(JslCourse jslCourse, RedirectAttributes redirectAttributes) {
jslCourseService.delete(jslCourse);
addMessage(redirectAttributes, "删除课程表成功");
return "redirect:"+Global.getAdminPath()+"/jsl/jslCourse/?repage";
}
}