使用Struts2 and Hibernate MVC框架定餐项目
此项目使用了一下技术:
Struts2
Hibernate
JDBC
Jsp
使用的是MVC框架
项目架构
项目主要功能:注册 登录 购物车 查看订单 上传菜谱 分页显示
Dao层
所以接口方法
public interface UserClDao { /** * 用户登录 * @param uname 账号 * @param pwd 密码 * @return */ public boolean loginValidate(String uname,String pwd); /** * 注册用户 * @param uname 用户名 * @param pwd 密码 * @param phone 手机号码 * @return */ public boolean addUser(String uname,String pwd,String phone); public ArrayList<UserBean> userList(int pageNow,int pageSize); /** * 获取总页数 * @param subhql * @return */ public int pageCount(String subhql,int pageSize); /** * 通过foodid查询菜的详情 */ public foods getfood(int foodid); /** * 上传菜谱 */ public foods sc(String fname,double fprice,String fjs,String foodphoto); /** * 购物车 * */ public void gwc(int userid,int foodsid,double foodprice,String foodname,String food); /** * 获取用户ID * @param uname */ public void getuserid(String uname); /** * 结算商品总价 * @param userid * @return */ public double foodzj(int userid); /** * 获取购物车数据 * @param userid * @return */ public List<MyCarBean> queryCar(int userid); /** * 删除购物车数据 * @param uid * @return */ public void del(int uid); /** * 生成订单 * @param userid * @param uname * @param scdz * @param phone * @param spzj * @param mark */ public void scdd(int userid,String uname,String scdz,String phone,double spzj,String mark); /** * 获取订单详情 * @param userid * @return */ public List<dingDang> ddxq(int userid); /** * 修改密码 * @param username * @param passWord * @param epassWord * @return */ public boolean xgmm(String username,String passWord,String epassWord); }
实体类
UserBean实体
public class UserBean { private int userid; private String userName; private String passWord; private String passWord1; private String phone; public int getUserid() { return userid; } public void setUserid(int userid) { this.userid = userid; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassWord() { return passWord; } public void setPassWord(String passWord) { this.passWord = passWord; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getPassWord1() { return passWord1; } public void setPassWord1(String passWord1) { this.passWord1 = passWord1; } }
菜谱实体
public class UserBean { private int userid; private String userName; private String passWord; private String passWord1; private String phone; public int getUserid() { return userid; } public void setUserid(int userid) { this.userid = userid; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassWord() { return passWord; } public void setPassWord(String passWord) { this.passWord = passWord; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getPassWord1() { return passWord1; } public void setPassWord1(String passWord1) { this.passWord1 = passWord1; } }
购物车实体
public class MyCarBean { private int id; private int userid; private int foodsid; private double foodprice; private String foodname; private int foodnum; private String foodphoto; public int getId() { return id; } public void setId(int id) { this.id = id; } public int getUserid() { return userid; } public void setUserid(int userid) { this.userid = userid; } public int getFoodsid() { return foodsid; } public void setFoodsid(int foodsid) { this.foodsid = foodsid; } public int getFoodnum() { return foodnum; } public void setFoodnum(int foodnum) { this.foodnum = foodnum; } public double getFoodprice() { return foodprice; } public void setFoodprice(double foodprice) { this.foodprice = foodprice; } public String getFoodname() { return foodname; } public void setFoodname(String foodname) { this.foodname = foodname; } public String getFoodphoto() { return foodphoto; } public void setFoodphoto(String foodphoto) { this.foodphoto = foodphoto; } }
订单实体
public class dingDang { private int id; private int userId; private String userName; private String scdz; private String phone; private double zfje; private String mark; private Date date; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getScdz() { return scdz; } public void setScdz(String scdz) { this.scdz = scdz; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public double getZfje() { return zfje; } public void setZfje(double zfje) { this.zfje = zfje; } public String getMark() { return mark; } public void setMaerk(String mark) { this.mark = mark; } public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } }
Hibernate 映射配置
订单.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.it.mod"> <class name="dingDang"> <id name="id"> <generator class="native" /> </id> <property name="userId" /> <property name="userName" /> <property name="scdz" /> <property name="phone" /> <property name="zfje" /> <property name="maerk" /> <property name="date" /> </class> </hibernate-mapping>
实体.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.it.mod"> <class name="UserBean"> <id name="userid"> <generator class="native"/> </id> <property name="userName" /> <property name="passWord" /> <property name="phone" /> </class> </hibernate-mapping>
菜谱.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.it.mod"> <class name="UserBean"> <id name="userid"> <generator class="native"/> </id> <property name="userName" /> <property name="passWord" /> <property name="phone" /> </class> </hibernate-mapping>
购物车.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.it.mod"> <class name="UserBean"> <id name="userid"> <generator class="native"/> </id> <property name="userName" /> <property name="passWord" /> <property name="phone" /> </class> </hibernate-mapping>
注册:
Action类
1 public class NewUser extends ActionSupport{ 2 private UserBean ub; 3 private UserClDao ucl = new UserClDaoImp(); 4 public String zc(){ 5 System.out.println(ub.getUserName()); 6 System.out.println(ub.getPhone()); 7 System.out.println(ub.getPassWord()); 8 9 if(ucl.addUser(ub.getUserName(),ub.getPassWord(),ub.getPhone())){ 10 System.out.println("注册成功!"); 11 return "y"; 12 }else{ 13 14 this.addActionError("用户名已存在"); 15 return "input"; 16 } 17 18 } 19 20 21 public UserBean getUb() { 22 return ub; 23 } 24 25 public void setUb(UserBean ub) { 26 this.ub = ub; 27 } 28 29 30 public UserClDao getUcl() { 31 return ucl; 32 } 33 34 35 public void setUcl(UserClDao ucl) { 36 this.ucl = ucl; 37 } 38 39 40 41 }
业务代码
public boolean addUser(String uname, String pwd, String phone) { System.out.println("进入注册。。。。"); boolean b = false; Session s = null; Transaction tr= null; UserClDaoImp ucl = new UserClDaoImp(); if(ucl.pc(uname)){ return false; } try{ if(uname != null && pwd!=null && phone !=null){ s = HibernateUtil.getSession(); tr = s.beginTransaction(); UserBean ub = new UserBean(); ub.setUserName(uname); ub.setPassWord(pwd); ub.setPhone(phone); s.save(ub); tr.commit(); b = true; }else{ return false; } }finally{ if(s !=null){ s.close(); } } return b; }
注册校验器配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.dtd">
<validators>
<field name="ub.userName">
<field-validator type="stringlength">
<param name="minLength">3</param>
<message>用户名长度不能少于3位</message>
</field-validator>
</field>
<field name="ub.passWord">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>密码不能为空!~</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">6</param>
<message>密码长度不能少于6位</message>
</field-validator>
</field>
<field name="ub.passWord1">
<field-validator type="fieldexpression">
<param name="expression"><![CDATA[ub.passWord1==ub.passWord]]></param>
<message>两次密码不一至</message>
</field-validator>
</field>
</validators>
JSP 预览
登录功能
Action类
public class UserAction { private UserBean ub; private UserClDao ucl = new UserClDaoImp(); public String yz(){ System.out.println(ub.getUserName()+ub.getPassWord()); if(ucl.loginValidate(ub.getUserName(),ub.getPassWord())){ ucl.getuserid(ub.getUserName()); System.out.println("登录成功!"); return "y"; }else{ System.out.println("登录失败!"); return "n"; } } public UserBean getUb() { return ub; } public void setUb(UserBean ub) { this.ub = ub; } public UserClDao getUcl() { return ucl; } public void setUcl(UserClDao ucl) { this.ucl = ucl; } }
业务代码
public boolean loginValidate(String uname, String pwd) { System.out.println("进入用户验证....."); boolean b = false; Session s = null; System.out.println("uname:"+uname); try{ s = HibernateUtil.getSession(); String Hql="from UserBean as u where u.userName =:name"; Query query = s.createQuery(Hql); query.setString("name",uname); List <UserBean> list = query.list(); if(list.size() > 0){ for(UserBean u:list){ System.out.println("loginValidate"); if(pwd.equals(u.getPassWord())){ b = true; break; } } } }finally{ if(s !=null){ s.close(); } } return b; }
JSP预览
购物车
public class MyCarAction { private foods f; private List<MyCarBean> foodlist; private double foodzj; private String Type; UserClDao ucl = new UserClDaoImp(); ActionContext ctx = ActionContext.getContext(); public String gwc(){ System.out.println("Type:"+Type); System.out.println("进入购物车"); int userid = (int)ctx.getSession().get("userid"); if(Type !=null && Type.equals("ckgwc")){ foodlist = ucl.queryCar(userid); foodzj = ucl.foodzj(userid); ctx.getSession().put("foodzj",foodzj); }else{ ucl.gwc(userid, f.getFoodid(),f.getFoodprice(),f.getFoodname(),f.getFoodphoto()); foodlist = ucl.queryCar(userid); foodzj = ucl.foodzj(userid); ctx.getSession().put("foodzj",foodzj); } return "ok"; } public foods getF() { return f; } public void setF(foods f) { this.f = f; } public List<MyCarBean> getFoodlist() { return foodlist; } public void setFoodlist(List<MyCarBean> foodlist) { this.foodlist = foodlist; } public double getFoodzj() { return foodzj; } public void setFoodzj(double foodzj) { this.foodzj = foodzj; } public String getType() { return Type; } public void setType(String type) { Type = type; } }
购物车业务代码
public void gwc(int userid, int foodsid, double foodprice,String foodname,String foodphoto) { Session s = null; Transaction tr = null; System.out.println("abcd"); try{ s = HibernateUtil.getSession(); tr = s.beginTransaction(); MyCarBean mcb = new MyCarBean(); mcb.setFoodsid(foodsid); mcb.setFoodprice(foodprice); mcb.setUserid(userid); mcb.setFoodnum(1); mcb.setFoodname(foodname); mcb.setFoodphoto(foodphoto); s.save(mcb); tr.commit(); }finally{ if( s!=null){ s.close(); } } }
JSP预览
上传菜谱
Action
public class FileAction { private File file; private String fileFileName; private String fileContentType; private foods f ; private UserClDao ucl = new UserClDaoImp(); public String sc() throws IOException{ String filePath = "D:/eclipse/java eclipse/Ordering/WebContent/images"; File os = new File(new File(filePath), fileFileName); if(!os.getParentFile().exists()){ os.getParentFile().mkdirs(); } FileUtils.copyFile(file,os); System.out.println("fileFileName: " + fileFileName); System.out.println("file: " + file.getName()); System.out.println("file: " + file.getPath()); System.out.println(file); String tx = "images/"+fileFileName; System.out.println("Tx:"+tx); ucl.sc(f.getFoodname(),f.getFoodprice(),f.getFoodjs(),tx); return "pass"; } public File getFile() { return file; } public void setFile(File file) { this.file = file; } public String getFileFileName() { return fileFileName; } public void setFileFileName(String fileFileName) { this.fileFileName = fileFileName; } public String getFileContentType() { return fileContentType; } public void setFileContentType(String fileContentType) { this.fileContentType = fileContentType; } public foods getF() { return f; } public void setF(foods f) { this.f = f; } }
业务代码
public foods sc(String fname, double fprice, String fjs, String foodphoto) { Session s = null; Transaction tr = null; foods f = new foods(); try{ s = HibernateUtil.getSession(); tr = s.beginTransaction(); f.setFoodjs(fjs); f.setFoodname(fname); f.setFoodprice(fprice); f.setFoodphoto(foodphoto); s.save(f); tr.commit(); }finally{ if( s !=null){ s.close(); } } return f; }
Jsp预览
Struts.xml配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <constant name="struts.configuration.xml.reload" value="true" /> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <package name="User" namespace="/test" extends="struts-default"> <interceptors> <interceptor name="loginInterceptor" class="com.it.control.LoginInterceptor"> </interceptor> <interceptor-stack name="loginInterceptorStack"> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="loginInterceptor"></interceptor-ref> </interceptor-stack> </interceptors> <global-allowed-methods>regex:.*</global-allowed-methods> <action name="login_*" class="com.it.control.UserAction" method="{1}"> <result type="redirectAction" name="y">fenye_fenye</result> <result name="n">/login.jsp</result> </action> <action name="newuser_*" class="com.it.control.NewUser" method="{1}"> <result name="y">/login.jsp</result> <result name="input">/NewUser.jsp</result> </action> <action name="fenye_*" class="com.it.control.fenyeAction" method="{1}"> <result name="ok">/Wlecome.jsp</result> </action> <action name="xq_*" class="com.it.control.xqAction" method="{1}"> <result name="ok">/xiangqing.jsp</result> </action> <action name="sc_*" class="com.it.control.FileAction" method="{1}"> <result type="redirectAction" name="pass">fenye_fenye</result> <result name="no">/login.jsp</result> <interceptor-ref name="loginInterceptor"></interceptor-ref> </action> <action name="gwc_*" class="com.it.control.MyCarAction" method="{1}"> <result name="ok">/gwc.jsp</result> </action> <action name="tc_*" class="com.it.control.TcAction" method="{1}"> <result type="redirectAction" name="ok">fenye_fenye</result> </action> <action name="del_*" class="com.it.control.DelAction" method="{1}"> <result name="ok">/gwc.jsp</result> <result name="no">/login.jsp</result> <interceptor-ref name="loginInterceptor"></interceptor-ref> </action> <action name="tjdd_*" class="com.it.control.JieSuan" method="{1}"> <result name="ok">/tjcg.jsp</result> <result name="no">/login.jsp</result> </action> <action name="userzx_*" class="com.it.control.userzxAction" method="{1}"> <result name="ok">/userzx.jsp</result> <result name="no">/login.jsp</result> <interceptor-ref name="loginInterceptor"></interceptor-ref> </action> <action name="sccp_*" class="com.it.control.SccpAction" method="{1}"> <result name="ok">/sccp.jsp</result> <result name="no">/login.jsp</result> <!-- <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="loginInterceptor"></interceptor-ref> --> </action> <action name="xgmm_*" class="com.it.control.xgmmAction" method="{1}"> <result name="ok">/xgcg.jsp</result> <result name="no">/userzx.jsp</result> </action> </package> </struts>