Coin163

首页 > Struts2+Spring+Hibernate三大框架的配置

Struts2+Spring+Hibernate三大框架的配置

2020腾讯云双十一活动,全年最低!!!(领取3500元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1073

2020阿里云双十一最低价产品入口,含代金券(新老用户有优惠),
入口地址https://www.aliyun.com/minisite/goods

相关推荐:Struts2框架技术

Struts2是基于WebWork的核心思想上产生的全新框架,在Java Web开发领域之中占有十分重要的地位,随着JSP技术的成熟,越来越多的程序开发人员专注于MVC框架,Struts2得到了广大程序员的青睐,本篇博客将对Struts2框架技术进行详细讲解。 创建Struts2项目步骤如

本文主要是为了方便自己的在工作中的查看需求,如果有什么疑惑可以在留言中交流。

web工程的配置文件web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.4">
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:beans.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <filter>
    <filter-name>encoding</filter-name>
    <filter-class>com.etcss.common.EncodingFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>encoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <filter>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

spring的配置文件beans.xml

相关推荐:Spring—struts2整合

1.整合步骤:     一、整合目标            Spring来管理Struts的Action      二、 新建一个web项目,配置web.xml文件如下:             <xml version="1.0" encoding="UTF-8">         <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" x

<?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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
     <context:annotation-config/>
     <context:component-scan base-package="com.etcss.servicebean"/>
     <context:component-scan base-package="com.etcss.controller"/>
     <bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/etcss?useUnicode=true&amp;characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
         <!-- 连接池启动时的初始值 -->
         <property name="initialSize" value="30"/>
         <!-- 连接池的最大值 -->
         <property name="maxActive" value="500"/>
         <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
         <property name="maxIdle" value="10"/>
         <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
         <property name="minIdle" value="3"/>
      </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
         <property name="dataSource" ref="dataSource"/>
         <property name="mappingResources">
            <list>
              <value>com/etcss/domain/User.hbm.xml</value>
              <value>com/etcss/domain/Subject.hbm.xml</value>
              <value>com/etcss/domain/LabBuilding.hbm.xml</value>
              <value>com/etcss/domain/LabRoom.hbm.xml</value>
              <value>com/etcss/domain/LabRoomCourse.hbm.xml</value>
            </list>
         </property>
         <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
                hibernate.hbm2ddl.auto=update
                hibernate.show_sql=false
                hibernate.format_sql=false
                hibernate.cache.use_second_level_cache=true
                hibernate.cache.use_query_cache=false
                hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
              </value>
         </property>
    </bean>
    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <tx:annotation-driven transaction-manager="txManager"/>

<!-- <bean id="personService" class="com.shs.service.StudentServiceBean" scope="prototype"/> <bean id="studentList" class="com.shs.service.StudentListServiceBean"/> --> 
</beans>

hibernate的实体映射文件:
首先建立一个类User.java类,并有以下属性。此处省略getter和setter方法。

public class User implements Serializable {

    private int id;             //id
    private String name;        //用户名
    private String account;     //账户
    private String psw;         //密码
    private String college;     //所属学院
    private int isManager;      //是否是排课老师
    private int isRoot;         //是否是管理员
    private String email;       //Email邮箱

    public User()
    {

    }

    public User(String name, String account, String psw, int isManager, int isRoot, String email) {
        super();
        this.name = name;
        this.account = account;
        this.psw = psw;
        this.isManager = isManager;
        this.isRoot = isRoot;
        this.email = email;
    }
    ......
}

User.hbm.xml 映射文件如下所示:

<?xml version="1.0" encoding="utf-8"?>  
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.etcss.domain">

    <class name="User" table="users">
        <id name="id" >
            <generator class="native"/>
        </id>
        <property name="name" not-null="false" type="string"/>
        <property name="account" not-null="false" type="string"/>
        <property name="psw" not-null="false" type="string"/>
        <property name="college" not-null="false" type="string"/>
        <property name="email" not-null="false" type="string"/>
        <property name="isManager" not-null="false" type="integer"/>
        <property name="isRoot" not-null="false" type="integer"/>
    </class>

</hibernate-mapping>

配置structs2 相关的配置文件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>
    <!-- 指定Web应用的默认编码集,相当于调用HttpServletRequest的setCharacterEncoding方法 -->
    <constant name="struts.i18n.encoding" value="UTF-8"/>
    <!-- 该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。 如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 -->
    <constant name="struts.action.extension" value="do,action"/>
    <!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->
    <constant name="struts.serve.static.browserCache" value="true"/>
    <!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 -->
    <constant name="struts.configuration.xml.reload" value="true"/>
    <!-- 开发模式下使用,这样可以打印出更详细的错误信息 -->
    <constant name="struts.devMode" value="false" />
     <!-- 默认的视图主题 -->
    <constant name="struts.ui.theme" value="simple" />
    <constant name="struts.objectFactory" value="spring" />
   <!-- <package name="index" namespace="/index" extends="struts-default"> <global-results> <result name="message">/index.jsp</result> </global-results> <action name="*" class="loginManageController" method="{1}"> <result name="jsjlab">/WEB-INF/page/jsjlab.jsp</result> <result name="jsjplab">/WEB-INF/page/jsjplab.jsp</result> <result name="xxgclab">/WEB-INF/page/xxgclab.jsp</result> </action> </package> -->

    <include file="./struts-login.xml"></include>
    <include file="./struts-user.xml"></include>
    <include file="./struts-labRoomCourse.xml"></include>
    <include file="./struts-root.xml"></include>   
    <include file="./struts-labBuilding.xml"></include>
    <include file="./struts-subject.xml"></include>
    <include file="./struts-room.xml"></include>
    <include file="./struts-course.xml"></include>
    <include file="./struts-tools.xml"></include>

    <!-- this is a test -->
    <package name="test" namespace="/" extends="struts-default">
        <global-results>
            <result name="message">/index.jsp</result>
        </global-results>
        <action name="*" class="myTestController" method="{1}">
            <result name="pagelist">/WEB-INF/page/test/pagelist.jsp</result>
        </action>
    </package>
</struts>

如上所示struts的配置文件可以分开文件来写,用include标签引入即可。

原文

本文主要是为了方便自己的在工作中的查看需求,如果有什么疑惑可以在留言中交流。 web工程的配置文件web.xml <xml version="1.0" encoding="UTF-8"><web-app xmlns:xsi="http://www.w3.org/2

------分隔线----------------------------