博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
注解式struts2配合ajax
阅读量:5146 次
发布时间:2019-06-13

本文共 9575 字,大约阅读时间需要 31 分钟。

struts代码

  
<把struts交给spring管理>

controll

package com.zy.control;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.util.HashMap;import java.util.Map;import java.util.UUID;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import org.apache.commons.io.IOUtils;import org.apache.struts2.ServletActionContext;import org.apache.struts2.convention.annotation.Action;import org.apache.struts2.convention.annotation.Namespace;import org.apache.struts2.convention.annotation.ParentPackage;import org.apache.struts2.convention.annotation.Result;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Controller;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;import com.opensymphony.xwork2.util.ValueStack;import com.zy.entity.User;import com.zy.service.UserService;import com.zy.util.ImageCut;@Controller("UserControl")@ParentPackage("json-default")@Namespace("/user")@Scope("prototype")@SuppressWarnings("serial")public class UserControl extends ActionSupport implements ModelDriven
{ private File image_file; private String image_fileFileName; public File getImage_file() { return image_file; } public void setImage_file(File image_file) { this.image_file = image_file; } public String getImage_fileFileName() { return image_fileFileName; } public void setImage_fileFileName(String image_fileFileName) { this.image_fileFileName = image_fileFileName; } private Map
data = new HashMap<>(); public Map
getData() { return data; } public void setData(Map
data) { this.data = data; } @Autowired private UserService us; private User user; // 退出 @Action(value = "exit", results = { @Result(name = "exit", location = "/index.jsp") }) public String exit() { ServletActionContext.getRequest().getSession().setAttribute("user", null); return "exit"; } // 登录验证 @Action(value = "login", results = { @Result(name = "fail", location = "/index.jsp"), @Result(name = "success", location = "/index.jsp") }) public String login() { User uu = us.selectUser(user); ValueStack stack = ActionContext.getContext().getValueStack(); if (uu == null) { stack.set("loginerror", "用户不存在"); return "fail"; } else if (!uu.getPassword().equals(user.getPassword())) { stack.set("loginerror", "密码错误"); return "fail"; } else { HttpSession session = ServletActionContext.getRequest().getSession(); session.setAttribute("user", uu); return SUCCESS; } } // 注册 邮箱是否存在Ajax验证 @Action(value = "available", results = { @Result(name = "ajax", type = "json", params = { "root", "data" }) }) public String available() { boolean a = us.checkUser(user); if (a) { data.put("info", "邮箱已存在"); } else { data.put("info", "邮箱可用"); } return "ajax"; } // 注册 @Action(value = "regist", results = { @Result(name = "regist", location = "/index.jsp") }) public String regist() { us.regist(user); return "regist"; } // 个人信息 @Action(value = "showMyProfile", results = { @Result(name = "showMyProfile", location = "/WEB-INF/before/my_profile.jsp") }) public String showMyProfile() { HttpSession session = ServletActionContext.getRequest().getSession(); User sessionuser = (User) session.getAttribute("user"); User uu = us.showMyProfile(sessionuser); ValueStack stack = ActionContext.getContext().getValueStack(); stack.set("user", uu); return "showMyProfile"; } // 去更改资料页面changeProfile @Action(value = "changeProfile", results = { @Result(name = "changeProfile", location = "/WEB-INF/before/change_profile.jsp") }) public String changeProfile() { HttpSession session = ServletActionContext.getRequest().getSession(); User sessionuser = (User) session.getAttribute("user"); User uu = us.showMyProfile(sessionuser); ValueStack stack = ActionContext.getContext().getValueStack(); stack.set("user", uu); return "changeProfile"; } // 更改资料 @Action(value = "updateUser", results = { @Result(name = "updateUser", type = "redirectAction", location = "showMyProfile") }) public String updateUser() { us.updateUser(user); return "updateUser"; } // 去更改头像页面changeAvatar @Action(value = "changeAvatar", results = { @Result(name = "changeAvatar", location = "/WEB-INF/before/change_avatar.jsp") }) public String changeAvatar() { HttpSession session = ServletActionContext.getRequest().getSession(); User sessionuser = (User) session.getAttribute("user"); User uu = us.showMyProfile(sessionuser); ValueStack stack = ActionContext.getContext().getValueStack(); stack.set("user", uu); return "changeAvatar"; } @Action(value = "upLoadImage", results = { @Result(name = "upLoadImage", type = "redirectAction", location = "showMyProfile") }) public String upLoadImage() throws Exception { HttpServletRequest request = ServletActionContext.getRequest(); if (image_file.exists()) {
// 根据文件尺寸判断有没有传文件 String x1 = request.getParameter("x1"); String x2 = request.getParameter("x2"); String y1 = request.getParameter("y1"); @SuppressWarnings("unused") String y2 = request.getParameter("y2"); int width = 0; // 宽度 if (!x1.equals("")) { width = (int) (Double.parseDouble(x2) - Double.parseDouble(x1)); } /* * //高度 int height=(Integer.parseInt(y2)-Integer.parseInt(y1)); */ // --------------------------------------------------------- // 图像上传本质就是用流的方式,将本地文件copy到服务器中 // 得到输入流 InputStream inputStream = new FileInputStream(image_file); // 得到真实路径 String path = "/img";// 项目路径 String realPath = getRealPath(path, request);// 真实路径 System.out.println(realPath); // 拼接上名字 String newName = getNewFileName(image_fileFileName); String finalPaht = realPath + "\\" + newName; // 得到输出流 FileOutputStream fileOutputStream = new FileOutputStream(finalPaht); // copy IOUtils.copy(inputStream, fileOutputStream); // 关闭资源 fileOutputStream.close(); inputStream.close(); // ----------------------------------------------------------- // 开始剪切 if (!x1.equals("")) { String imagePaht = finalPaht;// 真实路径ok?项目路径x? ImageCut.cutImage(imagePaht, (int) Double.parseDouble(x1), (int) Double.parseDouble(y1), width, width); } User user2 = (User) request.getSession().getAttribute("user"); String imgPath = path + "/" + newName; request.setAttribute("imgUrl", imgPath); user.setImgUrl(imgPath); user.setId(user2.getId()); us.updateImg(user); } return "upLoadImage"; } // 得到文件夹在服务器的真实路径 public String getRealPath(String dirPath, HttpServletRequest request) { // 通过request得到上下文对象,调用方法getRealPath得到真实路径 String realPath = request.getServletContext().getRealPath(dirPath); // 判断有没有改文件夹,没有就创建一个 // 构建file对象 File file = new File(realPath); if (!file.exists()) { file.mkdirs(); } return realPath; } // 得到新的文件名 public String getNewFileName(String oldName) { // 得到文件的后缀 int lastIndexOf = oldName.lastIndexOf("."); String substring = oldName.substring(lastIndexOf); // System.out.println(substring); // 生成一个很难重复的新文件名 UUID String uuid = UUID.randomUUID().toString();// 生成一个uuid随机字符串 String newName = uuid + substring; return newName; } @Override public User getModel() { user = new User(); return user; }}

 

 

spring

hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.show_sql=true hibernate.format_sql=true hibernate.current_session_context_class = org.springframework.orm.hibernate4.SpringSessionContext
mapping/admin.hbm.xml
mapping/user.hbm.xml

 

转载于:https://www.cnblogs.com/fs94/p/7978985.html

你可能感兴趣的文章
01: socket模块
查看>>
mysql触发器
查看>>
淌淌淌
查看>>
web页面实现指定区域打印功能
查看>>
使用PHP拆分中文字符串的方法(收藏) 小节
查看>>
win10每次开机都显示“你的硬件设置已更改,请重启电脑……”的解决办法
查看>>
VMware环境和Window环境进行网络连接的问题
查看>>
macOS10.12允许所有来源设置
查看>>
C++有关 const & 内敛 & 友元&静态成员那些事
查看>>
函数积累
查看>>
python搜索引擎(转)
查看>>
关于height,line-height导致的样式混乱的问题
查看>>
《SEO实战密码》读后一点感受
查看>>
bzoj 4815 [Cqoi2017]小Q的表格——反演+分块
查看>>
Swift 入门之简单语法(六)
查看>>
shim和polyfill有什么区别
查看>>
Failed to load the JNI shared library “E:/2000/Java/JDK6/bin/..jre/bin/client/jvm.dll
查看>>
Zabbix3.4服务器的搭建--CentOS7
查看>>
〖Python〗-- IO多路复用
查看>>
栈(括号匹配)
查看>>