Java web development SSH framework for collaborative processing application notes

  • 2020-04-01 04:31:38
  • OfStack

  Believe that no people don't know a SSH, struts 2 + spring + hibernate, enterprise development "foundation" of architecture, based on why quotes, because it's just a lot of people think this basis, the most basic is the servlet, many training institutions, many basic didn't teach, directly up to the three frameworks, SSH or SSI, make a lot of people think JAVAWEB development framework, English will be no framework with his hand. There is no harm in the three frameworks, practical, a lot of companies are using, direct development. But after graduation, I really haven't used the three frameworks for a long time. Spring is useful, especially springMVC, which feels much better than struts2. In fact, when you think about it, spring absorbs some of the advantages of struts and adds something RESTFUL to it. But we won't be looking at springMVC today, and we'll take a look at the elegance of springMVC later.

              Let's get down to business. SSH has been updating version, here I still want to say the 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 pretty new.
             
              Before we dive into the code, let's take a look at the roles each of the three frameworks plays.
              1) struts: why do we use struts, compared to servlets? In fact, we can still achieve MVC without struts, but in the configuration file may be more depressed. Struts mainly helps us to implement a function of distribution, to divide our specific request into a specific class, and to help us set the properties (via ActionForm in struts s1.x). Struts2 is much better than struts1, setting values automatically, not forcing implementation or inheritance of classes, and having a series of chain of requests and so on. Because these use not much, lest mislead brothers, say no more.
              2) spring: I believe that the name of spring does not make JAVA. Many of you probably knew about spring through its IOC at first, or didn't know anything at all, just the three frameworks SSH. It doesn't matter. When we use the three frameworks, spring is largely a tool for bonding. Combining the other two frameworks, SSI and SSH, is the same. Of course, a large part of the framework also relies on spring's IOC, and we certainly use transactions. AOP, the more advanced things, depending on the need, such as what logging requirements, interception requirements, you can use AOP to achieve better.
              3) Hibernate: Hibernate is also a big name in the JAVA world, which is basically the standard of ORM. It provides caching, level 1 and level 2, and it also has HQL, what do we use when we combine the three frameworks? Of course, that's the main function of the ORM mapping, so we're not going to worry about caching. Many people don't think about why they need ORM. In fact, it is mainly because of the conflict between the fields and classes of the data. If you use JDBC to operate, one field after another to set, it is estimated that if you do it for a long time, people will go crazy, so then ORM appears.
              The three frameworks are responsible for the following: struts2, which is responsible for request forwarding and corresponding processing of the form; spring, which is the organization of the classes (i.e.ioc), which manages the actions originally managed by struts2 as beans; and hibernate, which is the ORM mapping, which maps the classes into the table.
 
              After understanding the general division of labor, of course, we began to code. The big trouble with the three frameworks is the package, and many people like to use myeclipse mainly because it can help us import the package of the three frameworks. However, it is recommended that beginners do not use it. Firstly, myeclipse has its own project structure, and it will set some project facets and so on when getting the first guide of eclipse, which will bring inconvenience to others. Second, the company rarely use myeclipse, or familiar with eclipse better. Or ideas are good.
              The necessary packages of struts2 are as follows: antlr,asm,xwork,struts2-core,ognl,common-logging,common-fileupload,struts-spring-plugin. There are only a few of them, but not all of them are written. You can add in the case of startup times and errors.
              Spring 3 requires the following packages: spring-beans,spring-core,spring-context,spring-context-support,spring-expression,spring-orm (we use the three frameworks and need orm support),spring-web,spring-tx (we use transactions, but not in the example).
              The following packages are required for hibernate4: hibernate downloads all jars in the required folder within the package.
              Just about these packages are OK, or that sentence, it is not recommended to put all the packages at the beginning, because some of the three frameworks are in conflict with each other, in the need to add as appropriate, when ClassNotFound to the appropriate JAR, into the lib directory.
 
              That was the end of the preparations and we began in earnest.
              1) first, we need to use struts2 and make sure it intercepts 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 block all requests. Of course, this does not mean that all requests are blocked by spring. We can also configure struts. As follows:


<constant name="struts.objectFactory" value="spring" /> 
<constant name="struts.action.extension" value="action" /> 

                Struts.action. Extension is configured with the default suffix name of the interception, so that the suffix name will be checked during the interception, and the corresponding suffix name will be forwarded by struts. Struts.objectfactory instead means that the struts forwarding class is managed by spring, or managed as a bean.
             
              2) normally when we use spring directly, we will call *ApplicationContext, but now we are in the WEB situation, we can't call it manually. In fact, spring provides a method for invoking the WEB case, with a servlet (which I haven't used and don't know exactly how to use) and a listener, which is called by an application server that doesn't support filters, but now we basically use a listener.


<listener> 
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
 </listener> 

              In this case, spring needs to combine with other frameworks to use most of the cases, when we just use springMVC, we can directly configure the DispatcherServlet to replace the struts servlet above, we will see when we use springMVC later.
              In this case, you will load the applicationcontext.xml file in the web-inf directory, and you can configure the configContextPath attribute when the file name is not this or the path is not here


<context-param> 
  <param-name>configContextPath</param-name> 
  <param-value>WEB-INF/applicationContext.xml</param-value> 
</context-param> 

                It supports the use of the classpath: prefix, etc., and without further ado, you can look at spring's configuration files in detail. So we've actually combined struts and spring without some bean configuration and struts forwarding.
             
                3) hibernate, as ORM, is also managed by spring. Spring provides a LocalSessionFactory. Note that this class, hibernate3 and 4, is different. Spring provides 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> 
  </bean> 

 
                In fact, the configuration is similar to hibernate, here is not to say the specific code.
 
              4) before spring. X, we use most is HibernateDaoSupport, but spring3. X start does not provide the support, we need to use native session operate (you just need to inject SessionFactory), but it actually involves a problem, if we each request to open close connection, will be consume resources, but if we don't close the connection, and will not good. So there needs to be a compromise, a manager to manage the connections. In this case, spring provides an OpenSessionInView, which opens Session on every view (basically every request), but we're not going to worry about how it's managed internally.


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

                Again, let him block all the requests, of course, can be configured to only block the action suffix, there is no need to waste resources.
 
              5) basically, our SSH framework is set up and it's just a matter of code. Just configure the beans in the spring configuration file, or if you like annotations, just @component (value='bean name '), but remember to turn component-scan on 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"> 
    <!--  The user login  --> 
    <action name="login" class="userAction" method="login"> 
      <result name="ERROR">/pages/login.jsp</result> 
    </action> 
  </package> 

                In effect, you simply change the point in the class to point to the bean.
 
                The code is not posted, after all, is the summary of the post. In general, SSH is good for the standard code within a team, because the code written under the framework's specification basically has a specific form and is better for future maintenance. But for beginners, it is recommended not to rely on the framework too much, really must use, at least in the general understanding of what the framework does, when you need to use it, when you should never use it.


Related articles: