`

Spring 与struts2 和Hibernate 4.0 + Dwr 注解解决方安

 
阅读更多

1. web.xml 注册文件

 

 

<!--
   author liuqing
   datetime 2011-12-11
-->
<?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">
    
   <filter>
	   <filter-name>OpenSessionInView</filter-name>
	   <filter-class>
		   org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
	   </filter-class>
   </filter>
   <filter-mapping>
	   <filter-name>OpenSessionInView</filter-name>
	   <url-pattern>/*</url-pattern>
   </filter-mapping>
   
   
    
    <!--struts2 Filter-->
    <filter>
        <filter-name>struts2Filter</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        <!-- modify .action is .html -->
        <init-param>
        	<param-name>struts.action.extension</param-name>
        	<param-value>htmls</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>struts2Filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- dwr 与Spring Integrating -->
    <servlet>
		  <servlet-name>dwr</servlet-name>
		  <servlet-class>org.directwebremoting.spring.DwrSpringServlet</servlet-class>
		  <init-param>
		    <param-name>debug</param-name>
		    <param-value>true</param-value>
		  </init-param>
    </servlet>
	<servlet-mapping>
	  <servlet-name>dwr</servlet-name>
	  <url-pattern>/dwr/*</url-pattern>
	</servlet-mapping>
    
	
    <!--Spring Listenter-->
    <!--context-param>
        <param-name>contextClass</param-name>
        <param-value>
          org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </param-value>
    </context-param-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
               classpath:com/zk/website/conf/applicationContext.xml
        </param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
    
    <!-- loading TableColumn Information -->
    <listener>
    	<listener-class>com.zk.website.listener.TableColumnNameListener</listener-class>
    </listener>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>default.jsp</welcome-file>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

 

配置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:dwr="http://www.directwebremoting.org/schema/spring-dwr"
	 xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd
         http://www.directwebremoting.org/schema/spring-dwr
    http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd"
	xmlns:tx="http://www.springframework.org/schema/tx">

	<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<!--property name="location" value="classpath:jdbc.properties" /-->
		<property name="locations">
		    <list>
		        <value>classpath:jdbc.properties</value>
		        <value>classpath:hibernate.properties</value>
		    </list>
		</property>
	</bean>

	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driverClassName}" />
		<property name="jdbcUrl" value="${jdbc.url}" />
		<property name="user" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="hibernateProperties">
	        <props>
	            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
	            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
	            <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
	            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
	        </props>
        </property>
		<property name="packagesToScan" value="com.zk.website.domain" />
	</bean>

	<context:component-scan base-package="com.zk.website" />
	<context:annotation-config />
	<!-- 配置DWR 对象 -->
	<dwr:annotation-scan scanDataTransferObject="true" scanRemoteProxy="true" base-package="com.zk.website.dwr"/>
	<!-- 用配置文件的方法 -->
	<!--dwr:configuration>
		<dwr:convert type="bean" class="com.zk.website.dwr.Student"></dwr:convert>
	</dwr:configuration-->
	<dwr:controller id="dwrController" debug="true"></dwr:controller>

</beans>

 

配置文件hibernate.properties

 

hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.hbm2ddl.auto=update

 

jdbc.properties

 

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/website
jdbc.username=root
jdbc.password=123456

 

 

 

 

 

3. hibernate 类文件

 

 

package com.zk.website.domain;


import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;


/**
 * 
 * @author liuqing
 * @see 
 * @version 1.0
 * 2011-12-11
 */
@Entity
@Table(name="consultingType")
public class ConsultingType  implements java.io.Serializable {


	private static final long serialVersionUID = -1841212339564306628L;
	
	 private Integer id;
     private String name;
     private Set<ProductConsulting> productConsultings = new HashSet<ProductConsulting>(0);

    public ConsultingType() {
    }
   
    @Id @GeneratedValue(strategy=IDENTITY)
    @Column(name="id", unique=true, nullable=false)
    public Integer getId() {
        return this.id;
    }
    
    public void setId(Integer id) {
        this.id = id;
    }
    
    @Column(name="name")
    public String getName() {
        return this.name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="consultingType")
    public Set<ProductConsulting> getProductConsultings() {
        return productConsultings;
    }

    public void setProductConsultings(Set<ProductConsulting> productConsultings) {
        this.productConsultings = productConsultings;
    }



}

 

 

hibentate 2)

   package com.zk.website.domain;

import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;


/**
 * 
 * @author liuqing
 * @see
 * @version 1.0
 * 2011-12-11
 */
@Entity
@Table(name="coporationInfoType")
public class CoporationInfoType  implements java.io.Serializable {

	private static final long serialVersionUID = 5576757129051664373L;
	 
	 private Integer id;
     private String name;
     private String remark;
     private Set<CoporationInfo> coporationInfoes = new HashSet<CoporationInfo>(0);

    public CoporationInfoType() {
    }

    @Id @GeneratedValue(strategy=IDENTITY)
    @Column(name="id", unique=true, nullable=false)
    public Integer getId() {
        return this.id;
    }
    
    public void setId(Integer id) {
        this.id = id;
    }
    
    @Column(name="name")
    public String getName() {
        return this.name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    @Column(name="remark")
    public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="coporationInfoType")
    public Set<CoporationInfo> getCoporationInfoes() {
        return coporationInfoes;
    }

    public void setCoporationInfoes(Set<CoporationInfo> coporationInfoes) {
        this.coporationInfoes = coporationInfoes;
    }

}

 

struts2类文件

 

   package com.zk.website.web;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * @author liuqing
 * @see 公司信息
 * @version 1.0
 * 2011-12-8
 */
@Controller("coporationInfoAction")
@Scope("prototype")
public class CoporationInfoAction extends ActionSupport {
	
	private static final long serialVersionUID = 3048607397328082554L;
	
	public String coporationInfo() {
		return SUCCESS;
	}

}
 

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">

<!-- START SNIPPET: xworkSample -->
<struts>
    <constant name="struts.i18n.reload" value="false" />
    <constant name="struts.devMode" value="true" />
    
    <package name="global" namespace="/" extends="struts-default">
    	<action name="index" class="indexAction">
    		<result name="success">/index.jsp</result>
    	</action>
    	<action name="" class="indexAction">
    		<result name="success">/index.jsp</result>
    	</action>
    </package>
    
    <package name="test_1" namespace="/helloWorld" extends="global">
        <action name="testAction" class="testAction" method="add">
            <result name="list">/testAction/test.jsp</result>
        </action>
    </package>
    <package name="product_1" namespace="/productInfo" extends="global">
    	<action name="productInfoAction_*" class="productInfoAction" method="{1}">
    	    <result name="success">/productInfo/productInfo_list.jsp</result>
    	</action>
    </package>
    
    <package name="customer_1" namespace="/customerServer" extends="global">
    	<action name="customerServerAction_*" class="customerServerAction" method="{1}">
    		<result name="success">/customerServer/customerServer_list.jsp</result>
    	</action>
    </package>
    
    <package name="findPeople_1" namespace="/findPeople" extends="global">
    	<action name="findPeopleAction_*" class="findPeopleAction" method="{1}">
    		<result name="success">/findPeople/findPeople_list.jsp</result>
    	</action>
    </package>
    
</struts>

 

dwr 类文件

 

package com.zk.website.dwr;

import java.util.ArrayList;
import java.util.List;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProperty;
import org.directwebremoting.annotations.RemoteProxy;

/**
 * 
 * @author liuqing
 * @see
 * @version 1.0
 * 2011-12-11
 */
@RemoteProxy
public class DwrTest {
	
	public String test() {
		return "Hello World Dwr!";
	}
	//加了个这注象后没有加的默认不能访问
	@RemoteMethod
	public String usb() {
		return "usb";
	}
	@RemoteMethod
	public Student usbss() {
		Student stu = new Student();
		stu.setUsb("122431431");
		return stu;
	}
	@RemoteMethod
	public List<Student> queryByAll() {
		List<Student> students = new ArrayList<Student>();
		Student stu = new Student();
		stu.setUsb("122431431");
		Student stu1 = new Student();
		stu.setUsb("122431431");
		students.add(stu);
		students.add(stu1);
		return students;
	}
	
    protected String hellProtected() {
    	return "helloProtected";
    }
    
    private String priHello() {
    	return "private";
    }

} 

 

 

package com.zk.website.dwr;

import java.io.Serializable;

import org.directwebremoting.annotations.DataTransferObject;
import org.directwebremoting.annotations.RemoteProperty;

/**
 * 
 * @author liuqing
 * @see Dwr对象测试
 * @version 1.0
 * 2011-12-11
 */
@DataTransferObject
public class Student implements Serializable{
	
	
	private String usb;
	//同@RemoteMethod 一样
	@RemoteProperty
	private String id;

	public String getUsb() {
		return usb;
	}

	public void setUsb(String usb) {
		this.usb = usb;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
	
	
	

}
 

 

 

 

 

 

 

 OK 下面有整合后lib 包可以下载的报可以下载

 

希望对朋友们有所帮助

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics