---
title: "struts2+spring3+hibernate3整合"
author: "X_boy"
date: "2011-10-04T00:56:50Z"
tags: ["SSH", "struts2+spring3+hibernate3整合"]
categories: ["编程语言"]
slug: "struts2-spring3-hibernate3"
draft: false
lastmod: "2011-10-04T01:02:31Z"
id: 1185078
url: "https://x-boy.iteye.com/blog/1185078"
---

网上看了很多例子，ssh整合都没有往数据库中插入记录，比如像注册这样的例子。刚接触ssh整合，写得不好。我用的是eclipse for j2ee，先上图看看整体架构

 

![ssh](http://dl.iteye.com/upload/attachment/564069/54425793-84ff-3729-84d8-81e623546842.jpg)

 

1、LoginAction.java

 

 

package action;

 

import impl.MyServiceImpl;

 

import com.opensymphony.xwork2.Action;

 

import model.User;

 

public class LoginAction implements Action{

private User user;

private MyServiceImpl ms;

private String tip;

public User getUser() {

return user;

}

public void setUser(User user) {

this.user = user;

}

public MyServiceImpl getMs() {

return ms;

}

public void setMs(MyServiceImpl ms) {

this.ms = ms;

}

public String getTip() {

return tip;

}

public void setTip(String tip) {

this.tip = tip;

}

public String execute()throws Exception

    {      

  ms.regedit(user);

            if (ms.valid(user.getUsername(),user.getPassword()))

            {

                 setTip("呵，整合成功!");

                return SUCCESS;

             }

            else

                return ERROR;

 

     }

 

}

2、MyService.java

package impl;

public interface MyService {

public boolean valid(String username , String password);

}

3、MyServiceImpl.java

package impl;

import model.User;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.orm.hibernate3.HibernateTemplate;

public class MyServiceImpl  implements MyService{

//处理注册

public void regedit(User user){

ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");

HibernateTemplate ht=(HibernateTemplate)context.getBean("hibernateTemplate");

      

ht.save(user);

}

//处理验证

public boolean valid(String username , String password){

if (username.equals("hello") && password.equals("world"))

        {

            return true;

         }

        else

            return false;

    

}

}

4、User.java

package model;

public class User {

private Integer id;

private String username;

private String password;

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

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;

}

}

5、User.hbm.xml

&lt;?xml version="1.0"?&gt;

&lt;!DOCTYPE hibernate-mapping PUBLIC

        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"&gt;

&lt;!-- Used to demonstrate the declarative configuration of both hbm files 

and annotated classes See hibernate.cfg.xml and ConfigurationTest --&gt;

&lt;hibernate-mapping package="model"&gt;

&lt;class name="User" table="user\_table"&gt;

&lt;id name="id"&gt;

&lt;generator class="identity" /&gt;

&lt;/id&gt;

&lt;property name="username" /&gt;

&lt;property name="password" /&gt;

&lt;/class&gt;

&lt;/hibernate-mapping&gt;

6、bean.xml

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" 

"http://www.springframework.org/dtd/spring-beans-2.0.dtd"&gt;

&lt;beans&gt;

&lt;bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"

destroy-method="close"&gt;

&lt;property name="driverClass" value="com.mysql.jdbc.Driver" /&gt;

&lt;property name="jdbcUrl" value="jdbc:mysql://localhost:3306/hibernate?useUnicode=true&amp;characterEncoding=UTF-8" /&gt;  

&lt;property name="user" value="root" /&gt;

&lt;property name="password" value="mysql" /&gt;

&lt;property name="maxPoolSize" value="40" /&gt;

&lt;property name="minPoolSize" value="1" /&gt;

&lt;property name="initialPoolSize" value="1" /&gt;

&lt;property name="maxIdleTime" value="20" /&gt;

&lt;/bean&gt;

&lt;bean id="sessionFactory"

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;

&lt;property name="dataSource" ref="dataSource" /&gt;

&lt;property name="mappingResources"&gt;

