您的位置:首页 > 其它

SSH框架

2016-04-22 00:00 183 查看
摘要: 使用SSH框架实现对人员表的增删改查

1,搭建SSH框架

①导入Spring3.0的包(AOP,JDBC,J2EE,WEB),会自动生成applicationContext.xml配置文件,并配置web.xml配置文件去监听spring的配置文件

<?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>
<!-- 配置监听器 ,启动框架的时候,读取Spring配置文件applicationContext.xml文件-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

②导入Hibernate3.3,并自动生成hibernate.cfg.xml配置文件,会自动在applicationContext.xml配置文件中生成sessionFactory。

<?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="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/demo</property>
<property name="connection.username">root</property>
<property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="myeclipse.connection.profile">hibernateConnection</property>

</session-factory>

</hibernate-configuration>

③配置Spring和Hibernate连接,在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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/demo" />
<property name="username" value="root" />
<property name="password" value="" />

</bean>
<!-- 配置sessionFactory,初始化Hibernate,充当数据源代理 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<!-- 配置模板 -->
<bean id="ht" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置事务管理器 -->
<bean id="hts" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!--启动对事务注解的支持 -->
<tx:annotation-driven transaction-manager="hts" />

<!-- 配置dao -->
<bean id="dao" class="org.panda.user.daoimpl.NewUserDaoImpl">
<property name="ht" ref="ht"></property>
</bean>
<!-- 配置biz -->
<bean id="biz" class="org.panda.user.bizimpl.NewUserBizImpl">
<property name="dao" ref="dao"></property>
</bean>
<!-- 配置action -->
<bean id="action" class="org.panda.user.action.NewUserAction">
<property name="biz" ref="biz"></property>
</bean>

</beans>

④导入struts2.1的包,并自动在web.xml中生成filter配置文件。

<!-- 配置过滤器
filter可以改变一个request和response,Filter不是一个servlet,不能产生response,
它能够在一个request到达serlvet之前进行预处理,也可以在离开servlet时处理response。
Filter用处:
1,在HttpServletRequest到达Servlet,拦截HttpServletRequest;
2,根据需要检查HttpServletRequest,也可以修改HttpServletRequest的头和数据
3,在HttpServetResponse到达客户端之前,拦截HttpServletResponse;
4,根据需要检查HttpServletResponse,也可以修改HttpServletResponse的头和数据;
-->
<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>

<!-- 利用的spring的过滤器来设置编码方式 -->
<filter>
<filter-name>Spring character encoding filter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Spring character encoding filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

⑤配置struts.xml配置文件。处理编码问题,讲Struts控制权给Spring,配置action。

<?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>
<!-- 处理编码问题 -->
<constant name="struts.i18n.encoding" value="utf-8"></constant>
<!-- 将Struts控制权给Spring -->
<constant name="struts.objFactory" value="spring"></constant>

<package name="default" extends="struts-default" namespace="/">
<!-- struts中class为applicationContext.xml中的bean id -->
<action name="NewUserAction" class="action" method="addUser">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: