SSH2 Step by Step- Step 3 Struts2和Hibernate的整合
来源:互联网 发布:淘宝卖的钱在哪里 编辑:程序博客网 时间:2024/06/05 02:12
坑爹呀...保存的一篇掉了,只得重写....
话说前两篇介绍了怎么配置Struts和Hibernate,这篇就用一个简单的例子:用户注册,将这两种技术整合起来。
1. 写一个注册界面registration.jsp,这里用到了Java国际化的一些标记,能看懂的就看,看不懂的先别理他....
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib uri="/struts-tags" prefix="s" %> <%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><base href="<%=basePath%>"><title>Insert title here</title><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><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"></head><body><p>Registration Test</p><s:form name="registrationFrm" action="registration"><s:textfield name="name" key="username"></s:textfield><s:password name="password" key="password"></s:password><s:textfield name="birthday" key="birthday"/><s:submit label="submit"></s:submit></s:form><s:actionerror/></body></html>
2. 在struts.xml中增加一个action的mapping
<action name="registration" class="test.RegistrationAction"><result name="success" >index.jsp</result><result name="input">registration.jsp</result><result name="error">registration.jsp</result></action>
3. 增加一个注册的action类
package test;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;import com.test.model.User;import com.test.service.UserService;public class RegistrationAction extends ActionSupport implements ModelDriven<User> {private User user = new User();public User getModel() {return user;}public String execute(){System.out.println(user.toString());UserService us = new UserService();us.saveUser(user);return SUCCESS;}}
上面的类还引用了一个业务逻辑类,如下:
package com.test.service;import com.test.dao.UserDao;import com.test.model.User;public class UserService {private UserDao userdao = new UserDao();public void saveUser(User user){userdao.saveUser(user);}public User getUserInfo(String username){User user = userdao.getUserbyName(username);return user;}public boolean login(String username, String password){User user = userdao.getUserbyLoginInfo(username, password);if (null != user){return true;}else{return false;}}}
跟数据库的打交道的DAO类:
package com.test.dao;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import com.test.model.User;public class UserDao {private SessionFactory factory;public UserDao(){Configuration cfg = new Configuration().configure();this.factory = cfg.buildSessionFactory();}public void saveUser (User user){Session session = this.openSession();Transaction tx = null;try{//打开事务tx = session.beginTransaction();session.save(user);//关闭事务tx.commit();}catch(Exception ex){tx.rollback();ex.printStackTrace();}finally{session.close();}}public User getUserbyName(String username){Session session = this.openSession();String hql = "from user where name=:name";Query query = session.createQuery(hql);query.setString("name", username);//用uniqueResult获取唯一对象,或者用list获取对象列表User user = (User) query.uniqueResult();return user;}public User getUserbyLoginInfo(String username, String password){Session session = this.openSession();String hql = "from User where name=:name and password=:password";Query query = session.createQuery(hql);query.setString("name", username);query.setString("password", password);//用uniqueResult获取唯一对象,或者用list获取对象列表User user = (User) query.uniqueResult();return user;}public Session openSession(){Session session = this.factory.openSession();return session;}}
4. 注册成功后的显示jsp文件:
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib uri="/struts-tags" prefix="s" %> <%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><base href="<%=basePath%>"><title>SSH2 - Index</title><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><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="easyTalk"><meta http-equiv="description" content="This is my page"></head><body><p> <s:text name="username" /> <s:property value="#request.name"/></p><p> <s:text name="password" /> <s:property value="#request.password" /><p> <s:text name="birthday" /> <s:property value="#request.brithday"/></body></html>
5. 开始验证功能:
注册界面:
注册成功后的界面(这里有点奇怪,日期类型的参数传不过来,容后再看..):
源代码下载:点此下载
- SSH2 Step by Step- Step 3 Struts2和Hibernate的整合
- SSH2 Step by Step- Step 4 - Struts2、Spring和Hibernate的整合
- SSH2 Step by Step- Step 2 Hibernate的配置
- Struts2+Spring+Hibernate step by step 06 整合Hibernate
- SSH2 Step by Step- Step 1 Struts2初步配置学习
- SSH2 Step by Step- Step 1 Struts2初步配置学习
- struts2 tiles2 step by step
- Struts2+Spring+Hibernate step by step 03 整合Spring之一(在DAO层验证用户名和密码)
- Struts2+Spring+Hibernate step by step 04 整合Spring之二,从数据库验证用户名和密码
- Struts2+Spring+Hibernate step by step 01 创建Struts2项目
- Struts2+Spring+Hibernate step by step 07 ssh整合之增加用户
- Struts2+Spring+Hibernate step by step 08 ssh整合之删除用户
- Struts2+Spring+Hibernate step by step 09 ssh整合之查询用户
- Struts2+Spring+Hibernate step by step 10 ssh整合之修改用户
- Struts2+Spring+Hibernate step by step 11 ssh整合之拦截器验证用户是否登录
- Git Step By Step - Step 3: Branching
- mvn step by step 3
- jbpm4.3+SSH整合开发Step by Step
- Asterisk Registry(registe an account to local asterisk server)
- C/C++ New与Delete
- 关于maven的不错的文章
- 编写大容量和健壮的服务器系列—处理IOCP资源释放
- wpf Image动态加载图片终极解决办法
- SSH2 Step by Step- Step 3 Struts2和Hibernate的整合
- Oracle数据字典
- What's stdcall, cdecl, etc....
- OMF是什么?
- SQL判断某个表、存储过程、触发器、函数、某个字段是否存在
- CSS 参考手册
- Bridge 模式
- JS JavaScript 模态窗口为什么提交给自己总是弹出一个新窗口??
- 用gdb 调试GCC 程序