believe that SSH no one does not know it, struts2+spring+hibernate, enterprise development of the "foundation" framework, why the basis of the quotation marks, because this foundation is only a lot of people think, the most basic is the servlet, many training institutions are not taught many foundations, Directly up to three frames, SSH or SSI, make a lot of people think Javaweb development must frame, no frame with no hand. There is no harm in the three frameworks, practical, many companies are using, direct development. But graduation thought really for a long time did not use three big frame, spring is useful, especially SPRINGMVC, feel more than struts2 more cool. In fact, think about it, spring is absorbing some of the advantages of struts, plus some restful things, feel much better. But today we don't look at the SPRINGMVC, and later we take the time to look at Springmvc's elegant place.
Get to the point. SSH has been updated version, here I would like to say a version, or a bunch of friends will scold me, struts2 I use is 2.3.4,spring is the latest 3.2.2,hibernate is 4.1.9, or quite new ah.
Before entering the code, let's take a look at the roles that the next three frameworks play.
1 Struts: Why do we use struts to compare with a servlet? In fact, we can still do MVC without struts, but it may be more depressing on the configuration file. Struts primarily helps us implement a function of distribution, struts1 our specific requests into a specific class, and helps us to set the properties. X is done by Actionform). Struts2 is much more advanced than struts1, automatically setting values, not enforcing or inheriting classes, and having a series of concepts such as request chains. As these are not used much, lest you mislead the brothers, we will not say much more.
2 Spring: I do not know that spring's name does not engage in Java. Many people contact spring at the beginning should also be through its IOC understand it, or directly do not know, on the three main frame ssh. It's okay, anyway. When we use the three big frames, spring is largely a tool of the Bond class, and with the other two frameworks, Ssi,ssh, spring just provides the tools to make it easier for us to use the three broad frameworks. Of course, the framework combination of a large program also relies on the spring of the IOC, in addition, the transaction we will certainly use. AOP These more advanced things, depends on the need, such as what log requirements, interception requirements, with AOP can be achieved better.
3 Hibernate:hibernate is also famous in the Java world, and is basically an ORM standard. It provides caching, level one and level two, and there are hql, three big frames when combined, what do we use it for? Of course, ORM is the main function of this mapping, the cache we do not consider for the time being. A lot of people don't think about why you need ORM. In fact, mainly because of the data field and the conflict between classes, if the use of JDBC to operate, a field to set, estimated to do a long time, people will be crazy, so then Orm appeared.
The three main frameworks are responsible for the following: struts2--is responsible for request forwarding and the corresponding processing of forms, the spring--class organization (i.e. IOC), manages the action by Struts2 Management as a bean, hibernate--orm mapping, Maps a class to a table.
learned about the division of labor, of course, began our coding. The three major framework is more troublesome is the package, many people like to use myeclipse mainly because it can help us to import the three-frame package. But suggest novice still do not use that, one is myeclipse have own project structure, get eclipse A guide also want to set some project facet and so on, bring inconvenience to others; the second is that the company rarely use myeclipse, or familiar with eclipse better. Or it's good to be interested in idea.
The necessary packages for STRUTS2 are as follows: Antlr,asm,xwork,struts2-core,ognl,common-logging,common-fileupload,struts-spring-plugin, probably on these few, may not be complete, you can start the times in the wrong situation Plus, do not recommend to throw all the bags, the three frame some of the package is a conflict.
Spring3 needs the following packages: Spring-beans,spring-core,spring-context,spring-context-support,spring-expression, Spring-orm (We use three broad frameworks that require ORM support), SPRING-WEB,SPRING-TX (we use the transaction, but not in the example).
Hibernate4 needs the following package: Hibernate download all jar packages in the required folder in the package.
Maybe these bags are ok, or that sentence, do not suggest to put all the package, because the three frame some of the package is conflict, in need of the time as appropriate, plus, when the classnotfound to the corresponding jar package, put into the Lib directory.
This is the end of the preparations, we have officially started.
1 First, we need to use STRUTS2, we must let him intercept the request
<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>
This code allows struts to intercept all requests, of course, not so that all requests are blocked by spring, and we can configure struts.xml so that struts blocks a particular suffix name, usually action. As follows:
<constant name= "struts.objectfactory" value= "Spring"/> <constant "name="
value = "Action"/>
Struts.action.extension has been configured to intercept the default suffix name, so that when the interception will check the suffix name, the match will be forwarded by struts. Instead, struts.objectfactory represents the forwarding processing class for struts to be managed by spring, which is managed as a bean.
2 When we use spring directly, we call *applicationcontext, but we are now in the Web case, we can't call it manually. In fact, Spring provides a method to invoke in the Web, there is a servlet (which I used, I'm not sure how to use), a listener,servlet is for the application server that does not support the filter, But now we are basically using listener.
<listener>
<listener-class>org.springframework.web.context.contextloaderlistener</ Listener-class>
</listener>
In this case, most of the scenarios that spring needs to be combined with other frameworks, when we're just using SPRINGMVC, we can directly configure Dispatcherservlet to replace the top struts servlet. Specifically we later use SPRINGMVC time to see.
In this case, the general will go to load the Web-inf directory of the Applicationcontext.xml file, when your filename is not this, or the path is not here, you can configure the Configcontextpath property
<context-param>
<param-name>configContextPath</param-name>
<param-value> Web-inf/applicationcontext.xml</param-value>
</context-param>
It supports the use of classpath: prefixes, and so on, here's a little more to see the spring configuration file in detail. So in fact we've combined struts with spring, but not some bean configurations and struts forwarding.
3 and as an ORM hibernate, this is also managed by spring. Spring provides a localsessionfactory, note that this class, Hibernate3 and 4 are different, and spring offers two. The following section is configured in Applicationcontext.xml
<bean id= "Sessionfactory" class= "Org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
< Property Name= "DataSource" ref= "DataSource"/> <property name=
"mappingresources" >
<list>
</list>
</property>
<property name= "hibernateproperties" >
<props>
</props>
</property>
In fact, the configuration is similar to the hibernate, there is not much to say the specific code.
4 in the previous spring2.5.x, we used the most is hibernatedaosupport, but spring3.x started not to provide this support, we need to use the original session of the operation (only need to inject sessionfactory), but here is actually involved And to a problem, if we open each request to turn off the connection, it will consume resources, but if not close the connection, it will not be good. So you need to have a compromise solution with a manager to manage the connection. Here Spring provides a opensessioninview that opens the session every time a view is opened (basically every request), and how it is managed internally is a temporary matter.
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class> Org.springframework.orm.hibernate4.support.opensessioninviewfilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
< Url-pattern>/*</url-pattern>
</filter-mapping>
Here or let him intercept all requests, of course, can be configured to intercept only the action suffix, there is no need to waste resources.
5 Basically, our SSH framework is set up, and then it's just a matter of code. Just configure the Bean in the spring configuration file, or if you like annotation you can directly @component (value= ' bean name '), but remember to open Component-scan in the configuration file.
The code is not written in detail, roughly as follows:
@Component (value= "useraction") Public
class useraction{}
<package name= "user" extends= "Struts-default" namespace= "/user" >
<!--user login-->
<action name= "Login" class= "useraction" method= "Login" >
<result name= "ERROR" >/pages/login.jsp</result>
</action>
</package>
actually means to modify the point in class to point to the bean.
Code details are not posted, after all, is the summary post. In general, SSH is good for code within a team, because the code written under the framework's specification basically has a particular form in it, and it's better for later maintenance. But for novice friends, it is not recommended to rely too much on the framework, really need to use, at least in general to understand what the framework has done, what circumstances need to use it, and when not to use.