一:先随便建一个项目,为了方便架包的导入。(其实也可以不这样做)
在项目新建Add Spring Capabilities(Spring 3.1),勾选Spring3.1 前三个 Springlibraries add to buildpath
.在项目新建一个Hibernate3.2 勾选Hibernate 3.2Core Libraries,下一步勾选Spring configure file 在下一步选择Existing Spring conguration file .DB Driver勾选提前新建好的DB Driver。
在项目新建一个Struts Capabilities 选择 Struts 2.1 勾选都一个核心包,勾选Struts2 Spring Libraries
在项目新建一个entity 进入MyElicpse表结构视图 选择表 进行过hibernate反向生成配置文件和实体表(过程是默认选项),
注意生成的Myuser.java 中的属性 的类型有不合适的要改,在自己生成get sert方法。构造 默认构造方法
Myuser.hbm.xml里面的属性
注意seq_equsencey要自己写。
<id name="id" type="java.lang.Integer">
<column name="ID" precision="7" scale="0" />
<generator class="sequence">
<param name="sequence">seq_mysuser</param>
</generator>
</id>
<property name="username" type="java.lang.String">
<column name="USERNAME" length="20" />
</property>
<property name="userpwd" type="java.lang.String">
<column name="USERPWD" length="20" />
</property>
<property name="nickname" type="java.lang.String">
<column name="NICKNAME" length="20" />
</property>
type的类型要删掉
二、重新建一个新的项目
把34个jar包加入lib包,把原来的新建好的applicationContext.xml和struts.xml 和实体类和实体类的Myuser.hbm.xml放到自己新建好饿项目
(其实这样做的目的就是自己加入包,这样的更干净,不用他自己自动生成的jar包。不同的框架生成的架包,貌似冲突)
配置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">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--spring的监听器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--hibernate的OSIV -->
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!--struts2的filter -->
<filter>
<filter-name>Struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
</web-app>
新建一个UserDao类
UserDao要继承HibernateDaoSupport
package dao;
import java.util.List;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import entity.Myuser;
public class UserDao extends HibernateDaoSupport {
/*
* 根据用户名和密码查询用户
*/
public Myuser findUseraByNameAndPwd(Myuser user){
//String hql="from Myuser where username=?and userpwd=?";
/*List<Myuser>userList=getHibernateTemplate().find(hql);*/
String hql="from Myuser where username=:username and userpwd=:userpwd";
List<Myuser>userList=getHibernateTemplate().findByValueBean(hql,user);
if (userList.size()>0)
return userList.get(0);
return null;
//另一种查询方法
/*Query query=getSession().createQuery(hql);
query.setProperties(user);
user=(Myuser)query.uniqueResult();
if(null!=user){
return user;
}else{
return null;
}*/
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver">
</property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl">
</property>
<property name="username" value="system"></property>
<property name="password" value="password"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>entity/Myuser.hbm.xml</value>
</list>
</property>
</bean>
<!--以上配置都是自动生成的,下面是自己要配置的 --><!-- userDao要用session进行hql查询所以要引用sessionFactory -->
<!-- DAO -->
<bean id="userDao" class="dao.UserDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- SERVICE --><!--service里面有dao这个属性。所以这个service类要配置下,要引用上面配置的userDao类 -->
<bean id="userService" class="service.UserService">
<property name="userDao" ref="userDao"></property>
</bean>
<!--声明事务管理 -->
</beans>
编写UserDao
package service;
import dao.UserDao;
import entity.Myuser;
public class UserService {
private UserDao userDao;//这里是以UserDao为属性的
/*
* 用户登录业务方法
*/
public Myuser findUser(Myuser user){
return userDao.findUseraByNameAndPwd(user);
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}
登录页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登录页面</title>
</head>
<body>
<form action="loginUser.action" method="post">
用户名:<input type="text" name="user.username"/><br/>
密码:<input type="password" name="user.userpwd"/><br/>
<input type="submit" value="登录">
</form>
</body>
</html>
Action、
package action;
import service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import entity.Myuser;
public class UserAction extends ActionSupport {
private Myuser user;
private UserService userService;// 和spring的配置文件中的beanId相同
//登录处理方法
public String login(){
user=userService.findUser(user);
if (null!=user) {
ActionContext.getContext().getSession().put("u", user);
return SUCCESS;
}else{
return ERROR;
}
}
public Myuser getUser() {
return user;
}
public void setUser(Myuser user) {
this.user = user;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
}
strtus.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="*User" class="action.UserAction" method="{1}">
<result>success.jsp</result>
<result name="error">index.jsp</result>
</action>
</package>
</struts>
登录成功页面jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登陆成功页面</title>
</head>
<body>
恭喜:${u.nickname}
</body>
</html>