首页 > 程序开发 > 综合编程 > 安全编程 > 正文
安全编程之SSH框架整合
2016-09-09 11:40:38       个评论    来源:江军祥的博客  
收藏    我要投稿

ssh框架整合步骤如下

提示:myeclipse环境、工程环境、tomcat环境的jdk保持一致

1、新建一个工程,把工程的编码为utf-8

2、把jsp的编码形式改成utf-8

3、把jar包放入到lib下

4、建立三个src folder

src 存放源代码

config 存放配置文件

hibernate 存放hibernate的配置文件

spring 存放spring的配置文件

struts 存放struts的配置文件

struts.xml

test 存放单元测试

5、在src下建立包

cn.itcast.s2sh.domain

持久化类和映射文件

6、编写dao层和service层

7、写spring的配置文件

1、写sessionFactory

2、测试

3、写dao和service

4、测试

8、写action

9、写spring的配置文件

把action注入到spring容器中

<code class=" hljs d">      <bean id="personAction" class="cn.itcast.s2sh.struts2.action.sh.PersonAction" scope="prototype"></bean></code>

scope为”prototype”保证了action的多实例

10、在web.xml

加入spring的监听器

加入struts2的过滤器

11、请求

详细代码

持久化类与映射文件

<code class=" hljs xml"><!--?xml version="1.0" encoding="utf-8"?-->
 
<hibernate-mapping>
    <!-- 
        用来描述一个持久化类
        name  类的全名
        table 可以不写  默认值和类名一样 
        catalog  数据库的名称  一般不写
     -->
    <class name="cn.itcast.s2sh.domain.sh.Person">
        <!-- 
            标示属性  和数据库中的主键对应
            name  属性的名称
            column 列的名称
         -->
        <id name="pid" column="pid" length="200" type="java.lang.Long">
            <!-- 
                主键的产生器
                  就该告诉hibernate容器用什么样的方式产生主键
             -->
            <generator class="increment"></generator>
        </id>
        <!-- 
            描述一般属性
         -->
        <property name="pname" column="pname" length="20" type="string">
        </property>
 
        <property name="psex" column="psex" length="10" type="java.lang.String"></property>
    </class>
</hibernate-mapping></code>

dao

<code class=" hljs java">package cn.itcast.s2sh.sh.dao.impl;
 
import java.io.Serializable;
 
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
 
import cn.itcast.s2sh.domain.sh.Person;
import cn.itcast.s2sh.sh.dao.PersonDao;
 
public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao{
 
    @Override
    public void savePerson(Person person) {
        // TODO Auto-generated method stub
        this.getHibernateTemplate().save(person);
    }
 
    @Override
    public Person getPesonById(Serializable id) {
        // TODO Auto-generated method stub
        return (Person) this.getHibernateTemplate().load(Person.class, id);
    }
 
}</code>

service

<code class=" hljs java">package cn.itcast.s2sh.sh.service.impl;
 
import java.io.Serializable;
 
import cn.itcast.s2sh.domain.sh.Person;
import cn.itcast.s2sh.sh.dao.PersonDao;
import cn.itcast.s2sh.sh.service.PersonService;
 
public class PersonServiceImpl implements PersonService{
    private PersonDao personDao;
 
    public PersonDao getPersonDao() {
        return personDao;
    }
 
    public void setPersonDao(PersonDao personDao) {
        this.personDao = personDao;
    }
 
    @Override
    public void savePerson(Person person) {
        // TODO Auto-generated method stub
        this.personDao.savePerson(person);
    }
 
    @Override
    public Person getPersonByID(Serializable id) {
        // TODO Auto-generated method stub
        return this.personDao.getPesonById(id);
    }
}</code>

spring配置文件

applicationcontext.xml

<code class=" hljs 1c"><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 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/aop 
        http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    <import resource="applicationContext-db.xml">
    <import resource="applicationContext-person.xml">
</import></import></beans></code>

applicationContext-db.xml

<code class=" hljs xml"><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 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/aop 
        http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    <!--  
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation">
            <value>classpath:hibernate/hibernate.cfg.xml</value>
        </property>
    </bean>
    -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <value>classpath:jdbc.properties</value>
        </property>
    </bean>
 
 
    <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}">
        <property name="url" value="${jdbc.url}">
        <property name="username" value="${jdbc.username}">
        <property name="password" value="${jdbc.password}">
    </property></property></property></property></bean>
 
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource">
        </ref></property>
        <property name="mappingResources">
        <!-- list all the annotated PO classes -->
            <list>
                <value>
                    cn/itcast/s2sh/domain/sh/Person.hbm.xml
                </value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.MySQLDialect
                </prop>
                <prop key="hibernate.hbm2ddl.auto">
                    update
                </prop>
            <prop key="hibernate.show_sql">
                    true
                </prop>
                <prop key="hibernate.format_sql">
                    true
                </prop>
            </props>
        </property>
    </bean>
 
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="sessionFactory">
        </ref></property>
    </bean>
 
    <tx:advice id="tx" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*" read-only="false">
            <tx:method name="update*" read-only="false">
            <tx:method name="delete*" read-only="false">
            <!-- 
                * 代表了除了上述的三种情况的以外的情况
             -->
            <tx:method name="*" read-only="true">
        </tx:method></tx:method></tx:method></tx:method></tx:attributes>
    </tx:advice>
 
     
         
         
    </aop:advisor></aop:pointcut></aop:config>
</beans></code>

applicationContext-person.xml

<code class=" hljs applescript"><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 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/aop 
        http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    <bean id="personDao" class="cn.itcast.s2sh.sh.dao.impl.PersonDaoImpl">
        <property name="sessionFactory">
            <ref bean="sessionFactory">
        </ref></property>
    </bean>
 
    <bean id="personService" class="cn.itcast.s2sh.sh.service.impl.PersonServiceImpl">
        <property name="personDao">
            <ref bean="personDao">
        </ref></property>
    </bean>
 
    <bean id="personAction" class="cn.itcast.s2sh.struts2.action.sh.PersonAction" scope="prototype">
        <property name="personService">
            <ref bean="personService">
        </ref></property>
    </bean>
</beans></code>

struts.xml

<code class=" hljs xml"><!--?xml version="1.0" encoding="UTF-8"?-->
 
<struts>
    <constant name="struts.devMode" value="true">
    <include file="struts2/struts-person.xml"></include>
    <!--  <constant name="struts.objectFactory" value="spring" />-->
</constant></struts>   </code>

struts-person.xml

<code class=" hljs xml"><!--?xml version="1.0" encoding="UTF-8"?-->
 
<struts>
   <package name="person" namespace="/" extends="struts-default">
         
            <result name="index">index.jsp</result>
        </action>
   </package>
</struts>   </code>

web.xml文件的编写

<code class=" hljs 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">
    <!-- 整合Spring -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext.xml</param-value>
    </context-param>
 
     <filter>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <url-pattern>*.action</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>/*</url-pattern>
    </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app></code>

						
点击复制链接 与好友分享!回本站首页
上一篇:常见的三大加密算法
下一篇:无线局域网安全(一)———WEP加密
相关文章
图文推荐
文章
推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训
版权所有: 红黑联盟--致力于做实用的IT技术学习网站