现在的位置: 首页 > 综合 > 正文

SSH整合示例

2013年04月10日 ⁄ 综合 ⁄ 共 5902字 ⁄ 字号 评论关闭

1.导入struts,spring,hibernate所需的相应包文件

2.web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<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">
  <display-name>SSH</display-name>
  <!--Struts所需的配置 -->
  <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>
   <filter>
    <filter-name>struts-cleanup</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts-cleanup</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
    <!--Struts整合Spring所需的配置 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  </listener>
</web-app>

3.在src中新建struts.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<!-- struts2委托spring管理 -->
	<constant name="struts.objectFactory" value="spring" />
	<!-- struts2的包配置 -->
	<package name="demo_action" extends="struts-default">
		<action name="userDetail" class="userDetailBean" method="get">
			<result >/userDetail.jsp</result>
		</action>
	</package>
</struts>

4.在WEB-INF中添加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"
	xsi:schemaLocation="

http://www.springframework.org/schema/beans

    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd ">
    <!--采用单例形式配置Hibernate的SessionFactory,以classpath形式调用hibernate的配置文件 -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation">
			<value>classpath:hibernate.cfg.xml</value>
		</property>
	</bean>
	<!-- 采用单例形式配置Hibernate的transactionManager -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
	<!-- userDao的实现者 -->
	<bean id="userDao" class="dao.hibernate.UserDaoHibernate">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
	<!-- Id与struts.xml配置的class相同,此处的Class为真实的Class -->
	<bean id="userDetailBean" class="action.UserAction" scope="prototype">
		<property name="userDao">
			<ref bean="userDao" />
		</property>
	</bean>
</beans>

5.userDao类

package dao;
import java.util.List;
import entites.Userinfo;

public interface UserDao {
	public abstract Userinfo get(int userId);
}

6.userDao接口实现者

package dao.hibernate;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import dao.UserDao;
import entites.Userinfo;

public class UserDaoHibernate extends HibernateDaoSupport implements UserDao {
	@Override
	public Userinfo get(int userId) {
		return (Userinfo) getHibernateTemplate().get(Userinfo.class, userId);
	}
}

7.Struts的Action类UserAction

package action;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import dao.UserDao;
import entites.Userinfo;

public class UserAction extends ActionSupport {
	private UserDao userDao;

	public void setUserDao(UserDao dao) {
		userDao = dao;
	}
	public String get()
	{
		Userinfo userinfo=userDao.get(40);
		setUserInfo(userinfo);
		return super.SUCCESS;
	}
}

8.视图文件userDetail.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>Insert title here</title>
<style type="text/css">
table {
	border: 1px solid black;
	border-collapse: collapse;
}

table tbody tr td {
	border: 1px solid black;
	padding: 3px;
}
</style>
</head>
<body>
UserDetail
<table>
<tr><td>UserName:</td><td>${requestScope.UserInfo.userName}</td></tr>
<tr><td>PassWord:</td><td>${requestScope.UserInfo.password}</td></tr>
</table>
</body>
</html>

9.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">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">111</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/javadb</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <mapping resource="entites/Userinfo.hbm.xml" />
    </session-factory>
</hibernate-configuration>

10.Userinfo.hbm.xml文件

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2011-1-7 15:19:38 by Hibernate Tools 3.2.4.GA -->
<hibernate-mapping>
    <class name="entites.Userinfo" table="userinfo" catalog="javadb">
        <id name="userId" type="java.lang.Integer">
            <column name="UserId" />
            <generator class="identity" />
        </id>
        <property name="userName" type="string">
            <column name="UserName" length="30" />
        </property>
        <property name="password" type="string">
            <column name="Password" length="100" />
        </property>
        <property name="height" type="java.lang.Integer">
            <column name="Height" />
        </property>
        <property name="birth" type="date">
            <column name="Birth" length="10" />
        </property>
        <property name="ip" type="string">
            <column name="Ip" length="20">
                <comment>last ip address</comment>
            </column>
        </property>
    </class>
</hibernate-mapping>

 

抱歉!评论已关闭.