- 浏览: 27146 次
- 性别:
- 来自: 深圳
最新评论
spring 与 struts 整合 有3 种方法
实质是 spring 的 ContextLoader (org.springframework.web.context.ContextLoader) 在ContextLoaderServlet加载 ,由ContextLoader的initWebApplicationContext(ServletContext)初始化WebApplicationContext;
本质将spring context 放置于 servletContext对象范围内
servletContext.setAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
贴出spring 框架源码 如下 :
ContextLoaderServlet.class :
public class ContextLoaderServlet extends HttpServlet {
private ContextLoader contextLoader;
/**
* Initialize the root web application context.
*/
public void init() throws ServletException {
this.contextLoader = createContextLoader();
this.contextLoader.initWebApplicationContext(getServletContext());
}
/**
* Create the ContextLoader to use. Can be overridden in subclasses.
* @return the new ContextLoader
*/
protected ContextLoader createContextLoader() {
return new ContextLoader();
}
/**
* Return the ContextLoader used by this servlet.
* @return the current ContextLoader
*/
public ContextLoader getContextLoader() {
return this.contextLoader;
}
/**
* Close the root web application context.
*/
public void destroy() {
if (this.contextLoader != null) {
this.contextLoader.closeWebApplicationContext(getServletContext());
}
}
/**
* This should never even be called since no mapping to this servlet should
* ever be created in web.xml. That's why a correctly invoked Servlet 2.3
* listener is much more appropriate for initialization work ;-)
*/
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
getServletContext().log(
"Attempt to call service method on ContextLoaderServlet as [" +
request.getRequestURI() + "] was ignored");
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
}
public String getServletInfo() {
return "ContextLoaderServlet for Servlet API 2.2/2.3 " +
"(deprecated in favor of ContextLoaderListener for Servlet API 2.4)";
}
}
ContextLoader.class :
public WebApplicationContext initWebApplicationContext(ServletContext servletContext)
throws IllegalStateException, BeansException {
if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
throw new IllegalStateException(
"Cannot initialize context because there is already a root application context present - " +
"check whether you have multiple ContextLoader* definitions in your web.xml!");
}
servletContext.log("Initializing Spring root WebApplicationContext");
if (logger.isInfoEnabled()) {
logger.info("Root WebApplicationContext: initialization started");
}
long startTime = System.currentTimeMillis();
try {
// Determine parent for root web application context, if any.
ApplicationContext parent = loadParentContext(servletContext);
// Store context in local instance variable, to guarantee that
// it is available on ServletContext shutdown.
this.context = createWebApplicationContext(servletContext, parent);
servletContext.setAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
if (logger.isDebugEnabled()) {
logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" +
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");
}
if (logger.isInfoEnabled()) {
long elapsedTime = System.currentTimeMillis() - startTime;
logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");
}
return this.context;
}
catch (RuntimeException ex) {
logger.error("Context initialization failed", ex);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);
throw ex;
}
catch (Error err) {
logger.error("Context initialization failed", err);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err);
throw err;
}
}
配置 web.xml 文件
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
或者
struts在此被装载org.apache.struts.action.ActionServlet,还有它的配置参数config文件struts-config.xml,spring在此被装载org.springframework.web.context.ContextLoaderServlet还有它的配置文件applicationContext.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>contextLoaderServlet</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
spring 加载log4j 把log4j.properties文件放到 WEB-INF下面
再加上如下配置即可
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<servlet>
<servlet-name>log4j</servlet-name>
<servlet-class>org.springframework.web.util.Log4jConfigServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
spring + struts + hibernate +dwr 整合
Web.xml :
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!--
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>contextLoaderServlet</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
-->
<filter>
<filter-name>myfilter</filter-name>
<filter-class>com.jeny.filter.MyFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>myfilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<servlet>
<servlet-name>log4j</servlet-name>
<servlet-class>
org.springframework.web.util.Log4jConfigServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>dwr</servlet-name>
<servlet-class>
org.directwebremoting.servlet.DwrServlet
</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/dwr.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr</servlet-name>
<url-pattern>/lixing/*</url-pattern>
</servlet-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>
</web-app>
Dwr.xml :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
"http://www.getahead.ltd.uk/dwr/dwr20.dtd">
<dwr>
<allow>
<create creator="new" javascript="hello">
<param name="class" value="com.dwr.Getpwd"/>
</create>
</allow>
</dwr>
Condb.properties 文件 放置于 WEN-INF目录下 :
Condriver=com.microsoft.jdbc.sqlserver.SQLServerDriver
url=jdbc:microsoft:sqlserver://127.0.0.1:1433;Databasename=mypubs
uid=sa
pwd=
applicationContext.xml :
这个是spring的专有配置文件,里面配置代理hibernate资源和struts的action
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="configbean" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="locations">
<list>
<value>/WEB-INF/Condb.properties</value>
</list>
</property>
</bean>
<!--
由 Condb.properties文件 获得 数据库连接 的 DriverClass , url 等…-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${Condriver}</value>
</property>
<property name="url">
<value>${url}</value>
</property>
<property name="username">
<value>${uid}</value>
</property>
<property name="password">
<value>${pwd}</value>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/com/form/</value>
</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.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="UserDAO" class="com.jeny.util.UserDAO">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
transactionAttributes是对所代理的方法哪些方法提供事务,比如你定义一个以add开头的方法,那它就可以有事务管理了,对于它里面的所有操作,都可以实现事务机制,若有异常就回滚事务
target它是指向要注入的类,代理这个类所实现的接口
<bean id="userDAOProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="target">
<ref local="UserDAO" />
</property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean name="/registion" class="com.action.RegistionAction">
<property name="user">
<ref bean="userDAOProxy"/>
</property>
</bean>
</beans>
Struts-config.xml 中的action 的type 值必须是 : org.springframework.web.struts.DelegatingActionProxy
<action path="/updatepwd"
type="org.springframework.web.struts.DelegatingActionProxy"
name="password" attribute="pwd">
DAO 层
Hibernate 框架源码
public abstract class HibernateDaoSupport extends DaoSupport {
private HibernateTemplate hibernateTemplate;
/**
* Set the Hibernate SessionFactory to be used by this DAO.
* Will automatically create a HibernateTemplate for the given SessionFactory.
* @see #createHibernateTemplate
* @see #setHibernateTemplate
*/
public final void setSessionFactory(SessionFactory sessionFactory) {
this.hibernateTemplate = createHibernateTemplate(sessionFactory);
}
/**
* Create a HibernateTemplate for the given SessionFactory.
* Only invoked if populating the DAO with a SessionFactory reference!
* <p>Can be overridden in subclasses to provide a HibernateTemplate instance
* with different configuration, or a custom HibernateTemplate subclass.
* @param sessionFactory the Hibernate SessionFactory to create a HibernateTemplate for
* @return the new HibernateTemplate instance
* @see #setSessionFactory
*/
protected HibernateTemplate createHibernateTemplate(SessionFactory sessionFactory) {
return new HibernateTemplate(sessionFactory);
}
由上可看出 注 : 要获得getHibernateTemplate 必须在UserDAO bean内 注入 SessionFactory
<bean id="UserDAO" class="com.jeny.util.UserDAO">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
DAO 继承HibernateDaoSupport
HibernateDaoSupport实现了HibernateTemplate和SessionFactory实例的关联, HibernateTemplate对Hibernate Session操作进行了封装,HibernateTemplate.execute方法则是一封装机制的核心. 借助HibernateTemplate我们可以脱离每次数据操作必须首先获得Session实例、启动事务、提交/回滚事务以及烦杂的try/catch/finally的繁琐操作.
Spring中的事务管理实际上是基于动态AOP机制实现
public class UserDAO extends HibernateDaoSupport implements IUserDAO{
private SessionFactory sessionFactory;
public boolean insertUser(UserInfo userinfo) {
this.getHibernateTemplate().save(userinfo);
return true;
}
public boolean checkLogin(User user) {
try {
List list = this.getHibernateTemplate().find("from UserInfo as u where ( u.cardnum ="+ user.getCardnum().trim() +" and u.password = "+ user.getPassword().trim() +")");
if(list.size()==0){
return false;
}
} catch (Exception e) {
return false;
}
return true;
}
public boolean updatePwd(Integer id,String pwd) {
UserInfo ui = (UserInfo)this.getHibernateTemplate().get(UserInfo.class,id);
ui.setPassword(pwd);
return true;
}
public boolean updateInfo(Integer id, Info in) {
UserInfo u = (UserInfo)this.getHibernateTemplate().get(UserInfo.class,id);
u.setName(……);
return true;
}
Spting ioc 的serter 与getter 将biz注入到业务action中
public class UpdatePwdAction extends Action {
private IUserDAO muuser;
public IUserDAO getMuuser() {
return muuser;
}
public void setMuuser(IUserDAO muuser) {
this.muuser = muuser;
}
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
Password password = (Password)form;
String oldpwd = password.getOldpassword().trim();
String pwd = password.getPassword().trim();
UserInfo c = (UserInfo)request.getSession().getAttribute("userinfo");
Integer id = c.getId();
boolean b = muuser.updatePwd(id,pwd);
if(b){
return mapping.findForward("updatepwdsucess");
}else{
return mapping.findForward("updatepwdfail");
}
}
}
实质是 spring 的 ContextLoader (org.springframework.web.context.ContextLoader) 在ContextLoaderServlet加载 ,由ContextLoader的initWebApplicationContext(ServletContext)初始化WebApplicationContext;
本质将spring context 放置于 servletContext对象范围内
servletContext.setAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
贴出spring 框架源码 如下 :
ContextLoaderServlet.class :
public class ContextLoaderServlet extends HttpServlet {
private ContextLoader contextLoader;
/**
* Initialize the root web application context.
*/
public void init() throws ServletException {
this.contextLoader = createContextLoader();
this.contextLoader.initWebApplicationContext(getServletContext());
}
/**
* Create the ContextLoader to use. Can be overridden in subclasses.
* @return the new ContextLoader
*/
protected ContextLoader createContextLoader() {
return new ContextLoader();
}
/**
* Return the ContextLoader used by this servlet.
* @return the current ContextLoader
*/
public ContextLoader getContextLoader() {
return this.contextLoader;
}
/**
* Close the root web application context.
*/
public void destroy() {
if (this.contextLoader != null) {
this.contextLoader.closeWebApplicationContext(getServletContext());
}
}
/**
* This should never even be called since no mapping to this servlet should
* ever be created in web.xml. That's why a correctly invoked Servlet 2.3
* listener is much more appropriate for initialization work ;-)
*/
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
getServletContext().log(
"Attempt to call service method on ContextLoaderServlet as [" +
request.getRequestURI() + "] was ignored");
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
}
public String getServletInfo() {
return "ContextLoaderServlet for Servlet API 2.2/2.3 " +
"(deprecated in favor of ContextLoaderListener for Servlet API 2.4)";
}
}
ContextLoader.class :
public WebApplicationContext initWebApplicationContext(ServletContext servletContext)
throws IllegalStateException, BeansException {
if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
throw new IllegalStateException(
"Cannot initialize context because there is already a root application context present - " +
"check whether you have multiple ContextLoader* definitions in your web.xml!");
}
servletContext.log("Initializing Spring root WebApplicationContext");
if (logger.isInfoEnabled()) {
logger.info("Root WebApplicationContext: initialization started");
}
long startTime = System.currentTimeMillis();
try {
// Determine parent for root web application context, if any.
ApplicationContext parent = loadParentContext(servletContext);
// Store context in local instance variable, to guarantee that
// it is available on ServletContext shutdown.
this.context = createWebApplicationContext(servletContext, parent);
servletContext.setAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
if (logger.isDebugEnabled()) {
logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" +
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");
}
if (logger.isInfoEnabled()) {
long elapsedTime = System.currentTimeMillis() - startTime;
logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");
}
return this.context;
}
catch (RuntimeException ex) {
logger.error("Context initialization failed", ex);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);
throw ex;
}
catch (Error err) {
logger.error("Context initialization failed", err);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err);
throw err;
}
}
配置 web.xml 文件
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
或者
struts在此被装载org.apache.struts.action.ActionServlet,还有它的配置参数config文件struts-config.xml,spring在此被装载org.springframework.web.context.ContextLoaderServlet还有它的配置文件applicationContext.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>contextLoaderServlet</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
spring 加载log4j 把log4j.properties文件放到 WEB-INF下面
再加上如下配置即可
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<servlet>
<servlet-name>log4j</servlet-name>
<servlet-class>org.springframework.web.util.Log4jConfigServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
spring + struts + hibernate +dwr 整合
Web.xml :
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!--
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>contextLoaderServlet</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
-->
<filter>
<filter-name>myfilter</filter-name>
<filter-class>com.jeny.filter.MyFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>myfilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<servlet>
<servlet-name>log4j</servlet-name>
<servlet-class>
org.springframework.web.util.Log4jConfigServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>dwr</servlet-name>
<servlet-class>
org.directwebremoting.servlet.DwrServlet
</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/dwr.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr</servlet-name>
<url-pattern>/lixing/*</url-pattern>
</servlet-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>
</web-app>
Dwr.xml :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
"http://www.getahead.ltd.uk/dwr/dwr20.dtd">
<dwr>
<allow>
<create creator="new" javascript="hello">
<param name="class" value="com.dwr.Getpwd"/>
</create>
</allow>
</dwr>
Condb.properties 文件 放置于 WEN-INF目录下 :
Condriver=com.microsoft.jdbc.sqlserver.SQLServerDriver
url=jdbc:microsoft:sqlserver://127.0.0.1:1433;Databasename=mypubs
uid=sa
pwd=
applicationContext.xml :
这个是spring的专有配置文件,里面配置代理hibernate资源和struts的action
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="configbean" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="locations">
<list>
<value>/WEB-INF/Condb.properties</value>
</list>
</property>
</bean>
<!--
由 Condb.properties文件 获得 数据库连接 的 DriverClass , url 等…-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${Condriver}</value>
</property>
<property name="url">
<value>${url}</value>
</property>
<property name="username">
<value>${uid}</value>
</property>
<property name="password">
<value>${pwd}</value>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/com/form/</value>
</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.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="UserDAO" class="com.jeny.util.UserDAO">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
transactionAttributes是对所代理的方法哪些方法提供事务,比如你定义一个以add开头的方法,那它就可以有事务管理了,对于它里面的所有操作,都可以实现事务机制,若有异常就回滚事务
target它是指向要注入的类,代理这个类所实现的接口
<bean id="userDAOProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="target">
<ref local="UserDAO" />
</property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean name="/registion" class="com.action.RegistionAction">
<property name="user">
<ref bean="userDAOProxy"/>
</property>
</bean>
</beans>
Struts-config.xml 中的action 的type 值必须是 : org.springframework.web.struts.DelegatingActionProxy
<action path="/updatepwd"
type="org.springframework.web.struts.DelegatingActionProxy"
name="password" attribute="pwd">
DAO 层
Hibernate 框架源码
public abstract class HibernateDaoSupport extends DaoSupport {
private HibernateTemplate hibernateTemplate;
/**
* Set the Hibernate SessionFactory to be used by this DAO.
* Will automatically create a HibernateTemplate for the given SessionFactory.
* @see #createHibernateTemplate
* @see #setHibernateTemplate
*/
public final void setSessionFactory(SessionFactory sessionFactory) {
this.hibernateTemplate = createHibernateTemplate(sessionFactory);
}
/**
* Create a HibernateTemplate for the given SessionFactory.
* Only invoked if populating the DAO with a SessionFactory reference!
* <p>Can be overridden in subclasses to provide a HibernateTemplate instance
* with different configuration, or a custom HibernateTemplate subclass.
* @param sessionFactory the Hibernate SessionFactory to create a HibernateTemplate for
* @return the new HibernateTemplate instance
* @see #setSessionFactory
*/
protected HibernateTemplate createHibernateTemplate(SessionFactory sessionFactory) {
return new HibernateTemplate(sessionFactory);
}
由上可看出 注 : 要获得getHibernateTemplate 必须在UserDAO bean内 注入 SessionFactory
<bean id="UserDAO" class="com.jeny.util.UserDAO">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
DAO 继承HibernateDaoSupport
HibernateDaoSupport实现了HibernateTemplate和SessionFactory实例的关联, HibernateTemplate对Hibernate Session操作进行了封装,HibernateTemplate.execute方法则是一封装机制的核心. 借助HibernateTemplate我们可以脱离每次数据操作必须首先获得Session实例、启动事务、提交/回滚事务以及烦杂的try/catch/finally的繁琐操作.
Spring中的事务管理实际上是基于动态AOP机制实现
public class UserDAO extends HibernateDaoSupport implements IUserDAO{
private SessionFactory sessionFactory;
public boolean insertUser(UserInfo userinfo) {
this.getHibernateTemplate().save(userinfo);
return true;
}
public boolean checkLogin(User user) {
try {
List list = this.getHibernateTemplate().find("from UserInfo as u where ( u.cardnum ="+ user.getCardnum().trim() +" and u.password = "+ user.getPassword().trim() +")");
if(list.size()==0){
return false;
}
} catch (Exception e) {
return false;
}
return true;
}
public boolean updatePwd(Integer id,String pwd) {
UserInfo ui = (UserInfo)this.getHibernateTemplate().get(UserInfo.class,id);
ui.setPassword(pwd);
return true;
}
public boolean updateInfo(Integer id, Info in) {
UserInfo u = (UserInfo)this.getHibernateTemplate().get(UserInfo.class,id);
u.setName(……);
return true;
}
Spting ioc 的serter 与getter 将biz注入到业务action中
public class UpdatePwdAction extends Action {
private IUserDAO muuser;
public IUserDAO getMuuser() {
return muuser;
}
public void setMuuser(IUserDAO muuser) {
this.muuser = muuser;
}
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
Password password = (Password)form;
String oldpwd = password.getOldpassword().trim();
String pwd = password.getPassword().trim();
UserInfo c = (UserInfo)request.getSession().getAttribute("userinfo");
Integer id = c.getId();
boolean b = muuser.updatePwd(id,pwd);
if(b){
return mapping.findForward("updatepwdsucess");
}else{
return mapping.findForward("updatepwdfail");
}
}
}
发表评论
-
杂乱的东西
2008-06-15 20:18 957... -
使用jad及eclipse插件进行.class文件的反编译
2008-06-06 22:54 1648[/color][color=white][color=dar ... -
Criteria
2008-03-14 10:19 2215[color=white][color=gray][color ... -
软件工程 与 软件测试
2007-12-20 14:25 1223[color=white]1﹑软件产品的特性 产品特性: ⑴ ... -
hql 查询
2007-12-20 14:22 1195[color=white][color=black][colo ... -
分页`
2007-12-20 14:19 1873[color=black][color=white] 分页sq ... -
dwr util.js 应用
2007-12-20 14:18 241[color=white]regcheck.js------- ... -
sql 应用
2007-12-20 14:09 1102[/color][color=white][color=bla ... -
java 与 读取 .properties文件
2007-12-20 14:08 1425sqlcon.properties配置文件 连接数据库 dri ... -
双向 一对多
2007-12-20 13:59 1993hibernate双向 一对多映射 : 双向一对多关联 , ... -
浅入冬眠
2007-12-20 13:56 1109Hibernate Configuration 正如其名, ... -
struts 令牌.. 验证...
2007-12-20 13:52 2651关于Form 突出要求的是Form只是用来存表单数据,只是数据 ... -
对struts的认识
2007-12-20 13:37 1627struts 概述: model 部分 : ... -
浅谈mvc
2007-12-20 13:35 1314mvc 的核心思想 : 把程序分成相对独立,而又能协同工作 ...
相关推荐
### SSI框架整合小结 #### 一、概述 本文旨在详细介绍在SSI(Struts + Spring + iBatis)框架下的整体运作流程。通过本文,读者可以了解到在SSI框架环境中,每一项具体操作是如何被各个组件所处理和执行的。 ####...
SpringBoot整合MyBatis中的问题及出现的一些问题小结 在本文中,我们将讨论SpringBoot整合MyBatis中的常见问题及其解决方案,旨在帮助开发者快速解决这些问题,提高开发效率。 1. SpringBoot整合MyBatis mapper...
优选整合人教版九年级上册数学小结与复习1共35张PPT学习教案.pptx
《战略管理第七章小结——并购策略及其挑战》 在全球化的浪潮中,企业战略的多元化与灵活运用成为了企业持续发展的关键。其中,收购战略(Acquisition Strategies)在近年来备受青睐,这主要得益于全球化进程的加速...
课堂小结有助于巩固知识,整合课堂内容,同时为后续学习铺设道路。通过归纳总结,可以突出教学重点,通过悬念设置,激发学生思考,通过反馈练习,发现并弥补学习中的不足。此外,小结还能够承前启后,为下节课做好...
文档内容主要围绕鉴定人资格年审工作进行了总结,涉及到司法...综上所述,这份工作小结展示了司法鉴定工作中制度化、专业化、人性化和社会责任感的重要性,同时也反映出对行业发展中问题的敏锐洞察和对未来的积极规划。
在初中数学教学领域,评课和课后小结是两个不可或缺的环节,它们对于提升教学效果和学生学习成效具有至关重要的作用。评课是对教师授课质量的全面评价,同时也是教师专业成长的重要途径。评课文化的核心在于通过积极...
### 四轴飞行器DIY小结:从理论到实践的探索 四轴飞行器,以其机械结构简单、易于上手的特点,成为了无人机爱好者和航模玩家的理想选择。本文将根据给定文件中的标题、描述及部分内容,深入探讨四轴飞行器DIY的关键...
- 第10章 jQuery+Bootstrap整合开发 电子拍卖系统- 10.7 本章小结10.7 本章小结本章介绍了一个完整的前端开发+后端整合项目,内容覆盖
此外,北控水务还致力于打造一个金融控股服务平台,通过提供资产管理、资产证券化等服务,进一步整合资源,提高金融服务效率。 #### 六、固废处理处置 北控水务还涉足固废处理领域,目标是成为国际领先的固废处理...
一共四个,其中pdf 三个包,源码一个包 第一章 J2EE快速入门 1.1 J2EE概述 1.1.1 J2EE的来源 1.1.2 J2EE整体框架 1.1.3 从J2EE到JavaEE 1.2 J2EE组件 1.2.1 客户端组件 1.2.2 Web组件 ...15.13 小结
这份PPT课件是针对高一数学必修课程的知识整合与阶段检测,主要涵盖了四个核心主题:函数的进一步认识、函数的单调性、二次函数性质的再研究以及简单的幂函数。下面将对这些知识点进行详细阐述。 首先,我们讨论...
第12章 基于annotation注解技术的ssh 2整合开发 12.1 初识annotation522 12.2 亲身体验annotation的威力524 12.3 struts 2.x的annotation插件convention-plugin528 12.4 hibernate 3.x的annotation解决...12.6 小结563
但根据文件标题“水利工程毕业个人实习小结-5页.pdf”,可以推测文档应当涉及以下知识点和概念,这些都是水利工程师在实习期间可能会经历和需要了解的内容。 1. 水利工程基本概念:水利工程建设的目标、原则和作用...
##### 1.6 小结 完成以上步骤后,就具备了开始JSF项目的基本条件。接下来可以深入学习JSF的具体用法。 #### 二、JSF应用实践 ##### 2.1 JSF准备 在开始JSF项目之前,需要准备好开发环境。这包括配置JDK、安装...
### 小结 通过以上分析可以看出,SSH整合不仅涉及各个框架的基础知识点,还需要对这些框架如何协同工作有深入的理解。例如,如何在Spring中配置数据源以供Hibernate使用,如何在Struts2中正确配置Action等。此外,...
《现代SOC设计技术》的学习小结涵盖了多个关键领域,揭示了这一复杂技术的各个方面。SOC,即System on Chip,是将系统的关键组件集成在单个芯片上的技术,其发展源于市场对高集成度集成电路的需求以及微电子技术的...
一共四个,其中pdf 三个包,源码一个包 第一章 J2EE快速入门 1.1 J2EE概述 1.1.1 J2EE的来源 1.1.2 J2EE整体框架 1.1.3 从J2EE到JavaEE 1.2 J2EE组件 1.2.1 客户端组件 1.2.2 Web组件 ...15.13 小结
S2SH指的是Struts2、Spring和Hibernate三个框架的整合,用于构建Web应用程序。在Spring 3.2中,整合这三个框架更加顺畅。Spring可以作为它们之间的胶水,管理各层之间的依赖和事务。例如,通过`@Autowired`注解,...
【标题】和【描述】提及的是两个不同的培训项目个人小结,主要涵盖了参与者的培训体验和收获。这两个小结都强调了教育技术和信息技术在教学中的重要性,以及培训对个人专业成长的影响。 【标签】“资料”表明这些...