Hibernate-Spring-Struts
页面部分(注册和注册成功)
注册
Index.jsp |
<%@ page language="java"import="java.util.*"pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
<!DOCTYPEHTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <basehref="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title> <metahttp-equiv="pragma"content="no-cache"> <metahttp-equiv="cache-control"content="no-cache"> <metahttp-equiv="expires"content="0"> <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3"> <metahttp-equiv="description"content="This is my page"> <!-- <link rel="stylesheet" type="text/css"href="styles.css"> --> </head>
<body> 用户注册 <hr/> <formaction="register.do"name="registerform"id="registerform"method="post"> <br/> 用户ID:<inputtype="text"name="stu.userid"id="stu.userid"/> <br/> 用户名称:<inputtype="text"name="stu.username"id="stu.username"/> <br/> 用户密码:<inputtype="text"name="stu.pwd"id=stu.pwd/> <inputtype="submit"value="提交"/> </form> </body> </html>
|
注册成功
Success.jsp |
<%@ page language="java"import="java.util.*"pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
<!DOCTYPEHTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <basehref="<%=basePath%>">
<title>My JSP 'success.jsp' starting page</title>
<metahttp-equiv="pragma"content="no-cache"> <metahttp-equiv="cache-control"content="no-cache"> <metahttp-equiv="expires"content="0"> <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3"> <metahttp-equiv="description"content="This is my page"> <!-- <link rel="stylesheet" type="text/css"href="styles.css"> -->
</head>
<body> <astyle="color:red;font:25px;">Struts-Spring-Hibernate整合成功</a> </body> </html>
|
Web配置文件
Web.xml |
<?xmlversion="1.0"encoding="UTF-8"?> <web-appversion="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"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
<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>
<filter> <filter-name>filterDispatcher</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter>
<filter-mapping> <filter-name>filterDispatcher</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
</web-app>
|
全局属性文件的配置
Struts.properties |
struts.action.extension=do
struts.objectFactory=spring
struts.objectFactory.spring.autowire=name |
Struts配置文件
Struts.xml |
<?xmlversion="1.0"encoding="UTF-8"?> <!DOCTYPEstruts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <packagename="user"extends="struts-default"> <actionname="register"class="useraction"> <resultname="success">success.jsp</result> <resultname="fail">fail.jsp</result> </action> </package> </struts> |
Spring核心配置文件
ApplicationContext.xml |
<?xmlversion="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-2.5.xsd">
<importresource="applicationContext-dao.xml"/>
<importresource="applicationContext-tran.xml"/>
<importresource="applicationContext-service.xml"/>
<importresource="applicationContext-action.xml"/>
</beans> |
Spring节点配置文件(一)
ApplicationContext-action.xml |
<?xmlversion="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-2.5.xsd">
<beanid="useraction"class="com.ibm.action.UserManagerAction"> <propertyname="service"ref="useroperservice"></property> </bean> </beans> |
Spring节点配置文件(二)
applicationContext-service.xml |
<?xmlversion="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-2.5.xsd">
<beanid="useroperservice"class="com.ibm.service.iservice.UserOperService"> <propertyname="userOperDAO"ref="userdao"></property> </bean>
</beans> |
Spring节点配置文件(三)
applicationContext-dao.xml |
<?xmlversion="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-2.5.xsd">
<beanid="userdao"class="com.ibm.dao.idao.UserOperDAO"> <propertyname="sessionFactory"ref="sessionFactory"></property> </bean>
</beans> |
Spring节点配置文件(四)
applicationContext-tran.xml |
<?xmlversion="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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd ">
<beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <propertyname="configLocation" value="classpath:hibernate.cfg.xml"> </property> </bean>
<!-- 管理上面配置的SessionFactory --> <beanid="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <!-- 该"sessionFactory"是HibernateTransactionManager类的属性名,必须这么写 --> <propertyname="sessionFactory"ref="sessionFactory"></property> <!-- 上面的"ref"指定需要管理事务的SessionFactory,即上面配置的 -->
</bean>
<!--以下两个配置节点用来 --> <!-- 配置事务的传播属性 --> <tx:adviceid="tx"transaction-manager="transactionManager"> <tx:attributes> <tx:methodname="add*"propagation="REQUIRED"/> <tx:methodname="del*"propagation="REQUIRED"/> <tx:methodname="update*"propagation="REQUIRED"/> <!-- 找不到以上的方法则所有操作都默认执行下面这个方法,即以只读的方式操作(可以做"查"的操作) --> <tx:methodname="*"read-only="true"/> </tx:attributes> </tx:advice>
<!-- 切面(AOP)配置 --> <aop:config> <aop:pointcutexpression="execution (* com.ibm.service.*.*(..))"id="aid"/> <!-- 定义通知者(即advisor,它与aspect一样,它是由Pointcut与Advice组成的),一般用于事务的配置 * pointcut-ref:指定要使用的Pointcut,这里指定id即可 * advice-ref:指定Advice,这里指定id即可 * <aop:advisor>的含义就是将哪个Advice应用到约定的范围(即Pointcut指定的)上去 --> <aop:advisoradvice-ref="tx"pointcut-ref="aid"/> </aop:config>
</beans>
|
Hibernate核心配置文件
hibernate.cfg.xml |
<?xmlversion='1.0'encoding='UTF-8'?> <!DOCTYPEhibernate-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> <propertyname="dialect"> org.hibernate.dialect.SQLServerDialect </property> <propertyname="connection.url"> jdbc:sqlserver://localhost:1433;databaseName=ibm001 </property> <propertyname="connection.username">sa</property> <propertyname="connection.password">123</property> <propertyname="connection.driver_class"> com.microsoft.sqlserver.jdbc.SQLServerDriver </property> <propertyname="myeclipse.connection.profile">sqlconn</property> <propertyname="format_sql">true</property> <propertyname="show_sql">true</property>
<!-- <property name="hibernate.current_session_context_class">thread</property> -->
<mappingresource="com/ibm/pojo/Stu.hbm.xml"/> <mappingresource="com/ibm/pojo/Logtb.hbm.xml"/>
</session-factory>
</hibernate-configuration> |
Hibernate节点配置文件(一)
Stu.hbm.xml |
<?xmlversion="1.0"encoding="utf-8"?> <!DOCTYPEhibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <classname="com.ibm.pojo.Stu"table="stu"schema="dbo"catalog="ibm001"> <idname="userid"type="java.lang.Integer"> <columnname="userid"/> <generatorclass="native"></generator> </id> <propertyname="username"type="java.lang.String"> <columnname="username"length="32"/> </property> <propertyname="pwd"type="java.lang.String"> <columnname="pwd"length="32"/> </property> </class> </hibernate-mapping> |
JAVA代码部分
Javabean
Stu.java |
package com.ibm.pojo;
/** * Stu entity. @author MyEclipse Persistence Tools */
public class Stu implements java.io.Serializable {
// Fields
private Integeruserid; private Stringusername; private Stringpwd;
// Constructors
/** default constructor */ public Stu() { }
/** full constructor */ public Stu(String username, String pwd) { this.username = username; this.pwd = pwd; }
// Property accessors
public Integer getUserid() { return this.userid; }
public void setUserid(Integer userid) { this.userid = userid; }
public String getUsername() { return this.username; }
public void setUsername(String username) { this.username = username; }
public String getPwd() { return this.pwd; }
public void setPwd(String pwd) { this.pwd = pwd; }
} |
从页面来的数据传到UserManagerAction,对象stu得到属性值
UserManagerAction.java |
package com.ibm.action;
import com.ibm.pojo.Stu; import com.ibm.service.IUserOperService;
public class UserManagerAction { //定义一个stu对象 private Stustu; //定义一个service层的接口 private IUserOperServiceservice;
public String execute() { //调用service层 service.addUser(stu); System.out.println(stu.getUsername()); return"success"; }
public Stu getStu() { returnstu; } //set方法为stu对象设置值 public void setStu(Stu stu) { this.stu = stu; } public IUserOperService getService() { returnservice; } //action层通过Set方法注入接口IUserOperService public void setService(IUserOperService service) { this.service = service; } } |
//定义service接口
IuserOperService.java |
package com.ibm.service;
import com.ibm.pojo.Stu;
public interface IUserOperService {
public void addUser(Stu stu); } |
//接口service的实现类
UserOperService.java |
package com.ibm.service.iservice;
import com.ibm.dao.IUserOperDAO; import com.ibm.pojo.Stu; import com.ibm.service.IUserOperService;
public class UserOperService implements IUserOperService{ //定义用户接口userOperDAO IUserOperDAO userOperDAO;
public void addUser(Stu stu) { // TODO Auto-generated method stub //调用接口中实现类的add方法 userOperDAO.add(stu); }
//service层通过Set方法注入接口userOperDAO public void setUserOperDAO(IUserOperDAO userOperDAO) { this.userOperDAO = userOperDAO; } } |
定义功能接口IUserOperDAO
IUserOperDAO.java |
package com.ibm.dao;
import com.ibm.pojo.Stu;
public interface IUserOperDAO { public void add(Stu stu); } |
功能接口IuserOperDAO实现类
UserOperDAO.java |
package com.ibm.dao.idao;
import org.hibernate.Session; import org.hibernate.Transaction; import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.ibm.dao.ILogOperDAO; import com.ibm.dao.IUserOperDAO; import com.ibm.factory.HibernateSessionFactory; import com.ibm.pojo.Logtb; import com.ibm.pojo.Stu;
/** * 编程式事务演示 * @author Administrator * */ //public class UserOperDAO extends HibernateDaoSupport implements IUserOperDAO{ // // public void add(Stu stu) { //// Session session=null; //// Transaction tran=null; //// //// try { //// session=HibernateSessionFactory.getSessionFactory().getCurrentSession(); //// //// tran =session.beginTransaction(); //// //// session.save(stu); //// //// ILogOperDAO logDAO=new LogOperDAO(); //// Logtb logTb=new Logtb(); //// logTb.setActionname("增加用户"); //// logTb.setPersonid("U001"); //// logDAO.addLog(logTb); //// tran.commit(); //// //// } catch (Exception e) { //// e.printStackTrace(); //// tran.rollback(); //// } // } // //} //继承Spring中的HibernateDaoSupport类,并实现功能接口IUserOperDAO public class UserOperDAO extends HibernateDaoSupportimplements IUserOperDAO{
public void add(Stu stu){ //使用Spring自带的事务管理器来管理sessionFactory(配置文件在Spring中的applicationContext-tran.xml),得到getHibernateTemplate()方法,然后调用它的save方法保存数据 this.getHibernateTemplate().save(stu); } } |