SSH---integration struts2+spring+hibernate (i)

Source: Internet
Author: User
Tags i18n


SSH---integration struts2+spring+hibernate (i)

This scenario allows spring to create a Struts2 action that does not allow spring to fully manage the dependent objects in the Struts2 action,struts2action, which is automatically assembled by name by default

1. Create a Web project

2, introduce Struts2 dependency package, copy the dependent package to Web-inf/lib

*commons-logging-1.0.4.jar*freemarker-2.3.15.jar*ognl-2.7.3.jar* struts2-core-2.1.8.1.jar*xwork-core-2.1.6.jar* Commons-fileupload-1.2.1.jar

3, introduce spring's dependency package, copy the dependent package to Web-inf/lib

*spring.jar*lib/aspectj/*.jar


4, the introduction of Hibernate-dependent packages, the dependency package copy to Web-inf/lib

*hibernate3.jar*lib/*.jar

5. Database Driver

*mysql JDBC Driver

6. Copy the STRUTS2 and spring-integrated dependency packages to Web-inf/lib

*struts2-spring-plugin-2.1.8.1.jar

7. Configure Strutsprepareandexecutefilter in the Web. xml file

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

8, provide the Struts.xml configuration file, provide the configuration of the necessary properties

*struts.i18n.encoding=gb18030* Struts.configuration.xml.reload=true*struts.devmode=true  

9. Provide spring configuration file

*applicationcontext-service.xml*applicationcontext-dao.xml*applicationcontext-common.xml

10, provide hibernate.cfg.xml configuration file, provide log4j


11. Configure spring Contextloaderlistener in the Web. xml file to create Beanfactory

   <context-param>           <param-name>contextConfigLocation</param-name>           <!--           < Param-value>classpath:applicationcontext-*.xml</param-value>            -           <param-value> classpath:applicationcontext.xml</param-value>   </context-param>       <listener>           < Listener-class>org.springframework.web.context.contextloaderlistener</listener-class>    </ Listener>

12. Configure Opensessioninviewfilter in the Web. xml file (need to be placed in front of Struts2 filter)

<filter><filter-name>OpenSessionInViewFilter</filter-name><filter-class> Org.springframework.orm.hibernate3.support.opensessioninviewfilter</filter-class></filter> < filter-mapping><filter-name>opensessioninviewfilter</filter-name><url-pattern>/*</ Url-pattern></filter-mapping>


13. Provide user to add form and add_success.jsp


14. Create user entity class, write Hibernate mapping file, add mapping file to Hibernate.cfg.xml

<mapping resource= "/com/tgb/usermgr/domain/user.hbm.xml"/>

15, the establishment of Userdao and userservice, and to achieve

Userdao

Userdao interface Package Com.tgb.usermgr.dao;import Com.tgb.usermgr.domain.User; Public Interfaceuserdao {public    void Add (user user);} Userdao realizes Packagecom.tgb.usermgr.dao.impl; Importorg.springframework.orm.hibernate3.support.HibernateDaoSupport; Importcom.tgb.usermgr.dao.userdao;importcom.tgb.usermgr.domain.user; Publicclass Userdaoimpl extends Hibernatedaosupport implements Userdao {public    void Add (user user) {      Gethibernatetemplate (). Save (user);}       }


UserService

UserService interface Package Com.tgb.usermgr.service; Import Com.tgb.usermgr.domain.User; Public Interfaceuserservice {    public  void Add (user user);}// UserService realizes Packagecom.tgb.usermgr.service.impl; Importcom.tgb.usermgr.dao.userdao;importcom.tgb.usermgr.domain.user;importcom.tgb.usermgr.service.userservice; Publicclass Userserviceimpl implements UserService {    private Userdao Userdao;     public void Add (user user) {      userdao.add (user);   }    public void Setuserdao (Userdao userdao) {      This.userdao = Userdao;   }}

16. Build STRUTS2 action and configure it to Struts2 configuration file

<struts>   <constant name= "struts.i18n.encoding" value= "GB18030"/> <constant name=   " Struts.configuration.xml.reload "value=" true "/>   <constant name=" Struts.devmode "value=" true "/>   <!--automatically assemble dependent objects in action by name, now modified to type---   <constant name= "Struts.objectFactory.spring.autoWire" value = "type"/>   <package name= "user-package" extends= "Struts-default" namespace= "/user" >      <action Name= "Add" class= "Com.tgb.usermgr.web.action.AddUserAction" >        <result>/add_success.jsp</result >      </action>   </package></struts>


17. Invoke action in JSP

<body>   <form action= "user/add.action" >     user code: <input type= "text" name= "User.usercode" > <br>     user name: <input type= "text" name= "User.username" ><br>     Age: <input type= "text" Name= " User.age "><br>         <input type=" Submit "value=" Add ">   </form>  </body>

Finally attach the frame download link: http://download.csdn.net/detail/senior_lee/9059887


Copyright NOTICE: This article for Bo Master original article, not by Bo Master permission to mention the link can be reproduced.

SSH---integration struts2+spring+hibernate (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.