Spring,hibernate,struts1环境搭建步骤

第一步

1改web。Xml文件

<!--给context设置applicationContext.xml的位置-->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath*:applicationContext*.xml</param-value>

</context-param>

<!--配置监听器在启动程序的时候就加载spring配置文件并且会帮定到servlteContext里面--><listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

2

配置过滤器2个org.springframework.web.filter.CharacterEncodingFilter

org.springframework.orm.hibernate3.support.OpenSessionInViewFilter

得到代码为
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml</param-value> </context-param> <!-- 目的让spring的配置文件在启动的时候读取,以后直接用,不用在读取 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 加入spring 关session过滤器 请先配置事务管理 这个过滤器放第一位--> <filter> <filter-name>session</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>session</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置乱码过滤器,Spring已经写好了,直接配置 --> <filter> <filter-name>char</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>GBK</param-value> </init-param> </filter> <filter-mapping> <filter-name>char</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

第二步:
复制多份applicationContext.xml文件,分别用来注入不同包的类容
applicationContext_dao.xml
applicationConetxt_imp.xml
applicationContext_service.xml
applicationContext_action.xml
applicationContext_tran.xml //用来注入事务,原来事务由hibernate做,配置后由spring来做

第三步
生成action,然后修改action中的type属性改成org.springframework.web.struts.DelegatingActionProxy
或者不改,加入<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"></controller>这么一句代码!
然后在sping中配置

spring里面加入action的注入

<beanname="/login"class="hw.web.action.LoginAction">

<propertyname="userService"ref="userService"></property>//表示问LoginAction中的userService注入值

</bean>


第四步,映射数据库表,dao,dao.impl,service,service.impl,action,在
applicationContext_dao.xml
applicationConetxt_imp.xml
applicationContext_service.xml
applicationContext_action.xml
applicationContext_tran.xml
中注入值
例如(这是ssh搭建后的一个银行转帐的例子)
dao接口
package dao;

public interface VipuserDAO {
	public Object selectById(int id);
}

 

dao实现类
package dao.impl; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import pojo.Card; import dao.VipuserDAO; public class VipuserImpl extends HibernateDaoSupport implements VipuserDAO{ public Object selectById(int id) { return getHibernateTemplate().get(Card.class, id); } }
service接口
package service; public interface VipServiceDAO { public void zhuangzhang(int fromcard,int tocard,double money); }
service实现类
import pojo.Card; import dao.VipuserDAO; import service.VipServiceDAO; public class VipuserServiceImpl extends HibernateDaoSupport implements VipServiceDAO{ private VipuserDAO vipuserdao;//记得生成set跟get方法 public void zhuangzhang(int fromcard, int tocard, double money) { Card c1=(Card)vipuserdao.selectById(fromcard); Card c2=(Card)vipuserdao.selectById(tocard); c2.setDollar(c2.getDollar()+money); c1.setDollar(c1.getDollar()-money); getHibernateTemplate().update(c2); getHibernateTemplate().update(c1); } public VipuserDAO getVipuserdao() { return vipuserdao; } public void setVipuserdao(VipuserDAO vipuserdao) { this.vipuserdao = vipuserdao; } }
action类
package web.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import service.VipServiceDAO; import service.impl.VipuserServiceImpl; public class UserAction extends Action { private VipServiceDAO service;//记得生成get跟set方法 public VipServiceDAO getService() { return service; } public void setService(VipServiceDAO service) { this.service = service; } public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { System.out.println(service); service.zhuangzhang(1000, 1002, 100); return null; } }
配置文件
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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"> </property> <property name="url" value="jdbc:sqlserver://127.0.0.1:1433;databaseName=test"> </property> <property name="username" value="sa"></property> <property name="password" value="123"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.SQLServerDialect </prop> <prop key="hibernate.show_sql"> true </prop> </props> </property> <property name="mappingResources"> <list> <value>pojo/Card.hbm.xml</value></list> </property></bean></beans>
applicationContext_dao.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="baseDao" abstract="true"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="vipuserdao" class="dao.impl.VipuserImpl" parent="baseDao"></bean> </beans>applicationContext_service.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="vipuserservice" class="service.impl.VipuserServiceImpl" autowire="byType"></bean> </beans>
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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean name="/user" class="web.action.UserAction"> <property name="service" ref="vipuserservice"></property> </bean> </beans>
applicationContext_tran.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: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.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <tx:advice id="advice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="zhuangzhang*" propagation="REQUIRED"/> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(* service.impl.*.*(..))" id="point"/> <aop:advisor advice-ref="advice" pointcut-ref="point"/> </aop:config> </beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" 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> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>session</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>session</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>char</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>GBK</param-value> </init-param> </filter> <filter-mapping> <filter-name>char</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>3</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>3</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"> <struts-config> <data-sources /> <form-beans /> <global-exceptions /> <global-forwards /> <action-mappings > <action path="/user" type="web.action.UserAction"> <set-property property="cancellable" value="true" /> </action> </action-mappings> <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"></controller> <message-resources parameter="web.ApplicationResources" /> </struts-config>


请勿乱转,尊重程序之道!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值