您的位置:首页 > 其它

SSH框架

2016-04-05 00:00 561 查看
一,由SSH框架构建系统的基本业务流程:

1,在表示层中,首先通过JSP页面实现交互页面,负责传送请求(request)和接收响应(response),然后Struts根据配置文件(struts-config.xml)将ActionServlet接收到的request,委派给对应的Action进行处理。

2,业务层,管理服务组件的Spring IOC容器负责向Action提供业务模型,提供事务处理。

3,持久层,使用Hibernate对象化映射和数据库交互,处理DAO请求的数据,并返回处理结果。

二,

1,为什么要使用Spring?

Spring是一个轻量级的控制反转(IOC)和面向切面的容器框架,使开发人员使用最基本的JavaBean来完成以前只能用EJB来完成的事情,

2,为什么要使用Struts2?

Struts2以WebWork为核心,采用拦截器的机制来处理用户的请求,

3,为什么使用Hibernate?

Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。

SSH框架搭建

1,首先导入Spring3.0包(并选中Libraries其中有<AOP><Core><jdbc><j2ee><web>),会自动生成Srping的配置文件applicationContext.xml文件。

2,配置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>
<!-- 配置监听器 -->
<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>

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

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">
<!-- 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>

<mapping resource="org/panda/entity/Employee.hbm.xml" />
<mapping resource="org/panda/entity/Department.hbm.xml" />

</session-factory>

</hibernate-configuration

4,配置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.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.gjt.mm.mysql.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306//demo" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<!-- 生成sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<property name="dataSource" ref="dataSource"></property>
</bean>

5,导入Struts2.1包,并直接在web.xml配置文件中生成Filter

<!-- 配置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>

6,接下来配置struts.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>
<!-- 处理编码问题 -->
<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 name -->
<action name="addnewuser" class="action" method="addNewuser">
<result name="success">/index.jsp</result>
</action>

</package>
</struts>

至此,SSH框架就算搭建完毕了。。。。

还没有,最后还得要查看一下web.xml总的配置文件,查看一下是否配置监听器,去读取applicationContext.xml配置文件和过滤器struts2配置文件。下边就是最终的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>
<!-- 配置监听器 ,启动框架的时候,读取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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<!-- 利用的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>

<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>

这次,SSH框架真的就算搭建完成了。将项目部署到服务器,启动服务器,跑起来。。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: