• SSH框架整合


    文章根据传智播客人事管理系系统OA整理,感谢传智播客。

    1、新建JavaWeb项目,并修改字符编码。

    在项目上右击选择最后一项,修改为UTF-8
    1.1添加Junit4,右击项目选择Build path
    打开,
     
     
    添加以下jar包,写配置文件
    2、添加struts2环境。
    2.1添加过jar包之后,拷贝配置文件
    2.2在web.xml中加入struts2最核心的过滤器
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    3. <display-name>SSH-1</display-name>
    4. <!--Struts2过滤器 -->
    5. <filter>
    6. <!--过滤器名称 -->
    7. <filter-name>struts2</filter-name>
    8. <!--过滤器类 -->
    9. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    10. </filter>
    11. <!--Struts2过滤器映射 -->
    12. <filter-mapping>
    13. <!--过滤器名称 -->
    14. <filter-name>struts2</filter-name>
    15. <!--过滤器映射 -->
    16. <url-pattern>/*</url-pattern>
    17. </filter-mapping>
    18. <welcome-file-list>
    19. <welcome-file>index.jsp</welcome-file>
    20. </welcome-file-list>
    21. </web-app>
    修改struts2的配置文件为
    1. <?xml version="1.0" encoding="UTF-8" ?>
    2. <!DOCTYPE struts PUBLIC
    3. "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    4. "http://struts.apache.org/dtds/struts-2.3.dtd">
    5. <struts>
    6. <!-- 更改struts默认的主题 -->
    7. <constant name="struts.ui.theme" value="simple"/>
    8. <!-- 配置为开发模式 ,好处是修改过之后立即生效-->
    9. <constant name="struts.devMode" value="true"></constant>
    10. <!--声明包 -->
    11. <package name="pm" namespace="/" extends="json-default">
    12. <action name="product_*" class="com.atfuture.PM.action.ProductAction" method="{1}">
    13. <result name="list">/WEB-INF/jsp/product/product_list.jsp</result>
    14. <result name="success" type="redirect">/product_list</result>
    15. <result name="input">/WEB-INF/jsp/product/product_input.jsp</result>
    16. <result name="productStockAll">/WEB-INF/jsp/product/product_stock.jsp</result>
    17. <result name="cg_input">/WEB-INF/jsp/product/product_input.jsp</result>
    18. <result name="fintByCategory">/WEB-INF/jsp/product/product_cg_list.jsp</result>
    19. <result name="addinput">/WEB-INF/jsp/product/product_addinput.jsp</result>
    20. <result name="stockQuerySomeSuccess">/WEB-INF/jsp/product/someProduct.jsp</result>
    21. </action>
    22. </package>
    23. </struts>
    3、添加hibernate环境
    jar包,配置文件,hibernate.hbm.xml 映射文件和    hibernate.cfg.xml主文件
     
     这个必须加,不加报错。
    c3p0数据源
     3.1添加过jar包后,拷贝配置文件和映射文件
     
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    3. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    4. <hibernate-configuration>
    5. <session-factory>
    6. <!-- 1.数据库连接信息 <property name="connection.url">jdbc:mysql:///itcastoa0720</property>
    7. <property name="connection.driver_class">com.jdbc.mysql.Driver</property>
    8. <property name="connection.username">root</property> <property name="connection.password"></property> -->
    9. <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
    10. <!-- 2.其他配置 -->
    11. <property name="hbm2ddl.auto">update</property>
    12. <!-- 3.映射文件 -->
    13. <property name="show_sql">true</property>
    14. <property name="format_sql">true</property>
    15. <!-- 测试 -->
    16. <!-- <mapping resource="com/future/test/User.hbm.xml" /> -->
    17. <mapping resource="com/future/domain/User.hbm.xml" />
    18. </session-factory>
    19. </hibernate-configuration>
    1. 只是一个例子,具体内容以后修改。
    2. <?xml version="1.0"?>
    3. <!DOCTYPE hibernate-mapping PUBLIC
    4. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    5. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    6. <hibernate-mapping package="com.future.domain">
    7. <class name="User" table="t_user">
    8. <id name="id" type="int">
    9. <column name="I_ID" />
    10. <generator class="native" />
    11. </id>
    12. <property name="name" type="java.lang.String" unique="false">
    13. <column name="S_NAME" />
    14. </property>
    15. <property name="loginName" type="java.lang.String">
    16. <column name="S_LOGINNAME" />
    17. </property>
    18. <property name="password" type="java.lang.String">
    19. <column name="S_PASSWORD" />
    20. </property>
    21. <property name="phoneNum" type="java.lang.String">
    22. <column name="S_PHONENUM" />
    23. </property>
    24. <!--
    25. repairHistorys:本类和repairHistory一对多
    26. -->
    27. <set name="repairHistorys">
    28. <key column="repairManId"></key>
    29. <one-to-many class="RepairHistory" />
    30. </set>
    31. </class>
    32. </hibernate-mapping>
    4、添加spring环境 5个
    核心包
    依赖AOP
     子类代理
    日志
    之后考入配置文件:(注:这个配置文件必须自己手动创建,然后复制别人的内容)
     
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    4. xmlns:tx="http://www.springframework.org/schema/tx"
    5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    7. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    8. <!--自动扫描与装配bean 包含子包 -->
    9. <context:component-scan base-package="com.future"></context:component-scan>
    10. <!-- 导入外部得properties -->
    11. <context:property-placeholder location="classpath:jdbc.properties"/>
    12. </beans>
    至此,jar包添加完毕
    5、struts2与spring整合
    写测试类
    1. package cn.itcast.test;
    2. import com.opensymphony.xwork2.ActionSupport;
    3. public class TestAction extends ActionSupport{
    4. public String execute() throws Exception{
    5. System.out.println("==>>testAction execute!!");
    6. return "success";
    7. }
    8. }
    5.1之后配置测试页
     
    1. <%@ page language="java" contentType="text/html; charset=UTF-8"
    2. pageEncoding="UTF-8"%>
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    4. <html>
    5. <head>
    6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    7. <title>Insert title here</title>
    8. </head>
    9. <body>
    10. struts2 添加成功!
    11. </body>
    12. </html>
    struts2配置文件:
    1. <?xml version="1.0" encoding="UTF-8" ?>
    2. <!DOCTYPE struts PUBLIC
    3. "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    4. "http://struts.apache.org/dtds/struts-2.3.dtd">
    5. <struts>
    6. <!-- 更改struts默认的主题 -->
    7. <constant name="struts.ui.theme" value="simple"/>
    8. <!-- 配置为开发模式 ,好处是修改过之后立即生效-->
    9. <constant name="struts.devMode" value="true"></constant>
    10. <!--声明包 -->
    11. <package name="defult" extends="struts-default">
    12. <!-- 配置测试用Action,为与spring整合,所以写全类名 -->
    13. <action name="test" class="cn.itcast.test.TestAction">
    14. <result name="success">Test.jsp</result>
    15. </action>
    16. <!-- <action name="product_*" class="com.atfuture.PM.action.ProductAction" method="{1}">
    17. <result name="list">/WEB-INF/jsp/product/product_list.jsp</result>
    18. <result name="success" type="redirect">/product_list</result>
    19. <result name="input">/WEB-INF/jsp/product/product_input.jsp</result>
    20. <result name="productStockAll">/WEB-INF/jsp/product/product_stock.jsp</result>
    21. <result name="cg_input">/WEB-INF/jsp/product/product_input.jsp</result>
    22. <result name="fintByCategory">/WEB-INF/jsp/product/product_cg_list.jsp</result>
    23. <result name="addinput">/WEB-INF/jsp/product/product_addinput.jsp</result>
    24. <result name="stockQuerySomeSuccess">/WEB-INF/jsp/product/someProduct.jsp</result>
    25. </action> -->
    26. </package>
    27. </struts>
     
    如果报错:在web.xml中添加
    1. <!-- spring用于初始化容器对象得监听器配置监听器 -->
    2. <context-param>
    3. <param-name>contextConfigLocation</param-name>
    4. <param-value>classpath:applicationContext.xml</param-value>
    5. </context-param>
    6. <listener>
    7. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    8. </listener>
     连个JSP页面放在WebContext下。
    如果页面出现:
     并且访问test,出现
     代表struts2配置成功,
    (整合与为整合主要看struts配置文件)
     
    接下来测试spring
     
    1. package cn.itcast.test;
    2. import org.junit.Test;
    3. import org.springframework.context.ApplicationContext;
    4. import org.springframework.context.support.ClassPathXmlApplicationContext;
    5. public class SpringTest {
    6. private ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    7. @Test
    8. public void TestSpring(){
    9. TestAction testAction = (TestAction) ctx.getBean("testAction");
    10. System.out.println(testAction);
    11. }
    12. }
    不成功,因为还没有配置;
    在这出现注解
     现在TestAction中添加注解
     spring扫描到这个Bean,
    在applicationContext中添加自动扫描
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    4. xmlns:tx="http://www.springframework.org/schema/tx"
    5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    7. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    8. <!--自动扫描与装配bean 包含子包 -->
    9. <context:component-scan base-package="cn.itcast.test"></context:component-scan>
    10. <!-- 导入外部得properties -->
    11. <!-- <context:property-placeholder location="classpath:jdbc.properties"/> -->
    12. </beans>
    输入
    spring独立运行~~~
    5.2接下来整合spring
    5.2.1 添加spring监听(因为上面解决错误添加,现在就不用添加了)
     5.2.2添加整合jar包
     
    5.2.3测试
     在测试页中添加一句话
     显示
     整合成功、
     
    6、Hibernate与spring整合
    6.1首先
    在spring配置文件中 配置sessionFactory,
    applicationContext文件,配置之前写属性文件
     
    1. jdbcUrl=jdbc:mysql:///carmanage?useUnicode=true&characterEncoding=utf8
    2. driverClass=com.mysql.jdbc.Driver
    3. user=root
    4. password=root
     
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    4. xmlns:tx="http://www.springframework.org/schema/tx"
    5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    7. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    8. <!--自动扫描与装配bean 包含子包 -->
    9. <context:component-scan base-package="cn.itcast.test"></context:component-scan>
    10. <!-- 导入外部得properties -->
    11. <context:property-placeholder location="classpath:jdbc.properties"/>
    12. <!--配置sessionFactory -->
    13. <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    14. <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
    15. <property name="dataSource">
    16. <bean class="com.mchange.v2.c3p0.ComboPooledDataSource">
    17. <!-- 数据连接信息 -->
    18. <property name="jdbcUrl" value="${jdbcUrl}"></property>
    19. <property name="driverClass" value="${driverClass}"></property>
    20. <property name="user" value="${user}"></property>
    21. <property name="password" value="${password}"></property>
    22. <!-- 其他配置 -->
    23. <!--初始化时获取三个连接,取值应在minPoolSizemaxPoolSize之间。Default: 3 -->
    24. <property name="initialPoolSize" value="3"></property>
    25. <!--连接池中保留的最小连接数。Default: 3 -->
    26. <property name="minPoolSize" value="3"></property>
    27. <!--连接池中保留的最大连接数。Default: 15 -->
    28. <property name="maxPoolSize" value="5"></property>
    29. <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
    30. <property name="acquireIncrement" value="3"></property>
    31. <!-- 控制数据源内加载的PreparedStatements数量。如果maxStatementsmaxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
    32. <property name="maxStatements" value="8"></property>
    33. <!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
    34. <property name="maxStatementsPerConnection" value="5"></property>
    35. <!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
    36. <property name="maxIdleTime" value="1800"></property>
    37. </bean>
    38. </property>
    39. </bean>
    40. </beans>
    在springTest测试文件中测试sessionFactory
     
    1. package cn.itcast.test;
    2. import org.hibernate.SessionFactory;
    3. import org.junit.Test;
    4. import org.springframework.context.ApplicationContext;
    5. import org.springframework.context.support.ClassPathXmlApplicationContext;
    6. public class SpringTest {
    7. private ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    8. @Test
    9. public void TestSpring(){
    10. TestAction testAction = (TestAction) ctx.getBean("testAction");
    11. System.out.println(testAction);
    12. }
    13. @Test
    14. public void TestSessionFactory(){
    15. SessionFactory sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");
    16. System.out.println(sessionFactory);
    17. }
    18. }
    在这会出现一个错误,因为找不到hibernate得映射文件,在是之前复制得时候多复制了,现在把它注释掉
     在运行,成功~~
     6.2配置事务管理,在spring得配置文件applicationContext中配置。
    1. <!-- 配置声明式事物 -->
    2. <bean name="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    3. <property name="sessionFactory" ref="sessionFactory"></property>
    4. </bean>
    5. <!-- 配置处理事务的注释 -->
    6. <tx:annotation-driven transaction-manager="transactionManager"/>
    6.3配置测试用的javaBean
     
    1. package cn.itcast.domain;
    2. public class User {
    3. private Long id;
    4. private String name;
    5. public Long getId() {
    6. return id;
    7. }
    8. public void setId(Long id) {
    9. this.id = id;
    10. }
    11. public String getName() {
    12. return name;
    13. }
    14. public void setName(String name) {
    15. this.name = name;
    16. }
    17. }
    6.4配置映射文件
     
    1. <?xml version="1.0"?>
    2. <!DOCTYPE hibernate-mapping PUBLIC
    3. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    4. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    5. <hibernate-mapping package="cn.itcast.domain">
    6. <class name="User" table="test_user">
    7. <id name="id" type="int">
    8. <column name="I_ID" />
    9. <generator class="native" />
    10. </id>
    11. <property name="name" type="java.lang.String" >
    12. <column name="S_NAME" />
    13. </property>
    14. </class>
    15. </hibernate-mapping>
    在hibernate得配置文件中增加mapping
     6.5准备测试。
    6.5.1首先建立server层
     
    1. package cn.itcast.test;
    2. import javax.annotation.Resource;
    3. import org.hibernate.Session;
    4. import org.hibernate.SessionFactory;
    5. import org.springframework.stereotype.Service;
    6. import org.springframework.transaction.annotation.Transactional;
    7. import cn.itcast.domain.User;
    8. @Service("testService")
    9. public class TestService {
    10. @Resource
    11. private SessionFactory sessionFactory;
    12. @Transactional
    13. public void saveTwoUse(){
    14. Session session = sessionFactory.getCurrentSession();
    15. session.save(new User());
    16. int a=1/0;
    17. session.save(new User());
    18. }
    19. }
     接下来测试事务,在springTest中添加
    1. @Test
    2. public void TestTransaction(){
    3. TestService testService = (TestService) ctx.getBean("testService");
    4. testService.saveTwoUse();
    5. }
    提示,
     代表事务成功。
    注释掉:
     
    提示:
    最后:完整测试
    修改TestAction文件
     修改Test.jsp
     ,运行访问
    提示
     
     
    完全整合完成~
    7、资源分类
    7.1测试和主要内容分开,并且把配置文件放到一起
     7.2关于建包
     7.3关于资源
    8、配置日志
     日志jar包
     最后,关于日志在视频中找~~~。
     
  • 相关阅读:
    微软TechEd2013大会门票热卖!
    2013年国庆节前51Aspx源码发布详情
    2013年9月份第2周51Aspx源码发布详情
    微软TechEd2013大会将在北京、上海召开!
    2013年9月份第1周51Aspx源码发布详情
    2013年8月份第4周51Aspx源码发布详情
    设置快速导航
    Web Service超限
    深入浅出SharePoint2013——使用沙箱解决方案
    深入浅出SharePoint2013——常用术语
  • 原文地址:https://www.cnblogs.com/suiyue-/p/5740735.html
Copyright © 2020-2023  润新知