• 搭建Struts2.3.16,Hibernate4.3.1和Spring4.0.2环境


    1 下载包

    hibernate: http://hibernate.org/orm/downloads/

    struts2: http://struts.apache.org/download.cgi

    spring4: http://maven.springframework.org/release/org/springframework/spring/4.0.2.RELEASE/

    2 新建空Web Project  

     

    3 复制struts2常用包到WebRoot/WEB-INF/lib下

    4 修改web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" 
        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_3_0.xsd">
      <display-name></display-name>    
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
      
        <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>
    </web-app>

    在WEB-INF下新建jsp文件夹,用来保存jsp文件,也可以直接放在WebRoot下。放在WEB-INF下的好处是只有服务端可以访问。
    /jsp/Login.jsp:

    <%@ page contentType="text/html; charset=UTF-8" %>
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <html>
        <head>
            <title>Login</title>
        </head>
    
        <body>
            <s:form action="Auth" method="POST">
                <s:textfield key="name"/>
                <s:password key="pwd" />
                <s:submit/>
            </s:form>
        </body>
    </html>


    /jsp/Welcome.jsp

    <%@ page contentType="text/html; charset=UTF-8" %>
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <html>
        <head>
            <title>Welcome</title>
        </head>
    
        <body>
        <h2>Welcome!</h2>
            name: ${name} <br />
            pwd: ${pwd}
        </body>
    </html>

    5 在src目录下新建struts.xml文件

    struts.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
    
    <struts>
    
        <package name="default" namespace="/" extends="struts-default">
        
            <action name="Login" class="com.test.struts2.DemoAction" method="login">
                <result>/WEB-INF/jsp/Login.jsp</result>
            </action>
            <action name="Auth" class="com.test.struts2.DemoAction" method="auth">
                <result>/WEB-INF/jsp/Welcome.jsp</result>
            </action>
        </package>
    </struts>

    在src下新建DemoAction.java

    package com.test.struts2;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class DemoAction extends ActionSupport {
        
        private String name;
        private String pwd;
        
        public String execute() {
            return SUCCESS;
        }
        
        public String login() {
            return SUCCESS;
        }
        
        public String auth() {
            return SUCCESS;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getPwd() {
            return pwd;
        }
    
        public void setPwd(String pwd) {
            this.pwd = pwd;
        }
        
    }

    现在的工程结构图:


    struts就搭好了。访问http://localhost:8081/demo/Login查看结果。

    暂时没有做登录验证。只是显示输入的用户名和密码。

    4 复制hibernate常用包到/WebRoot/WEB-INF/lib下

    先把lib/required下面的包复制到lib中,然后下载mysql-connector-java-5.1.7-bin.jar 这个包加入。可能会下载到不同的版本,都无所谓。

    新建Account类:

    package com.test.model;
    
    public class Account {
        private Long id;
        private String name;
        private String pwd;
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getPwd() {
            return pwd;
        }
    
        public void setPwd(String pwd) {
            this.pwd = pwd;
        }
    
    }

    新建Account类和数据库表的映射文件Account.hbm.xml:

    <?xml version="1.0"?>
    <!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.test.model">
    
        <class name="Account" table="Account">
            <id name="id" column="Account_ID">
                <generator class="increment"/>
            </id>
            <property name="name" column="name"/>
            <property name="pwd" column="pwd"/>
        </class>
    
    </hibernate-mapping>

    在本地MySQL或者其他数据库新建数据库demo。新建hibernate配置文件:

    <?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    
    <hibernate-configuration>
        <session-factory>
    
            <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="connection.url">jdbc:mysql://localhost:3306/demo</property>
            <property name="connection.username">root</property>
            <property name="connection.password">root</property>
            <property name="connection.pool_size">5</property>
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
            <property name="show_sql">true</property>
            <property name="hbm2ddl.auto">create</property>
            <property name="current_session_context_class">thread</property>
    
            <mapping resource="com/test/model/Account.hbm.xml"/>
    
        </session-factory>
    
    </hibernate-configuration>


    根据自己的实际情况填写。现在只要本地可以连接数据库就OK了。新建IDao接口:

    package com.test.dao.impl;
    
    import java.util.List;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    
    import com.test.dao.IDao;
    
    
    public class IDaoImpl<T> implements IDao<T> {
    
        private static final SessionFactory sessionFactory;
        
        static {
            try {
                 sessionFactory = new Configuration()
                     .configure().buildSessionFactory();
            } catch (Throwable ex) {
                throw new ExceptionInInitializerError(ex);
            }
        }
        
        @Override
        public T get(String hql) {
            Session session = sessionFactory.openSession();
            try {
                session.beginTransaction();
                List list = session.createQuery(hql).list();
                if (list != null && list.size() > 0)
                    return (T) list.get(0);
                return null;
            } finally { 
                session.getTransaction().commit();
                session.close();
            }
        }
    
    }

    现在Hibernate已经和struts整合完成。实现了用户登录数据库验证。

    5 搭建Spring环境

    导入libs/下的*RELEASE.jar文件就可以了。下载commons-dbcp-1.4.jar,commons-pool-1.6.jar包。这两个包跟数据连接池有关。
    为了实现Service层和dao层的分离,新建Service层。

    Iservice.java:

    package com.test.service;
    
    public interface IService<T> {
        
        public T get(String hql); 
    }

    IServiceImpl.java

    package com.test.service.impl;
    
    import com.test.dao.IDao;
    import com.test.service.IService;
    
    public class IServiceImpl<T> implements IService<T> {
        
        private IDao<T> dao;
        
        public IDao<T> getDao() {
            return dao;
        }
    
        public void setDao(IDao<T> dao) {
            this.dao = dao;
        }
    
        @Override
        public T get(String hql) {
            // TODO Auto-generated method stub
            return dao.get(hql);
        }
        
    }

    为了使用spring,新建applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <?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:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx  
        http://www.springframework.org/schema/tx/spring-tx.xsd">
    
    
        <!-- 数据源配置 -->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
            destroy-method="close">
            <property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <property name="url" value="jdbc:mysql://localhost:3306/demo" />
            <property name="username" value="root" />
            <property name="password" value="root" />
            <property name="initialSize" value="5" />
            <property name="maxActive" value="500" />
        </bean>
    
        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
            destroy-method="destroy">
            <property name="dataSource" ref="dataSource" />
            <property name="mappingResources">
                <list>
                    <value>com/test/model/Account.hbm.xml</value>
                </list>
            </property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.format_sql">true</prop>
                </props>
            </property>
        </bean>
    
        <bean id="dao" class="com.test.dao.impl.IDaoImpl">
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>
    
        <!-- 在Service层配置事务 -->
        <bean id="hibernateTransactionManager"
            class="org.springframework.orm.hibernate4.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory" />
            <!-- <property name="dataSource" ref="dataSource" /> -->
        </bean>
    
        <bean id="hibernateTransactionAttributeSource"
            class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
            <property name="properties">
                <props>
                    <prop key="*">PROPAGATION_REQUIRED</prop>
                </props>
            </property>
        </bean>
    
        <bean id="service"
            class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
            <property name="transactionManager" ref="hibernateTransactionManager" />
            <property name="target">
                <bean class="com.test.service.impl.IServiceImpl">
                    <property name="dao" ref="dao" />
                </bean>
            </property>
            <property name="transactionAttributeSource" 
                ref="hibernateTransactionAttributeSource" />
        </bean>
        
        <!-- spring生成action -->
        <bean id="demoAction" scope="prototype" class="com.test.struts2.DemoAction">
            <property name="service" ref="service" />
        </bean>
    
    </beans>

    现在Hibernate.cfg.xml已经没用了,删掉这个文件。为了struts和spring结合,加入struts2-spring-plugin-2.3.16.jar包。

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" 
        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_3_0.xsd">
      <display-name></display-name>    
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
      
          <!-- spring配置 -->
          <context-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>
                  /WEB-INF/classes/applicationContext.xml
              </param-value>
          </context-param>
          
          <listener>
              <listener-class>
                  org.springframework.web.context.ContextLoaderListener
              </listener-class>
          </listener>
        
        <!-- struts配置 -->  
        <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>
    </web-app>

    struts.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
    
    <struts>
        
        <constant name="struts.devMode" value="true" />
        <constant name="struts.objectFactory" value="spring" />
        
        <package name="default" namespace="/" extends="struts-default">
            <action name="Login" class="com.test.struts2.DemoAction" method="login">
                <result>/WEB-INF/jsp/Login.jsp</result>
            </action>
            <action name="Auth" class="com.test.struts2.DemoAction" method="auth">
                <result>/WEB-INF/jsp/Welcome.jsp</result>
                <result name="error">/WEB-INF/jsp/error.jsp</result>
            </action>
        </package>
    </struts>

    DemoAction.java

    package com.test.struts2;
    
    import com.opensymphony.xwork2.ActionSupport;
    import com.test.model.Account;
    import com.test.service.IService;
    
    public class DemoAction extends ActionSupport {
        
        private IService service;
        
        private String name;
        private String pwd;
        
        public String execute() {
            return SUCCESS;
        }
        
        public String login() {
            return SUCCESS;
        }
        
        public String auth() {
            String hql = "from Account where name='" + name + "' and pwd='" + pwd + "'";
            Account account = (Account) service.get(hql);
            return account == null ? "error" : SUCCESS;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getPwd() {
            return pwd;
        }
    
        public void setPwd(String pwd) {
            this.pwd = pwd;
        }
    
        public IService getService() {
            return service;
        }
    
        public void setService(IService service) {
            this.service = service;
        }
        
    }


    现在ssh整合基本上完成了。还有一些没有描述的包可以在源码里面查看。

    百度网盘下载:http://pan.baidu.com/s/1dDDtYqH

  • 相关阅读:
    CSS基础——基本单位
    CSS基础——表格
    CSS基础——列表
    Single-Source Shortest Paths
    Maximum Flow
    Breadth-First Search
    Depth-First Search (III)
    Depth-First Search (II)
    Depth-First Search (I)
    Simple Complete Search
  • 原文地址:https://www.cnblogs.com/likeio/p/3584353.html
Copyright © 2020-2023  润新知