註解式struts2配合ajax
阿新 • • 發佈:2017-12-04
prototype 建議 read ner struts .sh values mage -a
struts代碼
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.action.extension" value="do" /> <do代表請求方式,可改成action><constant name="struts.convention.package.locators" value="com" /> <把struts交給spring管理> </struts>
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; importjava.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<User> { 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<String, Object> data = new HashMap<>(); public Map<String, Object> getData() { return data; } public void setData(Map<String, Object> 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
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.zy"></context:component-scan> <!-- 配置連接池 --> <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 四大組件 --> <property name="driverClass" value="com.mysql.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/video_system_s"/> <property name="user" value="root"/> <property name="password" value="123"/> <property name="maxPoolSize" value="10"/> <property name="minPoolSize" value="2"/> </bean> <!-- 配置sessionFactory工廠 --> <bean name="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /><!-- 引用上邊的數據源 --> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.show_sql=true hibernate.format_sql=true hibernate.current_session_context_class = org.springframework.orm.hibernate4.SpringSessionContext </value><!-- getCurrentSession --> </property> <property name="mappingResources"><!-- 配置映射 --> <list> <value>mapping/admin.hbm.xml</value> <value>mapping/user.hbm.xml</value> </list> </property> </bean> <!-- 配置事務管理 --> <bean name="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property><!-- 引用sessionFactory --> </bean> <!-- 配置AOP --> <aop:config> <!-- 配置切點 --> <!-- 任意類任意方法的參數 --> <aop:pointcut expression="execution(public * com.zy.service.impl.*.*(..))" id="myPointCut"/> <aop:advisor advice-ref="myAdvice" pointcut-ref="myPointCut"/> </aop:config> <!-- 建議 --> <tx:advice id="myAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="select*" read-only="true"/> <tx:method name="save*"/> <tx:method name="delete*"/> <tx:method name="update*"/> </tx:attributes> </tx:advice> </beans>
註解式struts2配合ajax