package com.kettas.action;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.MappingDispatchAction;
import com.kettas.biz.UserBiz;
import com.kettas.entity.User;
import com.kettas.exception.BizException;
import com.kettas.exception.LoginFailedException;
import com.kettas.exception.SystemException;
/*
* Title:用户操作相关Action
* Author:SecondGroup
* Time:2010.1.14
* Version:1.0.0
*/
public class UserAction extends MappingDispatchAction{
/*
* setter依赖注入
*/
private UserBiz ubiz;
public UserBiz getUbiz() {
return ubiz;
}
public void setUbiz(UserBiz ubiz) {
this.ubiz = ubiz;
}
/*
* 用户登陆
*/
public ActionForward login(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){
String name=request.getParameter("name");
String password=request.getParameter("password");
//验证码大小写不敏感
String validateCode=request.getParameter("validateCode").toUpperCase();
//System.out.println("============="+name+"============"+password+"===================");
HttpSession session=request.getSession(true);
String vcode=(String) session.getAttribute("validateCode");
if((validateCode.equals(vcode))&&(! vcode.equals(null))){}
User user=new User();
try {
if((validateCode.equals(vcode))&&(! vcode.equals(null))){
user=ubiz.login(name, password);
session.setAttribute("login", user);
return mapping.findForward("getAllProductTypes");
}else{
request.setAttribute("validateFailed", "验证码错误");
return mapping.findForward("login");
}
} catch (LoginFailedException e) {
throw new LoginFailedException(e);
} catch (Exception e){
//如果在运行过程中遇到其他未预料到的系统级别异常则定向到错误页面
throw new SystemException(e);
}
}
/*
* 用户注册Action
*/
public ActionForward regist(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){
DynaActionForm daf=(DynaActionForm) form;
/*
* 从客户端Form获得参数
*/
String name=(String) daf.get("name");
String password=(String) daf.get("password");
String address=(String) daf.get("address");
String postCode=(String) daf.get("postCode");
String email=(String) daf.get("email");
String homePhone=(String) daf.get("homePhone");
String cellPhone=(String) daf.get("cellPhone");
String officePhone=(String) daf.get("officePhone");
String question=(String) daf.get("question");
String answer=(String) daf.get("answer");
User user=new User(name,password,address,postCode,email,homePhone,cellPhone,officePhone,question,answer);
try {
ubiz.regist(user);
} catch (Exception e) {
throw new BizException(e);
}
HttpSession session=request.getSession(true);
session.setAttribute("login", user);
return mapping.findForward("jump");//注册成功之后5秒跳转首页
}
/*
* 用户修改信息Action
*/
public ActionForward modifyUser(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){
String idStr=request.getParameter("id");
Integer id=Integer.parseInt(idStr);
String name=request.getParameter("name");
String password=request.getParameter("password");
String address=request.getParameter("address");
String postCode=request.getParameter("postCode");
String email=request.getParameter("email");
String homePhone=request.getParameter("homePhone");
String cellPhone=request.getParameter("cellPhone");
String officePhone=request.getParameter("officePhone");
String question=request.getParameter("question");
String answer=request.getParameter("answer");
User user=new User();
try {
user = ubiz.getUserById(id);
} catch (Exception e) {
throw new BizException(e);
}
user.setName(name);
user.setPassword(password);
user.setAddress(address);
user.setPostCode(postCode);
user.setEmail(email);
user.setHomePhone(homePhone);
user.setCellPhone(cellPhone);
user.setOfficePhone(officePhone);
user.setQuestion(question);
user.setAnswer(answer);
//调用biz层方法修改User资料
try {
ubiz.modifyUser(user);
//提示用户修改成功
request.setAttribute("message", "修改成功!");
} catch (Exception e) {
throw new BizException(e);
}
HttpSession session=request.getSession(true);
session.setAttribute("login", user);
return mapping.findForward("modifyuser");
}
/*
* 用户注销登出,销毁session
*/
public ActionForward logout(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){
String clearsession=request.getParameter("clearsession");
if((clearsession!=null)&&(clearsession.equals("true"))){
HttpSession session=request.getSession(true);
session.removeAttribute("login");
session.invalidate();
}
return mapping.findForward("getAllProductTypes");
}
/*
* 找回密码Action
*/
public ActionForward findPassword(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){
String name=request.getParameter("name");
String answer=request.getParameter("answer");
User user=new User();
String password="";
try {
user=ubiz.getUserByName(name);
password=ubiz.findPassword(user.getId(), answer);
request.setAttribute("password", password);
} catch (Exception e) {
throw new BizException(e);
}
return mapping.findForward("findpassword");
}
/*
* 生成验证码action
*/
public ActionForward validateCode(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws IOException{
int width=60;
int height=20;
int codeCount=4;
int x=width/(codeCount+1);
//int fontHeight=height-2;
int codeY=height-4;
char[] codeSequence={'A','B','C','D','E','F','G','H','I','J',
'K','L','M','N','O','P','Q','R','S','T',
'U','V','W','X','Y','Z','1','2','3','4','5','6','7','8','9'};
//定义图像缓冲器
BufferedImage bImg=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics2D graph=bImg.createGraphics();
//创建一个随机数
Random random=new Random();
//设置背景色为灰色
graph.setColor(Color.WHITE);
graph.fillRect(0, 0, width, height);
//创建几组字体,随机选择
Font font[]=new Font[5];
font[0]=new Font("Ravie",Font.PLAIN,height-2);
font[1]=new Font("Antique Olive Compact",Font.PLAIN,height-2);
font[2]=new Font("Comic Sans MS",Font.PLAIN,height-2);
font[3]=new Font("Forte",Font.PLAIN,height-2);
font[4]=new Font("Gill Sans Ultra Bold",Font.PLAIN,height-2);
graph.setFont(font[random.nextInt(5)]);
//边框
graph.setColor(Color.BLACK);
graph.drawRect(0, 0, width-1, height-1);
graph.setColor(Color.LIGHT_GRAY);
for(int i = 0; i < 100; i++)
{
int x0 = random.nextInt(width);
int y0 = random.nextInt(height);
int x1 = random.nextInt(12);
int y1 = random.nextInt(12);
graph.drawLine(x0, y0, x0 + x1, y0 + y1);
}
//randomCode用于保存随即产生的验证码,以便之后登陆时进行验证
StringBuffer randomCode=new StringBuffer();
int red=0;
int green=0;
int blue=0;
for(int i=0;i<codeCount;i++){
String str=String.valueOf(codeSequence[random.nextInt(35)]);
//red=random.nextInt(255);
red=16;
//green=random.nextInt(2
SSH实现的网上购物车项目是一个综合性的Web应用开发实例,主要使用了Spring、Struts2和Hibernate这三大Java开源框架。这些框架在IT行业中被广泛应用于构建高效、可维护的Web应用程序,尤其在企业级开发中占据重要地位。 Spring框架作为整个应用的核心,它提供了依赖注入(Dependency Injection,DI)和面向切面编程(Aspect-Oriented Programming,AOP)的能力。依赖注入使得组件之间的耦合度降低,提高了代码的可测试性和可扩展性。而面向切面编程则用于处理系统中的横切关注点,如日志、事务管理等,使业务逻辑更专注于核心功能。 Struts2作为MVC(Model-View-Controller)设计模式的实现框架,负责处理HTTP请求,控制应用程序的流程,并将数据传递给视图层。在购物车项目中,Struts2配置文件定义了各种Action类,这些类对应不同的用户操作,例如添加商品到购物车、查看购物车内容、结算等。Action类会调用Service层的方法来处理业务逻辑,并将结果返回给视图层展示。 Hibernate是持久层框架,它简化了与数据库的交互,通过对象关系映射(Object-Relational Mapping,ORM)技术将Java对象与数据库表进行映射。在这个项目中,Hibernate用于创建、查询、更新和删除商品、订单、用户等实体,与Oracle数据库进行数据交换。Oracle数据库以其稳定性、高性能和丰富的功能特性,被广泛应用于大型企业级应用中。 在购物车项目的实现过程中,用户可以通过页面选择商品,点击添加按钮后,请求会被Struts2拦截并转发给相应的Action处理。Action通过Hibernate与数据库交互,将商品信息存储到用户的购物车中。当用户准备结算时,Action会计算总价,创建订单,并将购物车中的所有商品关联到这个订单。同时,可能还会涉及到用户登录和权限验证,这部分可以利用Spring的安全模块来实现。 页面美观性是用户体验的重要组成部分,此项目可能采用了CSS样式表和JavaScript来提升页面的视觉效果和交互性。例如,使用Ajax技术可以实现实时更新购物车内容,无需每次操作都刷新整个页面。 SSH实现的网上购物车项目涵盖了Web开发中的多个关键技术和最佳实践,对于学习和理解Java Web开发有着重要的参考价值。开发者可以通过研究该项目的源码,加深对SSH框架的理解,以及如何将它们整合到实际项目中,从而提升自己的开发技能。
















































































































- 1
- 2
- 3

- 粉丝: 11
- 资源: 2
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- (源码)基于InternLM27B模型的金融财务分析系统.zip
- (源码)基于嵌入式系统的室内火灾延迟装置.zip
- (源码)基于Arduino的220V定时器与温度控制器.zip
- (源码)基于pygame库的三类迷宫游戏项目.zip
- (源码)基于MIPS32汇编语言的监控程序.zip
- (源码)基于Arduino的屏幕时间限制设备系统.zip
- 智慧园区管理系统-活动资源
- ChatGPT-MP-DeepSeek资源
- 基于鸿蒙的音频深度伪造检测应用-deveco studio
- open-Manus-manus ai
- 编程期末作业-社交媒体
- 大学生活动社交小程序-活动资源
- DBCHM-postgresql
- open-Manus-manus ai
- awesome-3D-gaussian-splatting-manus
- java-eight-part-java八股文



- 1
- 2
- 3
- 4
前往页