分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

注解式struts2配合ajax

发布时间:2023-09-06 01:28责任编辑:熊小新关键词:暂无标签

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;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<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

原文地址:http://www.cnblogs.com/fs94/p/7978985.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved