s2sh整合

作者: l_sivan | 来源:发表于2017-03-13 22:30 被阅读91次

    一. 导包

    框架的版本情况:Struts2.3.4 + hibernate3.6.0+spring3.2.5
    有一些是非必需的,只不过为了功能的完整性,就全部都弄过去了,比如导出Word,Excel,还有文件上传,日志等。

    antlr-2.7.6.jar
    aopalliance.jar
    aspectjrt.jar
    aspectjweaver.jar
    c3p0-0.9.1.2.jar
    commons-codec-1.6.jar
    commons-collections-3.1.jar
    commons-fileupload-1.2.2.jar
    commons-httpclient-3.0.1.jar
    commons-io-2.0.1.jar
    commons-lang3-3.2.jar
    commons-logging-1.1.3.jar
    core-3.0.0.jar
    dom4j-1.6.1.jar
    freemarker-2.3.19.jar
    hibernate-jpa-2.0-api-1.0.0.Final.jar
    hibernate3.jar
    javassist-3.11.0.GA.jar
    jaxen-1.1-beta-6.jar
    json-lib-2.4-jdk15.jar
    jstl.jar
    jta-1.1.jar
    junit-4.12.jar
    log4j-1.2.17.jar
    mysql-connector-java-5.1.12-bin.jar
    ognl-3.0.5.jar
    poi-3.15.jar
    slf4j-api-1.6.1.jar
    spring-aop-3.2.5.RELEASE.jar
    spring-beans-3.2.5.RELEASE.jar
    spring-context-3.2.5.RELEASE.jar
    spring-core-3.2.5.RELEASE.jar
    spring-expression-3.2.5.RELEASE.jar
    spring-jdbc-3.2.5.RELEASE.jar
    spring-orm-3.2.5.RELEASE.jar
    spring-tx-3.2.5.RELEASE.jar
    spring-web-3.2.5.RELEASE.jar
    standard.jar
    struts2-core-2.3.4.1.jar
    struts2-spring-plugin-2.3.4.1.jar
    xwork-core-2.3.4.1.jar

    二. 配置web.xml

    <code>
    <?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">

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

    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:bean.xml</param-value>
    </context-param>
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    </web-app>
    </code>

    三.写applicationContext.xml

    <code>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssh_demo"></property>
    <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
    <property name="user" value="root"></property>
    <property name="password" value="root"></property>
    <property name="initialPoolSize" value="3"></property>
    <property name="maxPoolSize" value="6"></property>
    </bean>


    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
    </props>
    </property>
    <property name="mappingLocations">
    <list>
    <value>classpath:com/ycTime/entity/.hbm.xml</value>
    </list>
    </property>
    </bean>

    <context:component-scan base-package="com.ycTime.action"> </context:component-scan>
    <context:component-scan base-package="com.ycTime.service"></context:component-scan>
    <context:component-scan base-package="com.ycTime.dao"></context:component-scan>


    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
    <tx:method name="get
    " read-only="true"/>
    <tx:method name="find" read-only="true"/>
    <tx:method name="
    " read-only="false"/>
    </tx:attributes>
    </tx:advice>

    <aop:config>
    <aop:pointcut expression="execution(* com.ycTime.service.impl..(..))" id="pt"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
    </aop:config>
    </code>

    四.写entity

    • Notice.java
      <code>
      public class Notice {
      private int id;
      private String title;
      private String content;
      private Date doTime;
      private int status;// 0表示是草稿,1表示是已经发布
      public int getStatus() {
      return status;
      }
      public void setStatus(int status) {
      this.status = status;
      }
      public int getId() {
      return id;
      }
      public void setId(int id) {
      this.id = id;
      }
      public String getTitle() {
      return title;
      }
      public void setTitle(String title) {
      this.title = title;
      }
      public String getContent() {
      return content;
      }
      public void setContent(String content) {
      this.content = content;
      }
      public Date getDoTime() {
      return doTime;
      }
      public void setDoTime(Date doTime) {
      this.doTime = doTime;
      }
      }
      </code>

    • Notice.hbm.xml
      <code>
      <?xml version="1.0" encoding="UTF-8" ?>
      <!DOCTYPE hibernate-mapping PUBLIC
      "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
      "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
      <hibernate-mapping package="com.ycTime.entity">
      <class name="Notice" table="usad_Notice">
      <id name="id" column="id">
      <generator class="native"></generator>
      </id>
      <property name="title" column="title"></property>
      <property name="content" column="content"></property>
      <property name="doTime" column="doTime"></property>
      <property name="status" column="status"></property>
      </class>
      </hibernate-mapping>
      </code>

    五. 写Dao

    其中,@Repository是spring注解,上文中applicationContext.xml的扫描那部分,就是和这里配合的,配置了扫描之后,spring在加载applicationContext.xml的时候就会自动扫描带注解的类并在spring容器中创建相关的实例,以及完成依赖注入。@Repository是持久层注解,@Autowired是自动注入,NoticeDao就注入了SessionFactory。
    <code>
    @Repository
    public class NoticeDao {
    @Autowired
    private SessionFactory sessionFactory;
    public boolean saveNotice(Notice notice){
    Session session = sessionFactory.openSession();
    Transaction transaction = session.beginTransaction();
    try {
    transaction.begin();
    session.saveOrUpdate(notice);
    transaction.commit();
    } catch (Exception e) {
    transaction.rollback();
    return false;
    }finally {
    session.close();
    }
    return true;
    }
    }
    </code>

    六.写Service

    也是用了注解,类似
    <code>
    @Service
    public class NoticeService {
    @Autowired
    private NoticeDao noticeDao;
    public boolean saveNotice(String title, String content, Date doTime) {
    Notice notice = new Notice();
    notice.setContent(content);
    notice.setDoTime(doTime);
    notice.setTitle(title);
    notice.setStatus(0);
    boolean b = noticeDao.saveNotice(notice);
    return b;
    }
    }
    </code>

    七. 写action

    <code>
    @Controller
    public class NoticeAction {
    @Autowired
    private NoticeService noticeService;
    public String addNotice(){
    HttpServletRequest request = ServletActionContext.getRequest();
    String title = request.getParameter("title");
    String content = request.getParameter("content");
    boolean b = noticeService.saveNotice(title,content,new Date());
    if(b == true){
    return "success";
    }
    reutrn "error";
    }
    }
    </code>

    八. 写Struts配置文件

    Struts.xml:

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
    <struts>
    <constant name="struts.i18n.encoding" value="UTF-8" />
    <constant name="struts.configuration.xml.reload" value="true"/>
    <constant name="struts.action.extension" value="action,,"></constant>
    <include file="com/ycTime/config/*Struts.xml"></include>
    </struts>

    控制跳转的struts配置文件 NoticeStruts.xml:
    <code><?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
    <struts>
    <package name="noticeAction" namespace="/" extends="struts-default">
    <action name="addNotice" class="com.ycTime.action.NoticeAction" method="addNotice">
    <result name="success">success.jsp</result>
    <result name="error">error.jsp</result>
    </action>
    </package>
    </struts></code>

    九.写jsp页面

    <code><form action="${pageContext.request.contextPath }/addNotice.action">
    标题:<input name="title" type="text">

    内容:<input name="content" type="text">

    <input type="submit">
    </form>
    </code>

    至此,ssh整合完成。

    相关文章

      网友评论

        本文标题:s2sh整合

        本文链接:https://www.haomeiwen.com/subject/xgvqnttx.html