eclipse 下搭建 ssh 开发环境

一、Struts2Spring整合,创建一个OA工程
1、整合struts2
1)导入strut2jar:commons-logging-1.0.4.jar,freemarker-2.3.8.jar,ognl-2.6.11.jar,struts2-core-2.0.12.jar,xwork-2.06.jar。暂时导入这些jar包,到时候需要再倒入。
2)struts.xml文件放置在OA工程的src目录下。
3)web.xml里面配置struts2用到的核心过滤器。

Xml代码 

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

2、整合spring
1)导入springjar:spring.jar
2)
applicationContext.xml文件放置在OA工程的WEB-INF目录下。
3)web.xml里面配置spring用到的监听器。

Xml代码 

<listener> 

   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 

</listener> 


4)添加struts2-spring整合的插件:struts2-spring-plugin-2.0.12.jar,如果不使用这个插件,则需要在struts.xml里面配置:

Xml代码 

<constant name="struts.objectFactory"value="org.apache.struts2.spring.StrutsSpringObjectFactory"/> 


3、测试struts2spring整合对不对?
写一个jsp页面login.jsp来测试:

Html代码 

<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%> 

<%@ taglib prefix="s" uri="/struts-tags"%> 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01Transitional//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> 

</head> 

<body> 

    <s:formaction="Login" method="post"> 

        <s:textfieldname="userName"label="userName"></s:textfield> 

        <s:passwordname="password"label="password"></s:password> 

        <s:submitlabel="submit"></s:submit> 

    </s:form> 

</body> 

</html> 

写一个LoginAction

Java代码 

package com.struts2.action; 

 

import com.opensymphony.xwork2.ActionSupport; 

import com.struts2.service.LoginService; 

public class LoginAction extends ActionSupport { 

    private LoginServiceloginService; 

    private StringuserName; 

    private Stringpassword; 

    public voidsetLoginService(LoginService loginService) { 

        this.loginService =loginService; 

    } 

    public StringgetUserName() { 

        return userName; 

    } 

    public voidsetUserName(String userName) { 

        this.userName =userName; 

    } 

    public StringgetPassword() { 

        return password; 

    } 

    public voidsetPassword(String password) { 

        this.password =password; 

    } 

    @Override 

    public String execute()throws Exception { 

       if(loginService.isLogin(userName, password)) 

            return SUCCESS; 

        else 

            return INPUT; 

    } 

写一个Service

Java代码 

package com.struts2.service; 

 

public interface LoginService { 

    boolean isLogin(StringuserName,String password); 


写一个Service的实现

Java代码 

package com.struts2.service.impl; 

 

import com.struts2.service.LoginService; 

public class LoginServiceImpl implements LoginService { 

 

    public booleanisLogin(String userName, String password) { 

       if("hello".equals(userName) &&"world".equals(password)) 

            return true; 

        else  

            return false; 

    } 

struts.xml里面配置:

Xml代码 

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

    <actionname="Login" class="loginAction"> 

        <resultname="success">/result.jsp</result> 

        <resultname="input">/login.jsp</result> 

    </action> 

</package> 

applicationContext.xml里面配置:

Xml代码 

<bean id="loginService"class="com.struts2.service.impl.LoginServiceImpl"></bean> 

<bean id="loginAction" class="com.struts2.action.LoginAction"scope="prototype"> 

    <propertyname="loginService"ref="loginService"></property> 

</bean> 

启动tomcat,分别输入helloworld,提交,结果为helloworld说明struts2spring整合成功。

 

 

Struts+Spring整合实现,接下来继续整合Hibernate

前面已经讲述了Struts2+Spring整合,这里我们接着整合Hibernate
整合Hibernate
1)
导入Hibernate3.1jar:antlr-2.7.6.jar,commons-collections-3.1.jar,dom4j-1.6.1.jar,javassist-3.4.GA.jar,jta-1.1.jar,hibernate3.jar,slf4j-api-1.5.6.jar,slf4j-log4j12-1.5.6.jar,log4j-1.2.13.jar。暂时导入这些jar包,到时候需要再倒入。
2)spring的配置文件applicationContext-*.xml文件放置在OA工程的src目录下,这里我们有三个:
applicationContext-action.xml,applicationContext-beans.xml,applicationContext-common.xml
3)
web.xml里面配置

Xml代码 

<!-- when application server started,loading theapplicationContext-*.xml --> 

    <context-param> 

       <param-name>contextConfigLoaction</param-name> 

       <param-value>classpath*:applicationContext-*.xml</param-value> 

   </context-param> 

     

    <!-- integerate spring--> 

    <listener> 

   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 

    </listener> 

     

    <!-- let spring managehibernate's session,that we can focus bussiness layer --> 

    <filter> 

       <filter-name>hibernateFilter</filter-name> 

        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> 

    </filter> 

   <filter-mapping> 

       <filter-name>hibernateFilter</filter-name> 

       <url-pattern>*.action</url-pattern> 

   </filter-mapping> 

     

     <!-- configratestruts2 core filter --> 

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

     

    <!-- configurate webcharacter encoding --> 

    <filter> 

       <filter-name>encodingFilter</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>encodingFilter</filter-name> 

       <url-pattern>/*</url-pattern> 

   </filter-mapping> 


4)spring里面有声明式事务,它对hibernate做了封装,在applicationContext-common.xml里面配置spring的声明式事务:

Xml代码 

<!-- configure sessionFactory --> 

    <beanid="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 

        <propertyname="configLocation"> 

           <value>classpath:hibernate.cfg.xml</value> 

        </property> 

    </bean> 

     

    <!-- configuretransaction manager--> 

    <beanid="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 

        <propertyname="sessionFactory"> 

            <refbean="sessionFactory"/> 

        </property> 

    </bean> 

     

    <!-- configure transaction'spropagational feature --> 

    <tx:adviceid="txAdvice"transaction-manager="transactionManager"> 

       <tx:attributes> 

            <tx:methodname="add*" propagation="REQUIRED"/> 

            <tx:methodname="delete*" propagation="REQUIRED"/> 

            <tx:methodname="modify*" propagation="REQUIRED"/> 

            <tx:methodname="*" read-only="true"/> 

       </tx:attributes> 

    </tx:advice> 

     

    <!-- configure whichclass's which method take part in transaction --> 

    <aop:config> 

        <aop:pointcutid="allManagerMethod" expression="execution(*com.struts2.server.*.*(..))"/> 

        <aop:advisorpointcut-ref="allManagerMethod"advice-ref="txAdvice"/> 

    </aop:config> 

用到了springaop,所以在applicationContext.xml配置文件中引入spring-aop.xsd。具体如下:

 

<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:tx="http://www.springframework.org/schema/tx"

   xsi:schemaLocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

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

       http://www.springframework.org/schema/tx/spring-tx.xsd

       http://www.springframework.org/schema/aop                       http://www.springframework.org/schema/aop/spring-aop.xsd">

5)
测试Struts2+Spring+Hibernate整合是否成功
第一步:测试Spring+Hibernate整合是否成功
写一个需要映射的值对象Person

Java代码 

package com.oa.model; 

/**

 * @author lukuijun@hanqinet.com

 * @hibernate.classtalbe="t_person"

 */ 

public class Person { 

    /**

     * @hibernate.idgenerator-class="native" 

     */ 

    private int id; 

    /**

     * @hibernate.property

     */ 

    private String name; 

    /**

     * @hibernate.property

     */ 

    private String sex; 

    /**

     * @hibernate.property

     */ 

    private Stringaddress; 

    /**

     * @hibernate.property

     */ 

    private String duty; 

    /**

     * @hibernate.property

     */ 

    private String phone; 

    /**

     * @hibernate.property

     */ 

    private Stringdescription; 

    public int getId() { 

        return id; 

    } 

    public void setId(int id){ 

        this.id = id; 

    } 

    public String getName(){ 

        return name; 

    } 

    public void setName(Stringname) { 

        this.name = name; 

    } 

    public String getSex(){ 

        return sex; 

    } 

    public void setSex(Stringsex) { 

        this.sex = sex; 

    } 

    public String getAddress(){ 

        return address; 

    } 

    public voidsetAddress(String address) { 

        this.address =address; 

    } 

    public String getDuty(){ 

        return duty; 

    } 

    public void setDuty(Stringduty) { 

        this.duty = duty; 

    } 

    public String getPhone(){ 

        return phone; 

    } 

    public voidsetPhone(String phone) { 

        this.phone =phone; 

    } 

    public StringgetDescription() { 

        returndescription; 

    } 

    public voidsetDescription(String description) { 

        this.description =description; 

    } 

运行ant生成Person.hbm.xml映射文件:

Xml代码 

<?xml version="1.0"encoding="UTF-8"?> 

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/HibernateMapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 

 

<hibernate-mapping> 

  <classtable="t_person" name="com.oa.model.Person"> 

    <idaccess="field" name="id"> 

      <generatorclass="native"/> 

    </id> 

    <property name="name"access="field"/> 

    <propertyname="sex" access="field"/> 

    <propertyname="address" access="field"/> 

    <propertyname="duty" access="field"/> 

    <propertyname="phone" access="field"/> 

    <propertyname="description" access="field"/> 

  </class> 

</hibernate-mapping> 

运行ant生成hiberante.cfg.xml文件:

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> 

    <propertyname="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 

    <propertyname="hibernate.connection.url">jdbc:mysql://localhost:3306/oa</property> 

    <propertyname="hibernate.connection.username">root</property> 

    <propertyname="hibernate.connection.password">hanqinet</property> 

    <propertyname="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 

    <property name="hibernate.show_sql">true</property> 

    <propertyname="hibernate.hbm2ddl.auto">update</property> 

    <mappingresource="com/oa/model/Person.hbm.xml"/> 

 </session-factory> 

</hibernate-configuration> 

接着启动Tomcat,启动完毕检查数据库里面新增加了表t_personSpring2+Hibernate整合成功
第二步:测试Struts2+Spring整合是否成功
参考一、Struts2+Spring整合,测试成功
完成这个两步成功,说明我们的框架整合成功!

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值