I believe that no one doesn't know about SSH. struts2+spring+hibernate, the "basic" architecture of enterprise development, why is the basics marked in quotes? Because this foundation is just what many people think of it. The most basic is servlet. Many training institutions do not teach many basics, and they will have three major frameworks directly. SSH or SSI, which makes many people think that JAVAWEB development must have frameworks. Without a framework, it is the same as losing hands. There is no harm in the three major frameworks and they are practical. Many companies are using them and can directly start developing them. But after graduation, I thought I hadn't used the three major frameworks for a long time. Spring is useful, especially springMVC. It feels much more exciting to use than struts2. Actually, if you think about it, spring absorbs some of the advantages of struts, and with some RESTFUL things, it feels much more enjoyable. But today we won’t watch springMVC, and we will take time to see the elegant parts of springMVC in the future.
Get to the point. SSH has been updating the version, so I still want to talk about the version here, otherwise a lot of friends will scold me. I use 2.3.4 for struts2, spring is the latest 3.2.2, and hibernate is 4.1.9, which is quite new.
Before entering the code, let’s first understand the roles played by the three major frameworks.
1) struts: Why do we use struts? What is the difference between them and servlets? In fact, we can still do MVC without struts, but it may be a bit more depressed in the configuration file. struts mainly helps us realize a function of distribution, divide our specific requests into a specific class, and helps us set properties (which are done through ActionForm in struts1.X). struts2 has made a lot of progress compared to struts1. It automatically sets values, does not require implementation or inheritance classes, and has a series of concepts such as request chains. Since these are not used much, I won’t mislead you brothers, so I won’t say much.
2) spring: I believe that spring's daimyo is not JAVA, don't know. Many people should have learned about spring through its IOC at the beginning, or they didn’t know anything, just the three major frameworks SSH came. It doesn't matter, anyway, when we use the three major frameworks, spring is largely a tool for a link class. Combined with the other two major frameworks, SSI and SSH, they are the same. Spring only provides tools to make us more convenient to use the three major frameworks. Of course, a large number of programs in the framework combination also rely on spring's IOC, and we will of course use transactions. AOP and other more advanced things depend on the needs. If there are any log requirements and interception requirements, it is better to use AOP to achieve it.
3) Hibernate: Hibernate is also famous in the JAVA world, basically the standard of ORM. It provides cache, level one and level two, and there are also HQL. What do we use when combining the three major frameworks? Of course, it is the main function of the ORM mapping, so we will not consider the cache for the time being. Many people have never considered why they need ORM. In fact, it is mainly due to the conflict between the data fields and the class. If you use JDBC to operate and set fields one by one, you will probably go crazy after doing it for a long time, so the ORM appears at this time.
The three major frameworks are responsible for the following things: struts2 - responsible for request forwarding and corresponding processing of forms, spring - class organization (i.e. IOC), manages the Action originally managed by struts2 as beans, and hibernate - ORM mapping, maps the classes to tables.
After understanding the general division of labor, of course we started coding. The most troublesome thing about the three major frameworks is the package. Many people like to use myeclipse mainly because it can help us import packages of the three major frameworks. But it is recommended that novices should not use that. First, myeclipse has its own project structure. When you get the eclipse guide, you must set up some project facets, which will cause inconvenience to others; second, myeclipse rarely uses it in the company, so it is better to be familiar with eclipse. Or it's good if you like ideas.
The necessary packages for struts2 are as follows: antlr, asm, xwork, struts2-core, ognl, common-logging, common-fileupload, struts-spring-plugin. There are probably only a few, and it may not be written in full. You can add the errors at startup. It is not recommended to throw all packages out as soon as you start. Some packages in the three frameworks are in conflict.
The packages required by spring3 are as follows: spring-beans, spring-core, spring-context, spring-context-support, spring-expression, spring-orm (we use three major frameworks and require ORM support), spring-web, spring-tx (we use transactions, but not involved in the example).
The packages required by hibernate4 are as follows: hibernate downloads all JAR packages in the required folder in the package.
Just like these packages, it is still the same thing. It is not recommended to put all packages as soon as you start, because some packages in the three frameworks have conflicts, so just add them as appropriate when needed. When ClassNotFound appears, put the corresponding JAR package in the lib directory.
The preparations end here and we officially begin.
1) First of all, we need to use struts2, and we must make it 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> </filter-mapping>
This code allows struts to intercept all requests. Of course, it does not mean that all requests are intercepted by spring. We can also configure struts.xml to intercept specific suffix names. Generally speaking, it is an action. as follows:
<constant name="struts.objectFactory" value="spring" /> <constant name="struts.action.extension" value="action" />
struts.action.extension configures the default suffix name for intercepting, so that the suffix name will be checked when intercepting, and those that match will be forwarded by struts. The struts.objectFactory means that the struts forwarding processing class is handed over to spring for management, that is, it is managed as a bean.
2) Usually when we use spring directly, we will call *ApplicationContext, but we are now in WEB, so we can't call it manually. In fact, spring provides a method for call in WEB situation, with a servlet (I haven't used this, I don't know how to use it), a listener, a servlet is for application servers that do not support filters, but now we basically use listener.
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
In this case, most of the cases when spring needs to be combined with other frameworks. When we only use springMVC, we can directly configure the DispatcherServlet to replace the above struts servlet. Let's take a look at the details when using springMVC later.
In this case, the applicationContext.xml file in the WEB-INF directory will generally be loaded. When your file name 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: prefix, etc., I won’t talk about it here, you can read the configuration file of spring in detail. In this way, we have actually combined struts and spring, but we have not done some bean configuration and struts forwarding.
3) As an ORM, hibernate is also managed by spring at this time. spring provides a LocalSessionFactory. Note that this class, hibernate3 and 4 are different, spring provides two. The following configuration is in applicationContext.xml
<bean id="sessionFactory"> <property name="dataSource" ref="dataSource" /> <property name="mappingResources"> <list> </list> </property> <property name="hibernateProperties"> <props> </props> </property> </bean>
In fact, the configuration is similar to that in hibernate, so I won’t talk about the specific code here.
4) In the past, when spring 2.5.x, we used HibernateDaoSupport the most, but spring 3.X did not provide this support at the beginning. We need to use the native session to operate (just inject the SessionFactory), but there is actually a problem here. If we open and close the connection for each request, it will consume more resources, but if we do not close the connection, it will not be very good. So there is a trade-off solution, and a manager to manage the connection. Here spring provides an OpenSessionInView. Every time a view is opened (basically every request), the Session will be opened. We will ignore how it is managed internally for now.
<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 we still ask him to intercept all requests. Of course, it can be configured to only intercept action suffixes, so there is no need to waste resources.
5) Basically, our SSH framework is set up, and the next thing is just a problem with the code. You only need to configure the bean in the spring configuration file, or if you like Annotation, you can just @Component(value='bean name') directly, but remember to enable Component-scan in the configuration file.
The code will not be 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" method="login"> <result name="ERROR">/pages/login.jsp</result> </action> </package> In fact, it just needs to modify the pointer in the class to pointer bean.
I won’t post the detailed code, after all, it’s a summary. In general, SSH is more beneficial for a team's specification code, because the code written under the framework specifications basically has a specific form, which is better for future maintenance. But for beginners, it is recommended not to rely too much on the framework. If you really have to use it, you need to understand in general what this framework does, what circumstances you need to use it, and what circumstances should you never use it.