&lt;list&gt;

&lt;value&gt;model/User.hbm.xml&lt;/value&gt;

&lt;/list&gt;

&lt;/property&gt;

&lt;property name="hibernateProperties"&gt;

&lt;value&gt;

hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect

hibernate.hbm2ddl.auto=update

hibernate.shou\_sql=true

hibernate.format\_sql=true

&lt;/value&gt;

&lt;/property&gt;

&lt;/bean&gt;

&lt;bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"&gt;

&lt;property name="sessionFactory" ref="sessionFactory"/&gt;

&lt;/bean&gt;

&lt;/beans&gt;

7、struts.xml

&lt;?xml version="1.0" encoding="UTF-8" ?&gt;

&lt;!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    "http://struts.apache.org/dtds/struts-2.0.dtd"&gt;

&lt;struts&gt;

    &lt;package name="default" namespace="/" extends="struts-default"&gt;

        &lt;action name="login" class="action.LoginAction"&gt;

             &lt;result name="success"&gt;/success.jsp&lt;/result&gt;

             &lt;result name="error"&gt;/error.jsp&lt;/result&gt;

        &lt;/action&gt;

    &lt;/package&gt;

&lt;/struts&gt;

8、applicationContext.xml

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans 

       http://www.springframework.org/schema/beans/spring-beans.xsd"&gt;

             

&lt;bean id="ms" class="impl.MyServiceImpl" /&gt;

&lt;/beans&gt;

9、web.xml

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app\_2\_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app\_2\_5.xsd" id="WebApp\_ID" version="2.5"&gt;

  &lt;display-name&gt;ssh&lt;/display-name&gt;

  &lt;welcome-file-list&gt;

    &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;

  &lt;/welcome-file-list&gt;

  

  &lt;filter&gt;

        &lt;filter-name&gt;struts2&lt;/filter-name&gt;

        &lt;filter-class&gt;org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter&lt;/filter-class&gt;

    &lt;/filter&gt;

    &lt;filter-mapping&gt;

        &lt;filter-name&gt;struts2&lt;/filter-name&gt;

        &lt;url-pattern&gt;/\*&lt;/url-pattern&gt;

    &lt;/filter-mapping&gt;

    

    &lt;listener&gt;

    &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;

    &lt;/listener&gt;

&lt;/web-app&gt;

10、index.jsp

&lt;%@ page language="java" contentType="text/html; charset=UTF-8"%&gt;

&lt;%@ taglib prefix="s" uri="/struts-tags"%&gt;

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;

&lt;html&gt;

&lt;head&gt;

&lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;

&lt;title&gt;welcome login&lt;/title&gt;

&lt;/head&gt;

&lt;body&gt;

&lt;s:form action="login" method="post"&gt;

&lt;s:textfield name="user.username" label="用户名" /&gt;

&lt;s:textfield name="user.password" label="密码" /&gt;

&lt;s:submit value="登录" /&gt;

&lt;/s:form&gt;

&lt;/body&gt;

&lt;/html&gt;

11、success.jsp

&lt;%@ page language="java" contentType="text/html; charset=UTF-8"%&gt;

&lt;%@ taglib prefix="s" uri="/struts-tags"%&gt;

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;

&lt;html&gt;

&lt;head&gt;

&lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;

&lt;title&gt;success&lt;/title&gt;

&lt;/head&gt;

&lt;body&gt;

&lt;br&gt;

&lt;s:property value="tip"/&gt;&lt;br&gt;

&lt;s:property value="user.username"/&gt;

&lt;/body&gt;

&lt;/html&gt;

12、error.jsp

&lt;%@ page language="java" contentType="text/html; charset=UTF-8"%&gt;

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;

&lt;html&gt;

&lt;head&gt;

&lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;

&lt;title&gt;error&lt;/title&gt;

&lt;/head&gt;

&lt;body&gt;

哎~登录失败！！！

&lt;/body&gt;

&lt;/html&gt;