1:web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  6. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  7. <!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring 提供的classpath:前缀指定从类路径下寻找-->
  8. <context-param>
  9. <param-name>contextConfigLocation</param-name>
  10. <param-value>classpath:beans.xml</param-value>
  11. </context-param>
  12. <!-- 对Spring容器进行实例化 -->
  13. <listener>
  14. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  15. </listener>
  16. <!-- 启动struts2框架 -->
  17. <filter>
  18. <filter-name>struts2</filter-name>
  19. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  20. </filter>
  21. <filter-mapping>
  22. <filter-name>struts2</filter-name>
  23. <url-pattern>/*</url-pattern>
  24. </filter-mapping>
  25. <welcome-file-list>
  26. <welcome-file>index.jsp</welcome-file>
  27. </welcome-file-list>
  28. </web-app>

2:spring配置文件:

  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"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="
  8. http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  12. http://www.springframework.org/schema/context
  13. http://www.springframework.org/schema/context/spring-context-2.5.xsd
  14. http://www.springframework.org/schema/tx
  15. http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
  16. <!-- 通过注解的方式将类交给spring管理 -->
  17. <!-- 是被@Component、@Service、@Controller、@Repository注解的类,都可以纳入spring容器的管理中 -->
  18. <context:component-scan base-package="cn.itcast"/>
  19. <!-- 配置文件占位符 -->
  20. <!--<context:property-placeholder location="classpath:jdbc.properties"/>-->
  21. <!-- 配置数据源 -->
  22. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
  23. <property name="driverClass" value="com.mysql.jdbc.Driver"/>
  24. <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&characterEncoding=UTF-8"/>
  25. <property name="user" value="root"/>
  26. <property name="password" value="123"/>
  27. <!-- 连接池启动时的初始值 -->
  28. <property name="initialPoolSize" value="1"/>
  29. <!-- 连接池中保留的最小连接数 -->
  30. <property name="minPoolSize" value="1"/>
  31. <!-- 连接池中保留的最大连接数 -->
  32. <property name="maxPoolSize" value="15"/>
  33. <!-- 最大空闲时间 ,60秒内未使用则丢弃。若为0则永不丢弃。Default:0-->
  34. <property name="maxIdleTime" value="60"/>
  35. <!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数,default:3 -->
  36. <property name="acquireIncrement" value="5"/>
  37. <!-- 每60秒检查所有连接池中的空闲连接。Default:0 -->
  38. <property name="idleConnectionTestPeriod" value="60"/>
  39. </bean>
  40. <!-- 用spring创建sessionFactory供hibernate用 -->
  41. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  42. <property name="dataSource" ref="dataSource" />
  43. <property name="mappingResources">
  44. <list>
  45. <value>cn/itcast/bean/Employee.hbm.xml</value>
  46. </list>
  47. </property>
  48. <property name="hibernateProperties">
  49. <value>
  50. hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
  51. hibernate.hbm2ddl.auto=update
  52. hibernate.show_sql=true
  53. hibernate.format_sql=false
  54. </value>
  55. </property>
  56. </bean>
  57. <!--
  58. hibernate.cache.use_second_level_cache=true
  59. hibernate.cache.use_query_cache=false
  60. hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
  61. -->
  62. <!-- 用spring创建sessionFactory供hibernate用 -->
  63. <!-- 基于注解的映射文件-->
  64. <!--
  65. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  66. <property name="dataSource" ref="dataSource" />
  67. <property name="annotatedClasses">
  68. <list>
  69. <value>cn.itcast.bean.Employee</value>
  70. </list>
  71. </property>
  72. <property name="hibernateProperties">
  73. <value>
  74. hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
  75. hibernate.hbm2ddl.auto=update
  76. hibernate.show_sql=true
  77. hibernate.format_sql=false
  78. </value>
  79. </property>
  80. </bean>
  81. -->
  82. <!-- 用spring创建sessionFactory供hibernate用 -->
  83. <!-- 基于注解的映射文件-->
  84. <!--
  85. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  86. <property name="dataSource" ref="dataSource" />
  87. <property name="packagesToScan" value="com.sdcncsi.entity.*" />
  88. <property name="hibernateProperties">
  89. <value>
  90. hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
  91. hibernate.hbm2ddl.auto=update
  92. hibernate.show_sql=true
  93. hibernate.format_sql=false
  94. </value>
  95. </property>
  96. </bean>
  97. -->
  98. <!-- 事务管理服务 -->
  99. <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  100. <property name="sessionFactory" ref="sessionFactory"/>
  101. </bean>
  102. <!-- 采用@Transaction注解方式使用事务 -->
  103. <tx:annotation-driven transaction-manager="txManager"/>
  104. </beans>

ps:当使用注解的方式关于映射,则就不是写映射文件了;

而在bean中用注解,下面举个例子:

  1. @SuppressWarnings("serial")
  2. @Entity()
  3. @Table(name="zft_check_login")
  4. @Cache(usage = CacheConcurrencyStrategy.NONE)
  5. public class CheckloginEntity implements Serializable{
  6. private Long id;
  7. private Long staff_id;
  8. private Date latest_time;
  9. private Staff staff;
  10. private Set<Equipment> equipments;
  11. @Id
  12. @GeneratedValue(strategy = GenerationType.AUTO)
  13. public Long getId() {
  14. return id;
  15. }
  16. public void setId(Long id) {
  17. this.id = id;
  18. }
  19. public Long getStaff_id() {
  20. return staff_id;
  21. }
  22. public void setStaff_id(Long staff_id) {
  23. this.staff_id = staff_id;
  24. }
  25. public Date getLatest_time() {
  26. return latest_time;
  27. }
  28. public void setLatest_time(Date latest_time) {
  29. this.latest_time = latest_time;
  30. }
  31. @OneToOne(cascade = {CascadeType.PERSIST,CascadeType.MERGE},fetch = FetchType.LAZY)
  32. @JoinColumn(name = "staff_id",insertable=false,updatable=false)
  33. public Staff getStaff() {
  34. return staff;
  35. }
  36. public void setStaff(Staff staff) {
  37. this.staff = staff;
  38. }
  39. @OneToMany(fetch = FetchType.LAZY)
  40. @JoinColumn(name = "staff_id",referencedColumnName="staff_id",insertable=false,updatable=false)
  41. public Set<Equipment> getEquipments() {
  42. return equipments;
  43. }
  44. public void setEquipments(Set<Equipment> equipments) {
  45. this.equipments = equipments;
  46. }
  47. @Transient
  48. public String getPhoneNums() throws Exception{
  49. return ReflectionUtils.fetchElementPropertyToString(equipments,"equipmentPhone",", ");
  50. }
  51. @Transient
  52. public Long getMinutes() throws Exception{
  53. Date etime=new Date(System.currentTimeMillis());
  54. return (etime.getTime() - this.getLatest_time().getTime())/(60*1000);
  55. }
  56. }

3:struts.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  4. "http://struts.apache.org/dtds/struts-2.0.dtd">
  5. <struts>
  6. <!-- 更换掉struts2内部的对象创建工厂,action用spring创建 -->
  7. <constant name="struts.objectFactory" value="spring"/>
  8. <!-- 将struts的主题改成默认主题,防止其生成不必要的html代码 -->
  9. <constant name="struts.ui.theme" value="simple"/>
  10. <package name="employee" namespace="/employee" extends="struts-default">
  11. <!-- 因为action有spring创建,所以只需指定spring创建的action的对象的名称就行  -->
  12. <action name="list" class="employeeAction">
  13. <result name="list">/WEB-INF/page/employee.jsp</result>
  14. </action>
  15. <action name="manage_*" class="employeeManageAction" method="{1}">
  16. <result name="add">/WEB-INF/page/employeeAdd.jsp</result>
  17. <result name="message">/WEB-INF/page/message.jsp</result>
  18. </action>
  19. </package>
  20. </struts>

shh整合后web.xml、spring配置文件和struts.xml的内容的更多相关文章

  1. 一 SSH整合:Spring整合Struts2的两种方式,struts.xml管理Action&Bean管理Action

    SSH回顾 1 引入jar包 Struts2的jar包 D:\Struts2\struts-2.3.35\apps\struts2-blank\WEB-INF\lib  开发基本包 Struts2有一 ...

  2. maven创建web工程Spring配置文件找不到问题解决方案

    使用maven创建web工程,将Spring配置文件applicationContext.xml放在src/resource下,用eclipse编译时提示class path resource [ap ...

  3. Spring Web项目spring配置文件随服务器启动时自动加载

    前言:其实配置文件不随服务器启动时加载也是可以的,但是这样操作的话,每次获取相应对象,就会去读取一次配置文件,从而降低程序的效率,而Spring中已经为我们提供了监听器,可监听服务器是否启动,然后在启 ...

  4. maven创建web工程Spring配置文件找不到

    使用maven创建web工程,将Spring配置文件applicationContext.xml放在src/resource下,用eclipse编译时提示class path resource [ap ...

  5. struts2与struts1整合,Unable to load configuration. - interceptor-ref ... struts.xml

    struts.xml中为了与struts1的MVC模式整合,需要类似如下的拦截器的引用 <interceptor-stack name="integration"> & ...

  6. struts2中struts.xml配置文件详解【未整理】

    1.    深入Struts2的配置文件 本部分主要介绍struts.xml的常用配置. 1.1.    包配置: Struts2框架中核心组件就是Action.拦截器等,Struts2框架使用包来管 ...

  7. Struts2笔记1:--Struts2原理、优点、编程流程、6大配置文件以及核心配置文件struts.xml

    Struts2原理(底层使用的是Servlet的doFilter方法): Struts2优点: 第一个Struts程序: 在开发Struts程序之前,首先要导入额外的jar包,基本需求的是14个jar ...

  8. 关于spring配置文件的头部编写

    //普通spring配置文件模板1 <?xml version="1.0" encoding="UTF-8" ?> <beans xmlns: ...

  9. Spring配置文件的读取

    1.配置文件的命名 Spring框架中的默认配置文件,建议命名为applicationContext.xml * 编写配置文件,默认位置有两个 ①src目录.②WEB-INF目录 2.Spring 配 ...

随机推荐

  1. PHP memcache扩展模块安装

    安装php扩展模块memcache memcache 的工作就是在专门的机器的内存里维护一张巨大的hash表,来存储经常被读写的一些数组与文件,从而极大的提高网站的运行效率,减轻后端数据库的读写压力. ...

  2. MySQL-Last_Errno: 1594

    故障现象  :MySQL slave所在机器自动重启,启动MySQL后,查看主从信息如下: Error_code: 1594   mysql> show slave status \G . ro ...

  3. 20145201《Java程序设计》第1次实验报告

    实验内容 一.命令行下java程序开发 1.建立Code目录,输入mkdir 20145201命令建立实验目录,并使用dir命令查看目录建立情况. 运行结果如图 2.进入实验目录,输入mkdir ex ...

  4. Oracle中对现有表增加列

    altertable Tablename add(column1 varchar2(20),column2 number(7,2)...) --Oracle中修改列名不可以,但是可以删除列,增加列 a ...

  5. Linux下使用USB模拟ACM串口设备【转】

    本文转载自:https://www.cnblogs.com/pied/p/4549614.html 这个想法之前就在脑袋里有过,最近公司产品要用到,所以多做了些了解. 1. USB 简介 USB 是 ...

  6. System.load 和 System.loadLibrary详解

    System.load 和 System.loadLibrary详解 1.它们都可以用来装载库文件,不论是JNI库文件还是非JNI库文件.在任何本地方法被调用之前必须先用这个两个方法之一把相应的JNI ...

  7. 洛谷P4311 士兵占领

    题目描述 有一个M * N的棋盘,有的格子是障碍.现在你要选择一些格子来放置一些士兵,一个格子里最多可以放置一个士兵,障碍格里不能放置士兵.我们称这些士兵占领了整个棋盘当满足第i行至少放置了Li个士兵 ...

  8. Sql Server 日期时间格式转换

    日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, 20) 结果:2007-02-01 08:02 CONVERT(varchar(10), 时间一, 23) 结果:2 ...

  9. 通过wifi连接android设备的方法

    [转自]http://blog.csdn.net/kuanxu/article/details/7444874 最近由于要在另外一台android设备上调试代码,在本机PC上查看其log.两台机器离的 ...

  10. Hibernate -- 一对一映射

    一对一关联指两个表之间的记录是一一对应的关系.分为两种:外键关联和主键关联. (1)外键关联 比如一家公司(Company)和它所在的地址(Address).在业务逻辑中要求一家公司只有唯一的地址,一 ...