/* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */ package com.cn.struts.action; import java.io.ByteArrayInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.OutputStream; import java.lang.reflect.InvocationTargetException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //import org.apache.commons.beanutils.BeanUtils; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DispatchAction; import org.springframework.beans.BeanUtils; import org.springframework.util.FileCopyUtils; import com.cn.struts.dao.UserDao; import com.cn.struts.form.UserForm; import com.cn.struts.vo.UserVo; public class UserAction extends DispatchAction { private UserDao userDaoimp; public ActionForward regist(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { UserForm userForm = (UserForm) form;// TODO Auto-generated method stub UserVo user=new UserVo(); user.setUid(userForm.getUid()); user.setUname(userForm.getUname()); user.setUpassword(userForm.getUpassword()); try { user.setUimage(userForm.getUimage().getFileData()); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //BeanUtils.copyProperties(userForm, user); userDaoimp.insert(user); return mapping.findForward("seccuss"); } public ActionForward selectUser(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { String hql="from UserVo"; request.setAttribute("userList", userDaoimp.selectAll(hql)); return mapping.findForward("selectUser"); } public ActionForward deleteOne(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { //UserVo user=new UserVo(); int uid=new Integer(request.getParameter("uid")); userDaoimp.deleteOne(UserVo.class, uid); String hql="from UserVo"; request.setAttribute("userList", userDaoimp.selectAll(hql)); return mapping.findForward("selectUser"); } public ActionForward updateUser(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { //UserVo user=new UserVo(); int uid=Integer.parseInt(request.getParameter("uid")); System.out.println(uid); UserVo userVo=(UserVo)userDaoimp.selectOne(UserVo.class, uid); System.out .println(userVo.getUname()+":"+userVo.getUpassword()); //UserVo user=new UserVo(); //user.setUid(uid); //user.setUname(uname); //user.setUpassword(upassword); //userDaoimp.deleteOne(UserVo.class, uid); //String hql="from UserVo"; UserForm userForm = new UserForm(); //BeanUtils.copyProperties(userVo, userForm); userForm.setUid(userVo.getUid()); userForm.setUname(userVo.getUname()); userForm.setUpassword(userVo.getUpassword()); System.out.print(userForm.getUid()); request.setAttribute("userForm", userForm); return mapping.findForward("updateUser"); } public ActionForward updateUserDo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { UserForm userForm = (UserForm) form; UserVo user=new UserVo(); user.setUid(userForm.getUid()); user.setUname(userForm.getUname()); user.setUpassword(userForm.getUpassword()); userDaoimp.update(user); String hql="from UserVo"; request.setAttribute("userList", userDaoimp.selectAll(hql)); return mapping.findForward("selectUser"); } public ActionForward uimageShow(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { int uid=Integer.parseInt(request.getParameter("uid")); System.out.println(uid); UserVo userVo=(UserVo)userDaoimp.selectOne(UserVo.class, uid); System.out .println(userVo.getUname()+":"+userVo.getUpassword()); request.setAttribute("userVo", userVo); return mapping.findForward("uimageShow"); } public ActionForward uimage(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { int uid=Integer.parseInt(request.getParameter("uid")); System.out.println(uid); UserVo userVo=(UserVo)userDaoimp.selectOne(UserVo.class, uid); System.out .println(userVo.getUname()+":"+userVo.getUpassword()); /*response.setContentType("image/ico"); try { response.getOutputStream().write(userVo.getUimage()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ try { streamImage(userVo.getUimage(), response.getOutputStream()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public void streamImage(final byte[] blob, final OutputStream contentStream) throws Exception { ByteArrayInputStream is = new java.io.ByteArrayInputStream(blob); if (is != null) { FileCopyUtils.copy(is, contentStream); } } public UserDao getUserDaoimp() { return userDaoimp; } public void setUserDaoimp(UserDao userDaoimp) { this.userDaoimp = userDaoimp; } }
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %> <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <input type="text" value='<bean:write name="userVo" property="uname"/>'> <hr> <image src='/uimage.do?method=uimage&&uid=<bean:write name="userVo" property="uid"/>'/> <a href="selectUser.do?methods=selectUser">查询所有</a> </body> </html>