In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how Struts+Hibernate+Spring can be used together. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
In fact, for the combination of S2SH, you can pay attention to a few points:
First: because the Action of Struts2 is stateful, and the Bean in Spring defaults to singleton mode, be sure to set the scope of the Action configured with Struts2 in Spring to prototype. Action in Struts1 is stateless, so you don't have to set Spring to prototype when you SSH!
Second: unlike Struts1, the combination of Struts2 and Spring requires a struts2-spring-plugin-2.1.6.jar in a Struts2 and no other configuration in the struts.xml, just change the value of the class of the action in the struts.xml to the id of the Bean configured by the action in the Spring.
A simple example of Struts+Hibernate+Spring is as follows:
(this example includes most of the content of Struts2, mainly to experience the functions of Struts2.)
The database is as follows: uid,ufristName,ulastName,uage four fields
First, join the support of Spring
Second, join the Hibernate support (the configuration of Hibernate is managed by Spring)
Third, join the support of Struts2
Fourth, add Struts2 plug-in to Spring (struts2-spring-plugin-2.1.6.jar)
Add the configuration of Spring and Struts2 to web.xml:
< ?xml version="1.0" encoding="UTF-8"?> < web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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> < listener> < listener-class>Org.springframework.web.context.ContextLoaderListener
< /listener-class> < /listener> < filter> < filter-name>Struts2
< /filter-name> < filter-class>Org.apache.struts2.dispatcher.FilterDispatcher
< /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>The APIs of UsersDAO are as follows:
Public interface UsersDaoInter {public abstract void save (Users transientInstance); public abstract void delete (Users persistentInstance); public abstract void delete (final int uId); public abstract Users findById (java.lang.Integer id); public abstract List findByExample (Users instance); public abstract List findByProperty (String propertyName, Object value); public abstract List findAll (); public abstract Users merge (Users detachedInstance);}
The API UsersService is as follows:
Public interface UsersServiceInter {public abstract void save (Users transientInstance); public abstract void delete (Users persistentInstance); public abstract void delete (final int uId); public abstract Users findById (java.lang.Integer id); public abstract List findByExample (Users instance); public abstract List findByProperty (String propertyName, Object value); public abstract List findAll (); public abstract Users merge (Users detachedInstance) / * Export the Excel * @ return * / public abstract InputStream exportUsers () of the user list;}
The applicationContext.xml configuration for Spring is as follows:
< ?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/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd "> < !-- 数据源 --> < bean id="MyDataSource" class="org.apache.commons.dbcp.BasicDataSource"> < property name="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"> < /property> < property name="url" value="jdbc:microsoft:sqlserver://localhost:1433"> < /property> < property name="username" value="sa"> < /property> < /bean> < !-- SessionFactory配置 --> < bean id="MySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> < property name="dataSource"> < ref bean="MyDataSource"> < /ref> < /property> < property name="hibernateProperties"> < props> < prop key="hibernate.dialect">Org.hibernate.dialect.SQLServerDialect
< /prop> < /props> < /property> < property name="mappingResources"> < list> < value>Com/mengya/entity/Users.hbm.xml
< /value> < /list> < /property> < /bean> < !-- 事务管理器 --> < bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> < property name="sessionFactory"> < ref bean="MySessionFactory"> < /ref> < /property> < /bean> < !-- 定义事务的属性 --> < tx:advice id="txAdvice" transaction-manager="transactionManager"> < tx:attributes> < tx:method name="save*" propagation="REQUIRED" /> < tx:method name="delete*" propagation="REQUIRED" /> < tx:method name="merge*" propagation="REQUIRED" /> < tx:method name="*" read-only="false" /> < /tx:attributes> < /tx:advice> < !-- 使用Spring的AOP管理Hibernate的Transaction --> < aop:config> < !-- 定义AOP切面 --> < aop:pointcut id="allManagerMethod" expression="execution(* com.mengya.service.*.*(..))" /> < !-- 在AOP切面中配置事务 --> < aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod" /> < /aop:config> < !-- 如下是DAO,Service,Action的配置 --> < bean id="UsersDAO" class="com.mengya.dao.imple.UsersDAO"> < property name="sessionFactory"> < ref bean="MySessionFactory"> < /ref> < /property> < /bean> < bean id="userService" class="com.mengya.service.imple.UsersService"> < property name="usersdao"> < ref bean="UsersDAO"> < /ref> < /property> < /bean> < !-- 对于Struts2与Spring集成时,Strtus2的Action是有状态的故这个的scope必须为prototype,Spring默认的是单例模式 --> < bean id="saveUserAction" class="com.mengya.usersAction.SaveUsersAction" scope="prototype"> < property name="usersService"> < ref bean="userService"> < /ref> < /property> < /bean> < bean id="listUserAction" class="com.mengya.usersAction.ListUsersAction" scope="prototype"> < property name="userService"> < ref bean="userService"> < /ref> < /property> < /bean> < bean id="deleteUserAction" class="com.mengya.usersAction.DeleteUsersAction" scope="prototype"> < property name="usersService"> < ref bean="userService"> < /ref> < /property> < /bean> < bean id="updateUserAction" class="com.mengya.usersAction.UpdateUsersAction" scope="prototype"> < property name="userService"> < ref bean="userService"> < /ref> < /property> < /bean> < bean id="exportUsersAction" class="com.mengya.usersAction.ExportUsersAction" scope="prototype"> < property name="userService"> < ref bean="userService"> < /ref> < /property> < /bean> < /beans>The struts.xml configuration is as follows:
< !DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> < struts> < package name="mengya" extends="struts-default"> < action name="saveUser" class="saveUserAction"> < result name="success" type="redirectAction">ListUser.action
< /result> < result name="input">/ add2.jsp
< /result> < /action> < action name="listUser" class="listUserAction"> < result name="success">/ userList.jsp
< /result> < /action> < action name="deleteUser" class="deleteUserAction"> < result name="success" type="redirectAction">ListUser.action
< /result> < /action> < action name="getUser" class="updateUserAction" method="getUserById"> < result name="success">/ view.jsp
< /result> < /action> < action name="updateUser" class="updateUserAction" method="updateUser"> < result name="success" type="redirectAction">ListUser.action
< /result> < result name="input">/ view.jsp
< /result> < /action> < !-- 将用户信息用Excel导出 --> < action name="exportUsers" class="exportUsersAction"> < result name="success" type="stream"> < param name="contentType">Application/vnd.ms-excel
< /param> < !-- 对于第一个参数默认值为inline这样的话若在线打开的话会生成两个xls文件 --> < param name="contentDisposition">Attachment;filename= "allUsers.xls"
< /param> < param name="inputName">DownloadFile
< /param> < /result> < /action> < /package> < /struts>Jsp pages: user list
< table border="1" align="center" width="60%"> < tr> < td colspan="6" align="center"> < s:text name="UserListInfo"> < /s:text> < /td> < /tr> < tr> < td> < s:text name="UId"> < /s:text> < /td> < td> < s:text name="UFristName"> < /s:text> < /td> < td> < s:text name="ULastName"> < /s:text> < /td> < td> < s:text name="UAge"> < /s:text> < /td> < td> < s:text name="UpdateOperaction"> < /s:text> < /td> < td> < s:text name="DeleteOperaction"> < /s:text> < /td> < /tr> < s:iterator value="#request.listUser" var="user"> < tr> < td> < s:property value="#user.uid"/> < /td> < td> < s:property value="#user.ufristName"/> < /td> < td> < s:property value="#user.ulastName"/> < /td> < td> < s:property value="#user.uage"/> < /td> < td> < s:a href="getUser.action?users.uid=%{#user.uid }"> < s:text name="UpdateOperaction"> < /s:text> < /s:a> < /td> < td> < s:a href="deleteUser.action?uid=%{#user.uid }"> < s:text name="DeleteOperaction"> < /s:text> < /s:a> < /td> < /tr> < /s:iterator> < /table> < tr/> < tr/> < tr/> < div align="center"> < s:a href="exportUsers.action">Excel export
< /s:a> < /div>User information modification page:
< s:form action="updateUser" method="post"> < s:hidden name="users.uid"> < /s:hidden> < s:textfield name="users.ufristName" key="UFristName"> < /s:textfield> < s:textfield name="users.ulastName" key="ULastName"> < /s:textfield> < s:textfield name="users.uage" key="UAge"> < /s:textfield> < s:submit key="submit"> < /s:submit> < /s:form>Thank you for reading! This is the end of the article on "how to combine Struts+Hibernate+Spring". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.