Struts2
导入相应的jar包
配置web.xml(WEB-INF目录下)
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>TBS</display-name> <filter> <filter-name>struts</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
配置struts.xml(src目录下)
<?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"> <default-action-ref name="welcome"></default-action-ref> <action name="welcome"> <result>/WEB-INF/welcome.jsp</result> </action> <action name="SigninAction" class="TBS.Action.Signin"> <result name="success">/WEB-INF/homepage.jsp</result> <result name="input">/WEB-INF/welcome.jsp</result> </action> </package> </struts>
spring + hibernate
spring对hibernate配置文件hibernate.cfg.xml的集成相当好,可以在Spring中配置Hibernate的SessionFactory从而取代Hibernate.cfg.xml和HibernateSessionFactory.java
Spring在集成Hibernate时又分为两种形式:(集成后就不需要Hibernate.cfg.xml了)
1. 继续使用Hibernate的映射文件*.hbm.xml时扫描映射文件的方法
Spring集成Hibernate时去掉了Hibernate.cfg.xml,此时如果还继续使用Hibernate的映射文件*.hbm.xml的话,在配置Hibernate的 SessionFactory 时就要配置以何种方式寻找Hibernate映射文件*.hbm.xml
此时spring中配置SessionFactory bean时它对应的class应为org.springframework.orm.hibernate.LocalSessionFactoryBean
例如:
<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" /><!-- 引用数据源 -->
<property name="mappingDirectoryLocations">
<list>
<value>classpath:com/cn/nos/services/pojo/</value><!-- 加载hibernate的映射文件*.hbm.xml -->
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<!--<prop key="hibernate.current_session_context_class">thread</prop>-->
</props>
</property>
</bean>
LocalSessionFactoryBean有好几个属性用来查找hibernate映射文件:mappingResources、mappingLocations、mappingDirectoryLocations与mappingJarLocations
他们的区别:
mappingResources:指定classpath下具体映射文件名
<property name="mappingResources"> <value>petclinic.hbm.xml </value> </property>
mappingLocations:可以指定任何文件路径,并且可以指定前缀:classpath、file等
<property name="mappingLocations"> <value>/WEB-INF/petclinic.hbm.xml </value> </property> <property name="mappingLocations"> <value>classpath:/com/company/domain/petclinic.hbm.xml </value> </property>
也可以用通配符指定,’‘指定一个文件(路径)名,’*‘指定多个文件(路径)名,例如:
<property name="mappingLocations"> <value>classpath:/com/company/domainmaps/*.hbm.xml </value> </property> 上面的配置是在com/company/domain包下任何maps路径下的hbm.xml文件都被加载为映射文件
mappingDirectoryLocations:指定映射的文件路径
<property name="mappingDirectoryLocations"> <list> <value>WEB-INF/HibernateMappings</value> </list> </property>
也可以通过classpath来指出
<property name="mappingDirectoryLocations"> <list> <value>classpath:/XXX/package/</value> </list> </property>
mappingJarLocations:指定加载的映射文件在jar文件中
2. 使用jpa注解形式的pojo对象,而去掉*.hbm.xml的Hibernate映射文件 时配置的方法
Spring集成Hibernate时去掉了Hibernate.cfg.xml,此时如果使用jpa注解形式的pojo对象,而去掉Hibernate的映射文件*.hbm.xml的话,在配置Hibernate的SessionFactory时就要配置以何种方式寻找jpa注解形式的pojo映射对象
此时spring中配置SessionFactory bean时它对应的class应为org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" /><!-- 引用数据源 -->
<property name="packagesToScan">
<list>
<value>com.cn.nos.services.pojo*</value><!-- 加载hibernate的jpa注解形式的实体类 -->
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<!--<prop key="hibernate.current_session_context_class">thread</prop>-->
</props>
</property>
</bean>
AnnotationSessionFactoryBean中查找jpa注解形式的pojo映射对象的属性有:annotatedClasses、packagesToScan
annotatedClasses:指定classpath下指定的注解映射实体类的类名
<property name="annotatedClasses"> <list> <value>com.test.ObjectBean</value><!-- 可以在这个list中配置多个 --> </list> </property>
packagesToScan指定映射文件的包名
<property name="packagesToScan"> <list> <value>com.cn.nos.services.pojo*</value><!-- 加载hibernate的jpa注解形式的实体类 --> </list> </property>