shh整合後web.xml、spring配置文件和struts.xml的內容

1:web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<!-- 指定spring的配置文件,默認從web根目錄尋找配置文件,我們可以通過spring 提供的classpath:前綴指定從類路徑下尋找-->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:beans.xml</param-value>
	</context-param>
	<!-- 對Spring容器進行實例化 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<!-- 啓動struts2框架 -->
	<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>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

2:spring配置文件:

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"  
       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/aop 
		http://www.springframework.org/schema/aop/spring-aop-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/tx 
        http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
       <!-- 通過註解的方式將類交給spring管理 -->
       <!-- 是被@Component、@Service、@Controller、@Repository註解的類,都可以納入spring容器的管理中 -->
       <context:component-scan base-package="cn.itcast"/>
       
        <!-- 配置文件佔位符 -->
        <!--<context:property-placeholder location="classpath:jdbc.properties"/>-->
        <!-- 配置數據源 -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        	<property name="driverClass" value="com.mysql.jdbc.Driver"/>
        	<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&characterEncoding=UTF-8"/>
        	<property name="user" value="root"/>
        	<property name="password" value="123"/>
        	<!-- 連接池啓動時的初始值 -->
        	<property name="initialPoolSize" value="1"/>
        	<!-- 連接池中保留的最小連接數 -->
        	<property name="minPoolSize" value="1"/>
        	<!-- 連接池中保留的最大連接數 -->
        	<property name="maxPoolSize" value="15"/>
        	<!-- 最大空閒時間 ,60秒內未使用則丟棄。若爲0則永不丟棄。Default:0-->
        	<property name="maxIdleTime" value="60"/>
        	<!-- 當連接池中的連接耗盡的時候c3p0一次同時獲取的連接數,default:3 -->
        	<property name="acquireIncrement" value="5"/>
        	<!-- 每60秒檢查所有連接池中的空閒連接。Default:0 -->
        	<property name="idleConnectionTestPeriod" value="60"/>
        </bean>
       <!-- 用spring創建sessionFactory供hibernate用 -->
       <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
       		<property name="dataSource" ref="dataSource" />
  			<property name="mappingResources">
  			<list>
   				<value>cn/itcast/bean/Employee.hbm.xml</value>
  			</list>
  			</property>
  			<property name="hibernateProperties">
  				<value>
	  				hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
	  				hibernate.hbm2ddl.auto=update
	  				hibernate.show_sql=true
	  				hibernate.format_sql=false
				</value>
  			</property>
    	</bean>
    	<!-- 
    		hibernate.cache.use_second_level_cache=true
	  	hibernate.cache.use_query_cache=false
	  	hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider 
	 -->
	  	
	  	<!-- 用spring創建sessionFactory供hibernate用 -->
		<!-- 基於註解的映射文件-->
		<!--
		<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
       		<property name="dataSource" ref="dataSource" />
  			<property name="annotatedClasses">
				<list>
					<value>cn.itcast.bean.Employee</value>
				</list>
  			</property>
  			<property name="hibernateProperties">
  				<value>
	  				hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
	  				hibernate.hbm2ddl.auto=update
	  				hibernate.show_sql=true
	  				hibernate.format_sql=false
				</value>
  	</property>
    	</bean>
		-->
		<!-- 用spring創建sessionFactory供hibernate用 -->
		<!-- 基於註解的映射文件-->
		<!--
		<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
       		<property name="dataSource" ref="dataSource" />
  			<property name="packagesToScan" value="com.sdcncsi.entity.*" />
  			<property name="hibernateProperties">
  				<value>
	  				hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
	  				hibernate.hbm2ddl.auto=update
	  				hibernate.show_sql=true
	  				hibernate.format_sql=false
				</value>
  	</property>
    	</bean>
		-->
	  	
	  	<!-- 事務管理服務 -->
        <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
			<property name="sessionFactory" ref="sessionFactory"/>        	
       </bean>
        <!-- 採用@Transaction註解方式使用事務 -->
        <tx:annotation-driven transaction-manager="txManager"/>
        
</beans>



ps:當使用註解的方式關於映射,則就不是寫映射文件了;

而在bean中用註解,下面舉個例子:

@SuppressWarnings("serial")
@Entity()
@Table(name="zft_check_login")
@Cache(usage = CacheConcurrencyStrategy.NONE)
public class CheckloginEntity implements Serializable{
	
	private Long id;
	private Long staff_id;
	private Date latest_time;
	private Staff staff;
	private Set<Equipment> equipments;
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Long getStaff_id() {
		return staff_id;
	}
	public void setStaff_id(Long staff_id) {
		this.staff_id = staff_id;
	}
	public Date getLatest_time() {
		return latest_time;
	}
	public void setLatest_time(Date latest_time) {
		this.latest_time = latest_time;
	}
	@OneToOne(cascade = {CascadeType.PERSIST,CascadeType.MERGE},fetch = FetchType.LAZY)
	@JoinColumn(name = "staff_id",insertable=false,updatable=false)
	public Staff getStaff() {
		return staff;
	}
	public void setStaff(Staff staff) {
		this.staff = staff;
	}
	
	@OneToMany(fetch = FetchType.LAZY)
	@JoinColumn(name = "staff_id",referencedColumnName="staff_id",insertable=false,updatable=false)
	public Set<Equipment> getEquipments() {
		return equipments;
	}
	public void setEquipments(Set<Equipment> equipments) {
		this.equipments = equipments;
	}
	
	@Transient
	public String getPhoneNums() throws Exception{
		return ReflectionUtils.fetchElementPropertyToString(equipments,"equipmentPhone",", ");
	}
	
	@Transient
	public Long getMinutes() throws Exception{
		Date etime=new Date(System.currentTimeMillis());
		return (etime.getTime() - this.getLatest_time().getTime())/(60*1000);
	}
}


3: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>
	<!-- 更換掉struts2內部的對象創建工廠,action用spring創建 -->
	<constant name="struts.objectFactory" value="spring"/>
	<!-- 將struts的主題改成默認主題,防止其生成不必要的html代碼 -->
	<constant name="struts.ui.theme" value="simple"/>
	<package name="employee" namespace="/employee" extends="struts-default">
		<!-- 因爲action有spring創建,所以只需指定spring創建的action的對象的名稱就行  -->
		<action name="list" class="employeeAction">
			<result name="list">/WEB-INF/page/employee.jsp</result>
		</action>
		<action name="manage_*" class="employeeManageAction" method="{1}">
			<result name="add">/WEB-INF/page/employeeAdd.jsp</result>
			<result name="message">/WEB-INF/page/message.jsp</result>
		</action>
	</package>
</struts>



發佈了244 篇原創文章 · 獲贊 31 · 訪問量 39萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章