Five struts and spring integrations for SSH integration

Source: Internet
Author: User
Tags aop

1. First, we need to analyze first, our spring container in the Web environment, only need a copy of it.

In addition, it is our spring container, to be created at the time of our Tomcat launch (including its spring objects), how can we guarantee that our spring container will be created?

We can work with the listener to create our spring container, and then how do we implement our listener?

When ServletContext is created, it means that Tomcat starts up normally, we use listeners to listen to our servletcontext, if the creation succeeds, load the configuration file, create the spring container

2. We will first add our listener to our web. xml

<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee/http Java.sun.com/xml/ns/javaee/web-app_2_5.xsd "id=" webapp_id "version=" 2.5 "> <display-name>ssh_spring</ Display-name> <!--Configure spring's listener to load the spring configuration file, create our Spring object according to the configuration file the default load spring configuration file location: WEB-inf under the Applicationcontext.xml-<listener> <listener-class>org.springframework.web.context.contextloaderlistener</ Listener-class> </listener><!--Modify the path of the configuration file that spring loads by default--<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpa Th:applicationcontext.xml</param-value> </context-param><filter> <filter-name>ssh</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter-class> </filter> <filter-mapping> <filter-name>ssh</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</ Welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</ Welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</ Welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list></web-app> 

3. Get objects in our struts based on containers

The first is a plug-in package that uses struts (less used in this way)

The second is to give the struts action class to spring to manage

In Applicationcontext.xml

<?xml version= "1.0" encoding= "UTF-8"?><!--Spring configuration file: Import constraint--><beans xmlns= "/http Www.springframework.org/schema/beans "Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://www.springframework.org/schema/tx"xsi:schemalocation="http//Www.springframework.org/schema/beanshttp//www.springframework.org/schema/beans/spring-beans.xsdhttp//Www.springframework.org/schema/contexthttp//www.springframework.org/schema/context/spring-context.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp//www.springframework.org/schema/aop/spring-aop.xsdhttp//Www.springframework.org/schema/txhttp//www.springframework.org/schema/tx/spring-tx.xsd> <!--custom Java objects to be managed by spring<bean id= "customeraction" scope= "prototype" class= "Com.itheima.action.CustomerAction" > <property name= "CustomerService" ref= "customerservice" ></property> </bean><bean id= "CustomerService"class= "Com.itheima.service.impl.CustomerServiceImpl" > <property name= "Customerdao" ref= "Customerdao" &GT;&LT;/PR operty> </bean> <bean id= "Customerdao"class= "Com.itheima.dao.impl.CustomerDaoImpl" > <property name= "hibernatetemplate" ref= "Hibernatetemplate" >&lt   ;/property> </bean> <!--java objects in a third-party jar package are assigned to spring to manage-<!--Create a Hibernatetemplate object- <bean id= "Hibernatetemplate"class= "Org.springframework.orm.hibernate5.HibernateTemplate" > <!--injected sessionfactory--<property N Ame= "Sessionfactory" ref= "sessionfactory" ></property> </bean> <!--creating Sessionfactory objects--<! --when spring and hibernate are integrated, this Sessionfactory implementation class is provided by spring: Localsessionfactorybean, Creating Sessionfactory is based on Hibernate's core configuration file--<bean id= "Sessionfactory"class= "Org.springframework.orm.hibernate5.LocalSessionFactoryBean" > <property name= "configlocation" value= "Clas Spath:hibernate.cfg.xml "></property> </bean> <!--configuring hibernate transaction Management--<bean id=" Transacti Onmanager "class= "Org.springframework.orm.hibernate5.HibernateTransactionManager" > <property name= "sessionfactory" ref= "se Ssionfactory ></property> </bean> <!--Configuring transaction management notifications-<tx:advice id= "Txadvice" > & lt;tx:attributes> <tx:method name= "*" propagation= "REQUIRED" read-only= "false"/> <   Tx:method name= "get*" propagation= "SUPPORTS" read-only= "true"/> </tx:attributes> </tx:advice> <!--Configure the transaction AOP--<aop:config> <aop:pointcut expression= "Execution (* Com.itheima.service.impl. *.*(..))" Id= "pt"/> <aop:advisor advice-ref= "Txadvice" pointcut-ref= "pt"/> </aop:config></beans>

Configuring in Struts.xml

<!DOCTYPE Struts public"-//apache software foundation//dtd Struts Configuration 2.3//en" "Http://struts.apache.org/dtds/struts-2.3.dtd" > <struts> < PackageName= "Customer"extends= "Struts-default" namespace= "/customer" > <!--1.class: Fully qualified class name reflection for instructions to create an action class objectclass: The unique identity of the bean that needs to be modified as the action class in the container--<action name= "Addcustomerui"class= "Customeraction"method= "Addcustomerui" > <result name= "Success" >/jsp/customer/add.jsp</result> </action > <action name= "Getallcustomer"class= "Customeraction" method= "Getallcustomer" > <result name= "Success" >/jsp/customer/list.jsp</result&gt        ; </action> </ Package></struts>

We're in customeraction.

 Packagecom.itheima.action;Importjava.util.List;ImportCom.itheima.entity.Customer;ImportCom.itheima.service.CustomerService;ImportCom.opensymphony.xwork2.ActionSupport;ImportCom.opensymphony.xwork2.ModelDriven; Public classCustomeractionextendsActionsupportImplementsModeldriven<customer> {    PrivateCustomer customer =NewCustomer (); PrivateCustomerService CustomerService; PrivateList<customer>customers;  Public voidSetcustomers (list<customer>customers) {         This. Customers =customers; }     Public voidSetcustomerservice (CustomerService customerservice) { This. CustomerService =CustomerService; }         PublicList<customer>GetCustomers () {returncustomers; }     PublicCustomer Getmodel () {returncustomer; }        //go to add page     PublicString Addcustomerui () {return  This.    SUCCESS; }    //go to the list page     PublicString Getallcustomer () {Customers=Customerservice.getallcustomer (); return  This.    SUCCESS; }     }

Finally, test our results and visit the http://localhost:8080/ssh_spring/

Make a little progress every day, come here today!

Five struts and spring integrations for SSH integration

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.