SSH2 Step by Step- Step 3 Struts2和Hibernate的整合

版权声明:欢迎转载,转载请注明作者和出处 https://blog.csdn.net/bruesz/article/details/7011644

坑爹呀...保存的一篇掉了,只得重写....

话说前两篇介绍了怎么配置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. 开始验证功能:

注册界面:

注册成功后的界面(这里有点奇怪,日期类型的参数传不过来,容后再看..):

数据库查询结果 (之前已经:


源代码下载: 点此下载

猜你喜欢

转载自blog.csdn.net/bruesz/article/details/7011644