• [Java web]Spring+Struts2+Hibernate整合过程


     摘要

    最近一直在折腾java web相关内容,这里就把最近学习的spring+struts2+hibernate进行一个整合,也就是大家经常说的ssh。

    环境

    工具IDE :Idea 2018  

    数据库:Mysql

    关于如何安装tomcat、mysql、idea,请自行查找。

    1、使用idea创建spring项目

    2、创建项目SSHDemo,项目结构如下

    3、添加三层的包。

    这里采用三层的架构进行项目的设计

    action存放struts2的action类

    dao 数据库操作

    service业务层

    test单元测试

    domain javabean

    4、配置struts2

    在web.xml中你会看到下面的红字提示,引起这个的原因是因为struts2 版本>=2.5 需要使用另外的方式进行配置

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
        <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>

    修改后的配置如下

       <filter>
            <filter-name>struts2</filter-name>
            <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
        </filter>

    其他内容不变

    在action包 添加一个UserAction

    package com.ssh.action;
    
    import com.opensymphony.xwork2.ActionSupport;
    import com.opensymphony.xwork2.ModelDriven;
    import com.ssh.domain.User;
    
    /**
     * user相关action
     */
    public class UserAction extends ActionSupport implements ModelDriven<User> {
    
        private User user = new User();
    
        @Override
        public User getModel() {
            return user;
        }
    
        public String login() {
            System.out.println(user);
            return NONE;
        }
    }

    配置struts.xml

    <struts>
        <package name="user" extends="struts-default" namespace="/">
            <action name="user_*" class="com.ssh.action.UserAction" method="{1}"/>
        </package>
    </struts>

    添加一个登录页面login.jsp测试

    <%--
      Created by IntelliJ IDEA.
      User: sunwy
      Date: 2018/6/6
      Time: 10:23
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>登录</title>
    </head>
    <body>
    
    <form action="${pageContext.request.contextPath}/user_login.action">
        用户名:<input type="text" name="name">
        密码:<input type="password" name="password" id="password">
        <input type="submit" value="登录">
    </form>
    </body>
    </html>

    启动

    在启动时发现如下内容

    严重: Context [] startup failed due to previous errors
    [2018-06-06 10:25:36,908] Artifact SSHDemo:war exploded: Error during artifact deployment. See server log for details.

    打开project structure修复出现的问题

    重启Tomcat发现如下错误

    六月 06, 2018 10:57:12 上午 org.apache.catalina.startup.TldConfig execute
    信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
    ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
    ERROR Dispatcher Dispatcher initialization failed
    java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at com.opensymphony.xwork2.inject.ContainerImpl$MethodInjector.inject(ContainerImpl.java:289)
    at com.opensymphony.xwork2.inject.ContainerImpl$ConstructorInjector.construct(ContainerImpl.java:422)
    at com.opensymphony.xwork2.inject.ContainerBuilder$5.create(ContainerBuilder.java:231)
    at com.opensymphony.xwork2.inject.Scope$2$1.create(Scope.java:52)
    at com.opensymphony.xwork2.inject.ContainerBuilder$3.create(ContainerBuilder.java:106)
    at com.opensymphony.xwork2.inject.ContainerBuilder$7.call(ContainerBuilder.java:584)
    at com.opensymphony.xwork2.inject.ContainerBuilder$7.call(ContainerBuilder.java:581)
    at com.opensymphony.xwork2.inject.ContainerImpl.callInContext(ContainerImpl.java:560)
    at com.opensymphony.xwork2.inject.ContainerBuilder.create(ContainerBuilder.java:581)
    at com.opensymphony.xwork2.config.impl.DefaultConfiguration.createBootstrapContainer(DefaultConfiguration.java:287)
    at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:162)
    at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:66)
    at org.apache.struts2.dispatcher.Dispatcher.getContainer(Dispatcher.java:957)
    at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:463)
    at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:496)
    at org.apache.struts2.dispatcher.InitOperations.initDispatcher(InitOperations.java:73)
    at org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:61)
    at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:285)
    at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:266)
    at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:108)
    at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4997)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5699)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:1017)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:993)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)
    at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1900)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
    at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:618)
    at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:565)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
    at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468)
    at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
    at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309)
    at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401)
    at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:357)
    at sun.rmi.transport.Transport$1.run(Transport.java:200)
    at sun.rmi.transport.Transport$1.run(Transport.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:573)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:835)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:688)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:687)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
    Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.opensymphony.xwork2.inject.ContainerImpl$MethodInjector.inject(ContainerImpl.java:287)
    ... 64 more
    Caused by: java.lang.ExceptionInInitializerError
    at com.opensymphony.xwork2.ognl.OgnlValueStackFactory.setContainer(OgnlValueStackFactory.java:88)
    ... 69 more
    Caused by: java.lang.IllegalArgumentException: Javassist library is missing in classpath! Please add missed dependency!
    at ognl.OgnlRuntime.<clinit>(OgnlRuntime.java:169)
    ... 70 more
    Caused by: java.lang.ClassNotFoundException: javassist.ClassPool
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1928)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1771)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at ognl.OgnlRuntime.<clinit>(OgnlRuntime.java:166)
    ... 70 more
    六月 06, 2018 10:57:12 上午 org.apache.catalina.core.StandardContext startInternal
    严重: One or more Filters failed to start. Full details will be found in the appropriate container log file
    六月 06, 2018 10:57:12 上午 org.apache.catalina.core.StandardContext startInternal
    严重: Context [] startup failed due to previous errors

    好吧,解决问题吧

    Caused by: java.lang.ClassNotFoundException: javassist.ClassPool

    这个问题,是由于没有引入javassist-3.11.0.GA.jar包引起的,那么找到该包,引入即可

    重新发布,输入用户名和密码

    HTTP Status 404 - There is no Action mapped for namespace [/] and action name [user_find] associated with context path [].

    注意

    在struts2.3之前的版本,正常的配置就可以了,但在struts2.3版本之后,使用通配符调用方法时,内部会验证是否允许访问该方法,所以要加上

    <allowed-methods>方法名1,方法名2…</allowed-methods>代码。

    参考:https://www.cnblogs.com/gsy52300/p/5778754.html

    最后struts.xml文件内容,这种配置也是一种安全设置,通配符,如果该action有其他不想被别人访问到的方法,如果通过通配符的方式,直接暴漏出来了,通过这种配置,可以配置允许访问哪些方法,对其它隐私的方法进行保护。

    <?xml version="1.0" encoding="UTF-8"?>
    
    <!DOCTYPE struts PUBLIC
            "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
            "http://struts.apache.org/dtds/struts-2.5.dtd">
    
    <struts>
        <package name="userAction" extends="struts-default" namespace="/">
            <action name="user_*" method="{1}" class="com.ssh.action.UserAction">
                <allowed-methods>login</allowed-methods>
            </action>
        </package>
    </struts>

    测试struts2

    后台action能收到数据

    好了,到这里,struts2 算是配置成功了,发现还是不容易的。

    整合spring

    在src目录下添加applicationContext.xml配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <!--将action的创建交给spring ioc 注意action是多例的 一定要将scope设置为prototype-->
        <bean class="com.ssh.action.UserAction" id="userAction" scope="prototype"/>
    
    </beans>

    我们希望在服务器启动的时候加载该配置文件,那么需要在web.xml添加如下内容

      <!--配置spring在启动服务器的是加载配置文件的监听器-->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <!--配置具体的配置文件的位置-->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </context-param>

    这里需要添加spring-web-3.2.0.RELEASE.jar包

    成功加载

    整合Hibernate

     添加jar包

    在src目录下添加hibernate配置文件hibernate.cfg.xml

    <?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>
        <!--配置sessionFactory-->
        <session-factory>
            <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="hibernate.connection.url">jdbc:mysql:///sshdemo</property>
            <property name="hibernate.connection.username">root</property>
            <property name="hibernate.connection.password">abcd</property>
            <!--配置数据库方言-->
            <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
            <!--可选配置-->
            <property name="hibernate.show_sql">true</property>
            <property name="hibernate.format_sql">true</property>
            <!--是否根据javabean自动生成表结构-->
            <property name="hibernate.hbm2ddl.auto">update</property>
            <!--连接池-->
            <!--提供商-->
            <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
            <!--连接池中最小的可用连接-->
            <property name="c3p0.min_size">5</property>
            <!--最大连接-->
            <property name="c3p0.max_size">20</property>
            <!--数据库连接的过期时间 秒 如果数据库连接池中的某个连接处于空闲时间超过了timeout,则从连接池中清除-->
            <property name="c3p0.timeout">120</property>
            <!--每3000s检查空间连接-->
            <property name="c3p0.idle_test_period">3000</property
    <!--配置映射-->
    <mapping resource="com/ssh/domain/User.hbm.xml"/>
        </session-factory>
    </hibernate-configuration>

    在domain包下添加映射配置User.hbm.xml

    <?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>
        <class name="com.ssh.domain.User" table="users">
            <id name="id" column="id">
                <generator class="native"></generator>
            </id>
            <property name="name" column="name"></property>
            <property name="password" column="password"></property>
        </class>
    </hibernate-mapping>

    在applicationContext.xml中添加如下配置

    <?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: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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
    
        <!--加载sessionFactory配置-->
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
        </bean>
    
        <!-- 注入事务管理器,提交事务和回滚事务 -->
        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"/>
        </bean>
        <!--开启事务注解方式-->
        <tx:annotation-driven transaction-manager="transactionManager"/>
        <!--配置userDao-->
        <bean class="com.ssh.dao.UserDao" id="userDao">
            <!--注入sessionFactory-->
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
        <!--配置userService-->
        <bean id="userService" class="com.ssh.service.UserService">
            <!--注入userDao-->
            <property name="userDao" ref="userDao"></property>
        </bean>
        <!--将action的创建交给spring ioc 注意action是多例的 一定要将scope设置为prototype-->
        <bean class="com.ssh.action.UserAction" id="userAction" scope="prototype"/>
    
    </beans>

    userDao

    package com.ssh.dao;
    
    import com.ssh.domain.User;
    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
    
    public class UserDao extends HibernateDaoSupport {
        public void save(User user) {
            this.getHibernateTemplate().save(user);
    
        }
    }

    userService

    package com.ssh.service;
    
    import com.ssh.dao.UserDao;
    import com.ssh.domain.User;
    import org.springframework.transaction.annotation.Transactional;
    
    
    @Transactional
    public class UserService {
    
        private UserDao userDao;
        public void setUserDao(UserDao userDao) {
            this.userDao = userDao;
        }
        public void save(User user) {
            userDao.save(user);
        }
    
    
    }

    userServiceTest

    package com.ssh.test;
    
    import com.ssh.domain.User;
    import com.ssh.service.UserService;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    import javax.annotation.Resource;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:applicationContext.xml")
    public class UserServiceTest {
        @Resource(name = "userService")
        private UserService userService;
    
        @Test
        public void saveTest() {
            User user = new User();
            user.setId(1);
            user.setName("wolfy");
            user.setPassword("1234");
            userService.save(user);
        }
    }

    测试

    总结

    1、配置过程很麻烦,很可能会缺包。

    2、在保存数据的时候发现,执行了,但数据库中没有数据,原因是因为没有配置事务管理器,还要注意开启注解的方式管理事务。

     3、当然仍然可以进一步的整合,比如去掉hibernate.cfg.xml文件,在applicationContext.xml进行配置

    4、注意如果使用hibernate4需要下载hibernate4.2+的jar包

  • 相关阅读:
    攻击方法
    Paillier 同态加密方案
    $EL Gamal$ 密码方案的椭圆曲线形式
    【hbase】hbase的基本使用
    【linux】创建用户,查看用户
    【linux】删除命令
    【linux】上传文件也可以直接拖动文件到xshell中
    【linux】安装pip时报错
    【Linux】界面快捷键
    【linux】Ubuntu无法下载mysql
  • 原文地址:https://www.cnblogs.com/wolf-sun/p/9144643.html
Copyright © 2020-2023  润新知