使用SpingMVC和hibernate框架实现
1. web.xml中的配置文件
web.xml中的配置文件就按照这种方式写,只需要把"application.xml"换成你的配置文件名即可
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:application.xml
2. application.xml的配置文件(固定写法)
在这个配置文件中你还可以规定上传文件的格式以及大小等多种属性限制
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
3. 文件上传的前端HTML
注意:
1.enctype=“multipart/form-data” 必须写,封装表单
2.method=“post”,提交方式必须为"post"提交
3.action=“${text}/uploadfile”
支持的excel格式为:xls、xlsx、xlsb、xlsm、xlst!
4. 验证上传文件的格式
//用于验证文件扩展名的正则表达式
function checkSuffix(){
var name = document.getElementById("txt").value;
var strRegex = "(.xls|.xlsx|.xlsb|.xlsm|.xlst)$";
var re=new RegExp(strRegex);
if (re.test(name.toLowerCase())){
alert("上传成功");
document.fileupload.submit();
} else{
alert("文件名不合法");
}
}
5. dao层的接口和实现类
package com.gxxy.team1.yyd.dao;
public interface IFileUploadDao {
public void save(Object o);
}
package com.gxxy.team1.yyd.dao.impl;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.gxxy.team1.yyd.dao.IFileUploadDao;
@Repository
public class FileUploadDaoImpl implements IFileUploadDao {
@Autowired
private SessionFactory sessionFactory;
private Session getSession() {
Session session = sessionFactory.getCurrentSession();
return session;
}
@Override
public void save(Object o) {
getSession().save(o);
}
}
6. service层的接口和实现类
package com.gxxy.team1.yyd.service;
import java.util.List;
public interface IFileUploadService {
public List

本文详细介绍了如何使用SpringMVC和Hibernate框架进行Web应用开发,包括web.xml配置、multipartfileupload、前端HTML表单、DAO和Service接口实现,以及读取和处理Excel数据的过程。
最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



