所用技术:struts2 ,hibernate,jsp,mysql
本DEMO仅仅实现用户登录功能,采用MVC思想,自己也觉得相对是比较简单,比较容易理解数据流向的一个例子,通过整合这个过程,能够清晰的看出整个项目工程的数据流向和设计思想,是新手对于整合struts2和hibernate的最好的例子。
现将整合思想,过程,代码整理如下,如果新手看了还是有不明白或者需要源码,本人乐意效劳和分享源码。
JSP作为视图层V,显示登录,登录成功,失败页面;Struts2作为控制层C处理页面跳转;Hibernate用作数据模型M,它与前台程序的接口以DAO形式提供。
一.数据库设计:
1.创建数据库test 表名为:user
id int not null 自增
username varchar(11) not null
password varchar(11 not null)
二.M层开发:
2.Hibernate基本配置:
为Web项目添加Hibernate的相关jar包,hibernate.cfg.xml,并应用右击项目文件,myeclise,add hibernate …将建立sessionfacotry.这样就不用再自己去写了,实现了高效开发。下为hibernate.cfg.xml:
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="connection.username">root</property> <property name="connection.url">jdbc:mysql:///test</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="connection.password">123456</property> <property name="connection.driver_class">org.gjt.mm.mysql.Driver</property> <!-- 在控制台显示SQL语句 --> <property name="show_sql">true</property> <mapping resource="com/red/vo/User.hbm.xml" /> </session-factory> </hibernate-configuration>
3.生成POJO(Plain Old Java Object,简单的Java对象,通常也称为VO[ Value Object ]对象,值对象)对象类 ,POJO是一种特殊的Java类,其中有一些属性及其对应的getter/setter方法,不允许有业务方法。将User.hbm.xml配置文件一定要放在和User实体bean相同的文件目录下。(附User.hbm.xml代码)
<?xml version="1.0" encoding='UTF-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > <hibernate-mapping package="com.red.vo"> <class name="User" table="user"> <id name="id" column="id" type="integer"> <generator class="identity"/> </id> <property name="username" column="username" type="string" /> <property name="password" column="password" type="string" /> </class> </hibernate-mapping>
4.在src中另外建立两个包,分别放DAO接口IUserDAO及其实现类UserDAO。(附IUserDAO ,UserDAO 源码)
UserDAO.java
package com.red.dao.impl; import java.util.List; import javax.persistence.Query; import com.red.dao.IUserDAO; import com.red.factory.HibernateSessionFactory; import com.red.vo.User; /** * 实现接口 * @author Red * */ public class UserDAO implements IUserDAO{ public User validateUser(String username,String password){ String sql="from User u where u.username=? and u.password=?"; org.hibernate.Query query=HibernateSessionFactory.getSession().createQuery(sql); query.setParameter(0, username); query.setParameter(1, password); List users=query.list(); if(users.size()!=0){ User user=(User)users.get(0); return user; } HibernateSessionFactory.closeSession(); return null; } }
IUserDAO.java
package com.red.dao; import com.red.vo.User; /** * 接口 * @author Red * */ public interface IUserDAO { public User validateUser(String username,String password); }
三.C层开发:
5.加载,配置Struts2:
将jar包复制到lib目录下,并且将mysql的驱动包也放进去,修改web.xml的代码。(附web.xml代码)
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping><filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
6.实现Action:
在src中新建包action,创建类LoginAction类:(附代码LoginAction.java)
package com.red.action; import com.opensymphony.xwork2.ActionSupport; import com.red.dao.IUserDAO; import com.red.dao.impl.UserDAO; import com.red.vo.User; /** * 用户登录action * @author Red * */ public class LoginAction extends ActionSupport{ private String username; private String password; //处理用户请求的execute方法 public String execute() throws Exception{ boolean validated=false;//验证成功标识 IUserDAO userDAO=new UserDAO(); User user=userDAO.validateUser(getUsername(),getPassword()); if(user!=null) { validated=true; } if(validated) { //验证成功返回字符串“success” return SUCCESS; } else { //验证失败返回字符串“error” return ERROR; } } 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; } }
7.在struts.xml 中配置action:(附代码:struts.xml)
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="struts" extends="struts-default"> <action name="login" class="com.red.action.LoginAction"> <result name="success">/welcome.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts>
四.V层开发:
就是写当个JSP文件。源码序列分别是:index.jsp;welcome.jsp;error.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% 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>login</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> <form action="login.action" method="post"> 用户名:<input type="text" name="username"/><br/> 密码:<input type="password" name="password"/><br/> <input type="submit" value="登 录"> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% 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>welcome</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> <s:property value="username"/>,您好!欢迎光临RED‘S HOME. </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% 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>error</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> 登录失败! <br> </body> </html>
欢迎大神拍砖,新手朋友:我们可以一起学习学习呃。