ssh框架总结

struts2+spring+hibernate=ssh框架

————————————————————————————————————————————————————————————————————————————

struts2 实现跳转页面时service里面要做的事情获取参数

Action类继承ActionSupport类(其中功能有 校验和国际化),默认方法为execute()return的返回值决定跳转到相对应的页面,在xml文件里的

过滤器

<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>/*</url-pattern>
</filter-mapping>

<struts>

<package name="mypck001" extends="struts-default">

<action name="Index" class="myIndexAction(这里是被注入的实例,就是Bean的id)" method="execute1(这个是使用Action里的方法,默认为execute)">

<result name="success(匹配返回值)">/WEB-INF/jsp/index2.jsp(决定跳转的页面)</result>

总结用的sturts.xml写

<struts><!-- 告知Struts2运行时使用Spring来创建对象 -->

<constant name="struts.objectFactory(与注入有关是个监听器)" value="spring" />

<include file="s001.xml" />

 ————————————————————————————————————————————————————————————————————————————

spring是解耦的一个框架,这个能让各个功能方法实现模块化,主要体现是规避出现new一个类

在web.xml里有

监听器

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

我们需要一个applicationContext.xml文件

————————————————这个是头准备

<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:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">

————————————————

<bean id="myIndexAction(这个是给上面)" class="ssh.action.IndexAction(这个是类)" scope="prototype(非单例就是可以多个同时使用)">
<property name="is(这个是IndexAction类里的is)" ref="myIndexService(把ssh.service.IndexServiceImpl()类注入‘is’,spring自动调用IndexAction类里的setIs()方法)"/>
</bean>

————————————————————————————————————————————————————————————————————————————

hibernate这个是与数据库关联的框架,把与数据库有关的操作都都丢给hibernate去做,连接数据库建表获取数据存入数据

<bean id="mySessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 注入连接池,包含了数据库用户名,密码等等信息 -->
<property name="dataSource" ref="myDataSource"/>
<!-- 配置Hibernate的其他的属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.connection.autocommit">false</prop>
<!-- 开机自动生成表 -->
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>ssh/entity/BookCard.hbm.xml</value>
</list>
</property>
</bean>

<!-- 引入外部属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>

(这个是text或者properties写入文件里面对应着

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/CardDB
jdbc.user=root
jdbc.password=123456

<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
<!-- 每300秒检查所有连接池中的空闲连接 -->
<property name="idleConnectionTestPeriod" value="300"></property>
<!-- 最大空闲时间,900秒内未使用则连接被丢弃。若为0则永不丢弃 -->
<property name="maxIdleTime" value="900"></property>
<!-- 最大连接数 -->
<property name="maxPoolSize" value="2"></property>
</bean>

--------------------------------------------------------------------------------------------------------------------------------------------------------

 struts2完成接收参数,把数据库里的数据存到数组里然后交给客户端jsp

原文地址:https://www.cnblogs.com/Sosowu/p/5842538.html