码迷,mamicode.com
首页 > 其他好文 > 详细

struts2+mybatis

时间:2019-05-24 14:16:52      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:utils   and   文件夹   content   简单的   nfa   Servle   load()   eal   

简单的整合一下struts2+mybatis,此文主要写给自己看

public class SessionUtil {
	//获得session工厂
	private static SqlSessionFactory factory = null;
	//定义一个ThreadLocal存放session
	private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<>();
	static{
		try {
			//根据配置文件创建工厂
			InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
			factory = new SqlSessionFactoryBuilder().build(inputStream);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//编写方法,获取session,通过判断,一定得到线程中的session
	public static SqlSession getSession() {
		//1.从当前线程中获取session
		SqlSession session = threadLocal.get();
		if (session==null) {
			session = factory.openSession();
			threadLocal.set(session);//绑定到当前线程上
		}
		//2.返回session
		return session;
	}
	//从当前线程移除session
	public static void close() {
		SqlSession session = threadLocal.get();
		if (session!=null) {
			threadLocal.remove();
			session.close();
		}
	}
}

  配置struts中的拦截器,其作用类似于servlet的过滤器,

<!-- 配置拦截器 -->
        <interceptors>
            <!-- 1.配置自己的拦截器 -->
            <interceptor name="timeInterceptor" class="com.woniuxy.interceptor.CustomInterceptor"></interceptor>
            
            <!-- 2.配置自己的拦截器栈 -->
            <interceptor-stack name="myStack">
                <!-- 2.1引用struts的默认拦截器栈 -->
                <interceptor-ref name="defaultStack"></interceptor-ref>
                <!-- 2.2引用自己的拦截器 -->
                <interceptor-ref name="timeInterceptor"></interceptor-ref>
                
            </interceptor-stack>
        </interceptors>

上传文件

<form action="file_upload" enctype="multipart/form-data" method="post">
		<input type="file" name="abc"><br>
		<input type="submit" value="上传">
</form>

  

public class FileUploadAction {
	private File abc;				//保存文件
	private String abcContentType;	//文件类型
	private String abcFileName;		//文件名
	
	private Account account;
	
	private AccountDao accountService = new AccountService();
	
	public File getAbc() {
		return abc;
	}
	public void setAbc(File abc) {
		this.abc = abc;
	}
	public String getAbcContentType() {
		return abcContentType;
	}
	public void setAbcContentType(String abcContentType) {
		this.abcContentType = abcContentType;
	}
	public String getAbcFileName() {
		return abcFileName;
	}
	public void setAbcFileName(String abcFileName) {
		this.abcFileName = abcFileName;
	}
	public Account getAccount() {
		return account;
	}
	public void setAccount(Account account) {
		this.account = account;
	}
	//处理请求的方法
	public String upload() {
		System.out.println(abc);
		System.out.println(abcContentType);
		System.out.println(abcFileName);
		//1.将文件存放到服务器指定的文件夹
		//1.1.获取文件夹的路径
		String path = ServletActionContext.getServletContext().getRealPath("");
		File root = new File(path);
		File webapps = root.getParentFile();
		//1.2.获取images的File
		File images = new File(webapps, "images");
		if (!images.exists()) {
			images.mkdirs();
		}
		//1.3.新文件
		String fileName = UUID.randomUUID().toString()+abcFileName.substring(abcFileName.lastIndexOf("."));
		
		File newFile = new File(images, fileName);
		
		//1.4.存放文件
		try {
			FileUtils.copyFile(abc, newFile);
			//将文件路径存放到数据库中
			String newPath = "/images/"+fileName;
			//调用service向对应的表、记录中更新数据数据
			account.setHeadImg(newPath);
			
			accountService.addAccount(account);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
		System.out.println(path);
		return "success";
	}
}

  

<!-- 文件上传 -->
        <action name="file_*" class="com.woniuxy.action.FileUploadAction" method="{1}">
            <result name="success">html/success.html</result>
        </action>

 

struts2+mybatis

标签:utils   and   文件夹   content   简单的   nfa   Servle   load()   eal   

原文地址:https://www.cnblogs.com/harrypotterformhogwarts/p/10917762.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!