Integrated HIBERNATE4+SPRING4+STRUTS2
1. Project diagram
2, first we introduce the corresponding jar package
3, we configure the corresponding Web. xml
When we're integrating struts2, we're going to pull things out of the web, so we have to configure XML.
<web-app xmlns= "Http://java.sun.com/xml/ns/javaee" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version= "3.0" > <!--Instantiate a spring container in a Web container--<context-param> <param-name>contextconf Iglocation</param-name> <param-value>classpath:config/spring/beans.xml</param-value><!-- This specifies spring's configuration file, which, by default, looks for a configuration file from the Web root directory, which we can specify from the Classpath by using the classpath: prefix specified by spring-</context-param> <!-- Instantiate a spring container--<listener> <listener-class>org.springframework.web.context.contextloaderlistener& lt;/listener-class> </listener> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <!--<init-param> <p aram-name>config</param-name> <param-value>struts-default.xml,struts-plugin.xml,classpath:conf Ig/struts/struts.xml</param-value>struts placed directory </init-param> </filter> <filter-m apping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </f ilter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welco Me-file-list></web-app>
which
<!--instantiate a spring container in a Web container-- <context-param> <param-name>contextconfiglocation</ param-name> <param-value>classpath:config/spring/beans.xml</param-value><!-- This specifies spring's configuration file, which, by default, looks for a configuration file from the Web root directory, which we can specify from the classpath with the classpath: prefix specified by spring- </context-param>
To configure spring's location for the web
Implement a listener
<!--instantiating spring containers-- <listener> <listener-class> Org.springframework.web.context.contextloaderlistener</listener-class> </listener>
Configure STRUTS2
<filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter-class><!-- < init-param> <param-name>config</param-name> <param-value>struts-default.xml, Struts-plugin.xml,classpath:config/struts/struts.xml</param-value>struts placed directory </init-param> - </filter>
No matter what end of page you want to go through struts2
<filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</ Url-pattern> </filter-mapping>
4, we configure the spring configuration file Beans.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: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-4.1.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-4.1.xsd http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/ Spring-aop-4.1.xsd http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/ Spring-tx-4.1.xsd "> <!--scan classes with spring special mechanism, which is to add all the classes under these packages to spring for management--<context:component-scan BAS E-package= "Com.cutter_point"/> <!--Property Walker--<!--<context:property-placeholderlocation= "Classpath:jdbc.properties"/>-<!--Connection Database property configuration, destroy-method= "Close" This means that the bean's default Close method can be called when the bean is destroyed--<bean id= "myDataSource" class= " Org.apache.commons.dbcp2.BasicDataSource "destroy-method=" Close "> <property name=" driverclassname "value=" Oracle.jdbc.driver.OracleDriver "/> <property name=" url "value=" Jdbc:oracle:thin: @localhost: 1522:orcl "/> <property name= "username" value= "xf1205020116"/> <property name= "password" value= "xf1205020116"/> <!--initial value when connection pooling starts--<property name= "InitialSize" value= "1"/> <!--connection pool maximum dbcp2 It doesn't seem to be--&. Gt <!--<property name= "maxactive" value= "$"/>-<!--maximum idle value. After a peak time, the connection pool can slowly release a portion of the connection that has not been used, and has been reduced to Maxidle---<property name= "Maxidle" value= "2"/> <!--minimum idle value. When the number of idle connections is less than the threshold, the connection pool will pre-apply for some connections to avoid peaks Late request-<property name= "Minidle" value= "1"/> </beaN> <!--hibernate two cache configuration--<bean id= "sessionfactory" class= "Org.springframework.orm.hibernate4.LocalS Essionfactorybean "> <!--configuration elided for brevity and <property name=" DataSource "ref=" Mydat Asource "/> <!--<propertyname=" mappingresources "> <list> mapping File <val Ue>com/cutter_point/bean/product/producttype.hbm.xml</value> </list> </property>--& Gt <property name= "Hibernateproperties" > <!--to configure hibernate properties--<value> hi Bernate.dialect=org.hibernate.dialect.oracledialect Hibernate.hbm2ddl.auto=update <!--other values create, creat E-drop, update, validate none--> hibernate.show_sql=true hibernate.format_sql=true <!--turn on the level two cache function-hibernate.cache.use_second_level_cache= true Hibernate.cache. Use_query_cache= false hibernate.cache.region.factory_class= Org.hibernate.cache.ehcache.EhCacheRegionFactory <!--Hibernate3 's level two cache configuration-<!--<property name= "Hibernate.cache.provider_class" >org.hi bernate.cache.ehcacheprovider</property>--> </value> </property> <proper Ty name= "Packagestoscan" value= "Com.cutter_point.bean"/> </bean> <!--transaction Manager, the above configured bean is injected into this-- <bean id= "Txmanager" class= "Org.springframework.orm.hibernate4.HibernateTransactionManager" > <property Name= "Sessionfactory" ref= "Sessionfactory"/> </bean> <!--we use annotations to enable this transaction, first we open the transaction--<t X:annotation-driven transaction-manager= "Txmanager"/> <!--action to spring hosting-<!--<bean id= "PR Oducttypeaction "class=" Com.cutter_point.web.action.product.ProductTypeAction "scope=" prototype "/>--</ Beans>
Note that there is no need to write the corresponding XML file in our spring configuration file.
We can do this. The action is given to spring hosting, which is our controller, which is a servlet in the Java web, where a class inherits a class of Actionsupport as the action
<!--<beanid= "producttypeaction" class= "Com.cutter_point.web.action.product.ProductTypeAction" scope= " Prototype "/>--
5, create the controller, our page interactive class
Producttypeaction.java
/** * Function: This is the realization of the product class and Web layer interaction * Time: May 16, 2015 10:50:36 * file: Producttypeaction.java * cutter_point */packagecom.cutter_ Point.web.action.product; Import Java.util.Map; Import Javax.annotation.Resource; Importorg.springframework.context.annotation.scope;importorg.springframework.stereotype.component; Importcom.cutter_point.bean.product.producttype;importcom.cutter_point.service.product.producttypeservice; Importcom.opensymphony.xwork2.actioncontext;importcom.opensymphony.xwork2.actionsupport; @Namespace ("/product") @Component @scope ("prototype") public class Producttypeaction extendsactionsupport{@Resourc E//Dependency injection privateproducttypeservice producttypeservice; @Override publicstring Execute () throws Exception {maprequest = (MAP) actioncontext.getc Ontext (). Get ("request"); if (producttypeservice== null) {System.out.println ("???"); } PRoducttypept = Producttypeservice.find (Producttype.class, 88); Request.put ("ProductType", PT);//System.out.println (PT); return "List"; } publicproducttypeservice Getproducttypeservice () {returnproducttypeservice; } publicvoid Setproducttypeservice (Producttypeservice producttypeservice) {This . producttypeservice= Producttypeservice; }}
6. Our JSP page
<%@ page iselignored= "false" Language= "java" import= "java.util.*" pageencoding= "utf-8"%><%string path = Request.getcontextpath (); String BasePath =request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; System.out.println (BasePath); ProductType pt = (producttype) request.getattribute ("ProductType"); %> <! DOCTYPE HTML Public "-//w3c//dtdhtml 4.01 transitional//en" >
7, Next we test whether STRUTS2 integration successWe visit this website
Http://localhost:8080/babaSport_0600_sh_struts2/product/type
Notice why we're writing URLs like this.
Because that's what happens when we're in the struts2.
<package name="Product"namespace="/product" extends= "struts-default" >
Notice the namespace, that's our namespace, about namespaces.
Name spaceThe namespace determines the access path to the action, which defaults to "", which can accept action for all paths
Amespace can be written as/, or/xxx, or/XXX/YYY, the corresponding action access path is/index.action,/xxx/index.action, or
by/xxx/yyy/index.action
Namespace it is best to also use a module to name
Annotations:
Package : Used to distinguish between duplicate names.
namespace : Must be used "/" the beginning, such as: /front , in URL We are going to enter: Http://localhost:8080/babaSport_0600_sh_struts2/product/type
the package and namespace are named in the module when they are developed.
Result: all Name= "Success" of the result, can not write: Name= "Success" that is:<result>/namespace.jsp</result>
There are also values that return a different value.
<result name= "List" >/test.jsp</result>
6. SummaryThe key to integrating STRUTS2 is the introduction of STRUTS2 's jar package, which is already mapped, and then what is the configuration format of struts2, and how does spring integrate into it,Finally, be sure to note that you should remember to configureWeb. XML, Bo Master hasNTimes toWeb. XMLto forget, the results reported variousBug, inexplicably, to the last dawned of the original wasWeb. XMLproblems, beginners generally have this problem, especially like to useMyEclipsethe classmate, the formerJavawebWriteservletthe timeMyEclipsewill help us automatically.servletadded toWeb. XMLinside, after using the frame we don't have to configureservlet, but as on the point still to write inWeb,xml.
"Java EE Spring" 31, BA-BA Sports network integration S2sh