1) 新建一个web项目;
2) 配置struts2
a) 添加struts2所需的基本jar到lib目录下,尤其注意不要漏掉struts2-spring-plugin-2.2.1.1.jar包;
b)在web.xml中增加struts2的过滤器,配置如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" 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_3_0.xsd"> <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> </web-app>
|
c)在src目录下创建struts的配置文件,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> <package name="struts2" extends="struts-default"> </package> </struts>
|
3)配置Spring
a)导入所需的包;
b) 取消Enable AOP Builder功能,并选择将配置文件生成到WEB-INF目录下;
c) 配置 <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:beans.xml</param-value> </context-param>
|
4) 配置Hibernate
a) 导入所需的包包括数据库驱动包和连接池相关的包导入到lib目录下;
c) 在Spring配置文件中配置datasource和SessionFactory,如下:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://localhost:3306/test</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>root</value> </property> </bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref local="dataSource" /> </property> <property name="mappingResources"> <list> <!--<value>com/test/bean/User.hbm.xml</value>--> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">create</prop> </props> </property> </bean>
|
=====================================================================================
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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<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>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
</web-app>
2.applicationContext.xml
<?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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<!-- 配置事务管理器,需将sessionFactory注入事物管理器中,即将多个操作放在一起捆绑进行-->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sf"></property>
</bean>
<!-- 事务规则,相当于异常增强、前置增强等的方法-->
<tx:advice id="txadvice" transaction-manager="txManager">
<tx:attributes><!-- read-only="true" ,即此事务只读操作-->
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="change*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- 织入 -->
<aop:config>
<aop:pointcut expression="execution(* com.zx.biz.*.*(..))" id="pc" />
<aop:advisor advice-ref="txadvice" pointcut-ref="pc" />
</aop:config>
<!-- 自带数据源 -->
<bean id="ds"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="username" value="${jdbc.user}"></property>
</bean>
<bean id="sf"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="ds"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="mappingLocations">
<list>
<value>classpath:com/zx/entity/TbCareCard.hbm.xml</value>
<value>classpath:com/zx/entity/TbContact.hbm.xml</value>
<value>classpath:com/zx/entity/TbContactCusType.hbm.xml</value>
<value>classpath:com/zx/entity/TbContactType.hbm.xml</value>
<value>classpath:com/zx/entity/TbCustomer.hbm.xml</value>
<value>classpath:com/zx/entity/TbCustomerProduct.hbm.xml</value>
<value>classpath:com/zx/entity/TbFinancial.hbm.xml</value>
<value>classpath:com/zx/entity/TbMessage.hbm.xml</value>
<value>classpath:com/zx/entity/TbModel.hbm.xml</value>
<value>classpath:com/zx/entity/TbProduct.hbm.xml</value>
<value>classpath:com/zx/entity/TbReferralCard.hbm.xml</value>
<value>classpath:com/zx/entity/TbReview.hbm.xml</value>
<value>classpath:com/zx/entity/TbRole.hbm.xml</value>
<value>classpath:com/zx/entity/TbSalesGroup.hbm.xml</value>
<value>classpath:com/zx/entity/TbServices.hbm.xml</value>
<value>classpath:com/zx/entity/TbSysDept.hbm.xml</value>
<value>classpath:com/zx/entity/TbSysUser.hbm.xml</value>
<value>classpath:com/zx/entity/TbUserRole.hbm.xml</value>
<value>classpath:com/zx/entity/TbWorkPlan.hbm.xml</value>
<value>classpath:com/zx/entity/TbProductPlan.hbm.xml</value>
<value>classpath:com/zx/entity/TbCustomerProduct.hbm.xml</value>
<value>classpath:com/zx/entity/TbFamily.hbm.xml</value>
<value>classpath:com/zx/entity/TbCusFamily.hbm.xml</value>
</list>
</property>
</bean>
<import resource="applicationContext-dao.xml" />
<import resource="applicationContext-biz.xml" />
<import resource="applicationContext-action.xml" />
</beans>
3.applicationContext-dao.xml
<pre name="code" class="html"><?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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" >
<bean id="loginDao" class="com.zx.dao.impl.TbSysUserDAO">
<property name="sessionFactory" ref="sf"></property>
</bean>
<bean id="conTypeDAO" class="com.zx.dao.impl.TbContactTypeDAO">
<property name="sessionFactory" ref="sf"></property>
</bean>
<bean id="conCusTypeDAO" class="com.zx.dao.impl.TbContactCusTypeDAO">
<property name="sessionFactory" ref="sf"></property>
</bean>
<bean id="conDAO" class="com.zx.dao.impl.TbContactDAO">
<property name="sessionFactory" ref="sf"></property>
</bean>
<bean id="salesGroupDAO" class="com.zx.dao.impl.TbSalesGroupDAO">
<property name="sessionFactory" ref="sf"></property>
</bean>
</beans>
4.applicationContext-biz.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
<span style="white-space:pre"> </span>xmlns="http://www.springframework.org/schema/beans"
<span style="white-space:pre"> </span>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<span style="white-space:pre"> </span>xmlns:p="http://www.springframework.org/schema/p"
<span style="white-space:pre"> </span>xmlns:tx="http://www.springframework.org/schema/tx"
<span style="white-space:pre"> </span>xmlns:aop="http://www.springframework.org/schema/aop"
<span style="white-space:pre"> </span>xsi:schemaLocation="http://www.springframework.org/schema/beans
<span style="white-space:pre"> </span>http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
<span style="white-space:pre"> </span>http://www.springframework.org/schema/tx
<span style="white-space:pre"> </span>http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
<span style="white-space:pre"> </span>http://www.springframework.org/schema/aop
<span style="white-space:pre"> </span>http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" >
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span><bean id="loginDao" class="com.zx.dao.impl.TbSysUserDAO">
<span style="white-space:pre"> </span> <property name="sessionFactory" ref="sf"></property>
<span style="white-space:pre"> </span></bean>
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span><bean id="conTypeDAO" class="com.zx.dao.impl.TbContactTypeDAO">
<span style="white-space:pre"> </span><property name="sessionFactory" ref="sf"></property>
<span style="white-space:pre"> </span></bean>
<span style="white-space:pre"> </span><bean id="conCusTypeDAO" class="com.zx.dao.impl.TbContactCusTypeDAO">
<span style="white-space:pre"> </span><property name="sessionFactory" ref="sf"></property>
<span style="white-space:pre"> </span></bean>
<span style="white-space:pre"> </span><bean id="conDAO" class="com.zx.dao.impl.TbContactDAO">
<span style="white-space:pre"> </span><property name="sessionFactory" ref="sf"></property>
<span style="white-space:pre"> </span></bean>
<span style="white-space:pre"> </span><bean id="salesGroupDAO" class="com.zx.dao.impl.TbSalesGroupDAO">
<span style="white-space:pre"> </span><property name="sessionFactory" ref="sf"></property>
<span style="white-space:pre"> </span></bean>
<span style="white-space:pre"> </span></beans>
5.applicationContext-action.xml
<?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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" >
<bean id="conAction" class="com.zx.action.ContactAction">
<property name="conBiz" ref="conBiz"></property>
</bean>
<bean id="conTypeAction" class="com.zx.action.ContactTypeAction">
<property name="conTypeBiz" ref="conTypeBiz"></property>
</bean>
<bean id="conCusTypeAction" class="com.zx.action.ContactTypeAction">
<property name="conCusTypeBiz" ref="conCusTypeBiz"></property>
</bean>
<bean id="salesGroupAction" class="com.zx.action.SellGradeAction">
<property name="salesGroupBiz" ref="salesGroupBiz"></property>
</bean><bean id="loginAction" class="com.zx.action.LoginAction">
<property name="TbSysUserBizImpl" ref="TbSysUserBizImpl"></property>
</bean>
</beans>
6.struts.xml(注入失败时可考虑将action-class写为action-bean-id)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="Contaction" extends="struts-default" namespace="/">
<default-action-ref name="defaultAction"></default-action-ref>
<action name="defaultAction">
<result>/error.jsp</result>
</action>
<action name="updatecontact" class="com.zx.action.ContactAction" method="updatecontact">
<result type="redirect">selectCustomer</result>
<result name="error" type="redirect">/login.jsp</result>
<result name="input" type="redirect">/login.jsp</result>
</action>
<action name="toUpdateContact" class="com.zx.action.ContactAction" method="selectContactBycontactId">
<result type="redirect">/jsp/plan/3addUpdate.jsp</result>
<result name="error" type="redirect">/login.jsp</result>
<result name="input" type="redirect">/login.jsp</result>
</action>
<action name="selectCustomer" class="com.zx.action.ContactAction" method="findContactByUser">
<result type="redirect">/jsp/plan/3hisPlanDetail.jsp</result>
<result name="error" type="redirect">/login.jsp</result>
<result name="input" type="redirect">/login.jsp</result>
</action>
<action name="selectcontact" class="com.zx.action.ContactAction" method="selectContact">
<result type="redirect">/jsp/plan/3planManager.jsp</result>
<result name="error" type="redirect">/login.jsp</result>
<result name="input" type="redirect">/login.jsp</result>
</action>
<action name="deletecontactfuben" class="com.zx.action.ContactAction" method="deletecontactfuben">
<result type="redirect">/jsp/plan/3hisPlanDetail.jsp</result>
<result name="error" type="redirect">/login.jsp</result>
<result name="input" type="redirect">/login.jsp</result>
</action>
<action name="deletecontact" class="com.zx.action.ContactAction" method="deletecontact">
<result type="redirectAction">selectcontact</result>
<result name="error" type="redirect">/login.jsp</result>
<result name="input" type="redirect">/login.jsp</result>
</action>
<action name="selectTypeList" class="com.zx.action.ContactTypeAction" method="contactTypeList">
<result type="redirectAction">selectcontact</result>
<result name="error" type="redirect">/login.jsp</result>
<result name="input" type="redirect">/login.jsp</result>
</action>
<action name="addcontact" class="com.zx.action.ContactAction" method="addcontact">
<result type="redirectAction">selectCustomer</result>
<result name="error" type="redirect">/login.jsp</result>
<result name="input" type="redirect">/login.jsp</result>
</action>
<action name="TbReview_reviewCaseInfoshowById" class="com.zx.action.TbReviewAction" method="reviewCaseInfoshowById">
<result name="success">/jsp/reviewcase/reviewCaseInfo.jsp</result>
</action>
</package>
</struts>
7.jdbc.properties
jdbc.url=jdbc\:oracle\:thin\:@172.16.17.157\:1521\:orcl
jdbc.driver=oracle.jdbc.OracleDriver
jdbc.user=root
jdbc.password=root
8.log4j.properties
### \u8BBE\u7F6ELogger\u8F93\u51FA\u7EA7\u522B\u548C\u8F93\u51FA\u76EE\u7684\u5730 ###
log4j.rootLogger=debug, stdout,logfile
### \u628A\u65E5\u5FD7\u4FE1\u606F\u8F93\u51FA\u5230\u63A7\u5236\u53F0 ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout
### \u628A\u65E5\u5FD7\u4FE1\u606F\u8F93\u51FA\u5230\u6587\u4EF6\uFF1Ajbit.log ###
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=jbit.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %l %F %p %m%n
9.Action实例中:jsp的接收,spring的注入,默认的request的传入参数(set,get)
public class ContactAction extends ActionSupport{
private int id1;
private int id2;
private int id3;
private Date id4;
private String id5;
private int id6;
private int id7;
private int id9;
private String id8;
private TbCustomer customer;
private TbContactHelp tbContact;//存放传入的动态条件查询的帮助类
private PageHelp ph1;//存放传入的分页帮助类
private String startContactDate;
private String endContactDate;
private int[] conId;
private IContactBiz conBiz;//联络计划的biz
private TbContact contact=new TbContact();//添加联络计划
private TbContact oneContact;
10.底层方法:两种写法以及父类
public class TbContactDAO extends HibernateDaoSupport implements ITbContactDAO {
/* (non-Javadoc)
* @see com.zx.dao.ITbContactDAO#findByExample(com.zx.entity.TbContact)
*/
public PageHelp findByExample(final TbContactHelp i,final PageHelp p,final TbCustomer customer) {
HibernateTemplate ht=this.getHibernateTemplate();
return ht.execute(new HibernateCallback(){
public PageHelp doInHibernate(Session session) throws HibernateException {
// Query query=arg0.createQuery("from com.yunhe.ssh.entity.Users u where u.name=:name");
// query.setParameter("name", u.getName());
// //Query query = session.createQuery("from Employee");
// //query.setFirstResult((page - 1) * size);
// //query.setMaxResults(size);
Criteria c=session.createCriteria(TbContact.class);
if(i.getContactId()!=null&&i.getContactId()>0){
c.add(Restrictions.eq("contactId",i.getContactId()));
}
if(i.getContactState()!=null&&i.getContactState()>0){
c.add( Restrictions.eq("contactState",i.getContactState()));
}
if(i.getStartContactDate()!=null){
c.add(Restrictions.ge("contactDate", i.getStartContactDate()));
}
if(i.getEndContactDate()!=null){
c.add(Restrictions.le("contactDate", i.getEndContactDate()));
}
if(i.getTbContactCusType()!=null&&i.getTbContactCusType().getConCusId()!=null&&i.getTbContactCusType().getConCusId()>0){
c.add(Restrictions.eq("tbContactCusType",i.getTbContactCusType()));
}
if(customer!=null){
c.add(Restrictions.eq("tbCustomer",customer));
}
if(i.getTbContactType()!=null&&i.getTbContactType().getConTypeId()!=null&&i.getTbContactType().getConTypeId()>0){
c.add(Restrictions.eq("tbContactType",i.getTbContactType()));
}
c.addOrder(Order.desc("contactDate"));
p.setAllCount( c.list().size());
if(p.getPageSize()>0&&p.getPageIndex()>0){
c.setFirstResult((p.getPageIndex()-1)*p.getPageSize());
c.setMaxResults(p.getPageSize());
}
p.setList(c.list() );
return p;
}
});
}
网络经典案例连接:http://blog.csdn.net/u010820702/article/details/51584387