Java three-frame ssh and MVC

Source: Internet
Author: User
Tags html form

Java-ssh (MVC)

The respective roles of the three main frameworks of Java

Hibernate is the underlying JDBC-based ORM (object-relational mapping) persistence framework, which is the mapping of tables and classes, the mapping of fields and attributes, and the mapping database model of records and objects.
The rich tags provided by struts are used to display layer views, and struts acts as a control function (either Struts1 or Struts2), receiving parameters, and view distribution.
Spring is used to reduce the level of layer-to-layer coupling, that is, all classes can be created uniformly by spring, only when injected (IOC)


Functionally, Hibernate is M,struts2 V, or c,spring can be c.
But for most projects, Hibernate is used by M,struts2 as v+c,spring to help manage classes and implement some business-agnostic functions, such as logging, most of which use struts to do C.

First, Spring

Spring is a powerful framework that solves many of the problems that are common in the development of Java EE. Spring provides a consistent approach to managing business objects and encourages the good habit of injecting programming to interfaces rather than classes. The schema base of spring is based on the inversion of control container that uses the JavaBean property. However, this is only part of the complete Picture: Spring is unique in using the IOC container as a complete solution to all architectural layers. Spring provides a unique data access abstraction, including a simple and efficient JDBC framework, which greatly improves efficiency and reduces possible errors. Spring's data Access architecture also integrates hibernate and other O/R mapping solutions. Spring also provides a unique transaction management abstraction that provides a consistent programming model across a variety of underlying transaction management techniques, such as JTA or JDBC transactions. Spring provides an AOP framework written in the standard Java language that provides POJOs with declarative transaction management and other enterprise transactions-if you need it-to implement your own aspects. This framework is powerful enough to allow applications to throw away the complexity of EJBS while enjoying the critical services associated with traditional EJBS. Spring also provides a powerful and flexible MVC web framework that can be integrated with IOC containers.

Second, STRUCTS

Struts is an MVC framework based on the Sun EE platform, which is implemented mainly by servlet and JSP technology. Because struts can fully meet the needs of application development, easy-to-use, agile and rapid, over the past year has been a considerable concern. Struts consolidates Servlets, JSPs, custom tags, and information resources into a single, unified framework that developers use to develop without having to code their own full set of MVC patterns, saving time So struts is a very good application framework.

Third, Hibernate

Hibernate is an open-source object-relational mapping framework that provides JDBC with a very lightweight object encapsulation that allows Java programmers to manipulate databases at will using object programming thinking. Hibernate can be applied to any JDBC application, both in Java client applications and in servlet/jsp Web applications, and most revolutionary is that hibernate can replace CMP in the EE architecture of the EJB application. The task of achieving data persistence. Hibernate can be used to replace CMP in the Java EE architecture of EJB application, and to accomplish the data Persistence task. Mapping is very simple, is the map of the page, do more practice will be.

The three frameworks of Java are used to make Web applications.struts is primarily responsible for presentation layer display spring uses its IOC and AOP to handle the control business (responsible for database operations) Hibernate is primarily the persistence of data to the database. When using the JSP servlet to do Web page development, there is a mapping file with Web. XML, there is a mapping tag is used to do file mapping. When you enter the URL address in the browser, the file will be based on the name you write to a Java file, according to the content written in the Java file displayed in the browser, is a Web page, so the page name is casually written, random name, whatever you get up is. php. jsp. Do or everything else corresponds to this Java file, the code in this Java file to do what to do, display a word or connect the database or jump to other pages and so on, this Java file to encapsulate the data for security and easy to manage the role. In fact, this Java file compiled is a. Class of a bytecode file, there is not a similar HTML embedded tags and code page file. The difference between him and the JSP file is that the JSP embeds the code in the HTML tag. And the commit name in the action of the servlet or the HTML form in struts corresponds to a Java file, and struts is generally. Do, which is mapped as above.

1. Talk about your MVC understanding
MVC is the abbreviation for Model-view-controler. That is, the model-view-controller. MVC is a design pattern that forcefully separates the input, processing, and output of an application.
Models, views, and controllers in MVC have different tasks.
Views: A view is an interface that the user sees and interacts with. The view displays the relevant data to the user and accepts the user's input. The view does not handle any business logic.
Model: Models represent business data and business processes. Equivalent to JavaBean. A model can provide data for multiple views. This improves the reusability of the application
Controller: When the user clicks the Submit button in the Web page, the controller accepts the request and invokes the appropriate model to process the request.
The corresponding view is then called according to the result of the processing to display the results of the processing.
MVC process: First the controller accepts the user's request, invokes the corresponding model for business processing, and returns the data to the controller. The controller invokes the corresponding view to display the results of the processing. and presented to the user through a view.

Struts
1. What is the difference between struts1.2 and struts2.0? How do I control the singleton pattern in both frames?
Comparison of struts1.2 and struts2.0
A, Action class:
struts1.2 requires the action class to inherit a base class. struts2.0 action requires inheriting Actionsupport base class
B, Threading mode
The struts1.2 action is singleton and must be thread-safe because there is only one instance of the action that handles all requests.
The singleton strategy limits what Struts1.2 action can do and is especially careful when developing. The action resource must be thread-safe or synchronous.
struts2.0 action generates an instance for each request, so there is no thread safety issue.
C, servlet dependency
The struts1.2 action relies on the servlet API because HttpServletRequest and HttpServletResponse are passed to the Execut method when an action is invoked.
The struts2.0 action does not depend on the container, allowing the action to be tested separately from the container. If required, the STRUTS2 action can still access the initial request and response.
However, other elements reduce or eliminate the need for direct access to HttpServletRequest and HttpServletResponse.
D, testability
A major problem in testing the struts1.2 action is that the Execute method exposes the servlet API (which makes the test dependent on the container). One third-party extension: Struts TestCase
A set of struts1.2 simulation objects is provided for testing.
Struts2.0 action can be tested by initializing, setting properties, calling methods, and "Dependency injection", which makes testing easier.

2. Why SSH is used in the project
1. Struts is used because struts is based on the MVC pattern, and the application is layered so that developers are more focused on the implementation of the business logic; second, struts has a wealth of taglib, and if flexible use, can greatly improve the development efficiency.
2. Hibernate: Hibernate provides an easy-to-use, efficient object-relational mapping framework for Java applications. Hibernate is a lightweight, durable framework that is rich in functionality.
3. Use spring: Because Spring is based on the framework of the IOC (inversion of control, reverse control) and AOP architecture multi-layered Java EE system, it does not force you to have to use spring in every layer, because it is well-modularized, Allows you to choose one of its modules according to your needs; IOC makes it easy to implement bean assembly, provides concise AOP, and implements transaction management (Transcation managment), etc.

3. How struts and spring inherit
Do not know the meaning of the topic: whether it is said that the struts action is inherited from Org.apache.struts.action.Action and spring inheritance is unclear.

4. How struts achieves internationalization
The following are examples of the two languages (Chinese, English):
1. Add struts support to your project
2. Edit the applicationresource.properties file to include information that you want to use for internationalization, such as:
Lable.welcome.china=welcome!!!
3. Create an English resource file applicationresource_en.properites
4. Create temporary Chinese resource file Applicationresource_temp.properites For example: lable.welcom.china= China welcomes you!
5. Encode and convert the temporary Chinese resource file. You can use the MyEclipse plug-in, or you can do it under DOS:
Native2ascii-encoding gb2312 applicationresource_temp.properties applicationresource_zh_cn.properties
6. Add struts's bean tag library to the JSP <%@ taglib uri= "/web-inf/struts-bean.tld" prefix= "Bean"%>
Display information:
You can define multiple resource bundles in the Struts-config.xml file, and each resource bundle can use the key property to specify the name of the package.

5. Common tags for struts2.0
1. Pass the value in action:
2. The display Tag property is used to output the specified value:
3. Incoming the value of the object used to move from page to action (user):
4. Determine the specific data used in the page .....
5. Iterations are used to iterate through a collection of lists, maps, ArrayList, and so on
6. URL address tag, used to generate a URL address, you can send the request parameter to the URL address by the child element specified by the URL tag
7. Hyperlinks are generally used with tags for multiple parameters. "> Hyperlinks
8. Set label for placing a value within the specified range. such as application,session and so on.

6. How to configure Form-bean, action, tiles in struts
The struts1.2 is configured here.
Form-bean configuration: (in configuration file struts-config.xml):
Name: Specify the name of the form; Type specifies the kind of form: Package name + class name;
Action configuration: (in configuration file struts-config.xml)
Attribute= ""
Input= ""
Name= ""
Parameter= ""
Scope= "Request"
Type= ""
>
Path: Request the name of the action; The name of the Attribute:form, corresponding to the name in the Form-bean; Input: Enter the path of the page;
Name: If the configuration of Attribute,name does not work, as with attribute; Parameter: When using the Distribute action, specify the call distribution
The name of the method in action;

The scope of the scope:action; Type of Type:action: Package name + class name;

7. What are the benefits of IOC (DI) (Java Project's spring IOC Simulation)

IOC (inversion of control, inversion of controls): When a property of a class needs to be used, this class needs to initialize itself. Some properties of the spring class are managed by spring instead of being initialized by themselves. This transformation is the IOC idea. Spring's IOC idea is that spring's IOC container initializes the corresponding objects in the configuration and puts them in the container at initialization time. The properties of the corresponding class are initialized at the same time based on the configuration. That is, Di (Dependency Injection Dependency injection), the initialization of the class does not require the class itself to be initialized and given to the spring container.
The advantage of the IOC is that it reduces coupling: a property of a class that was previously controlled by the class, and that, after spring, the properties of the class are controlled by spring.
What the Spring IOC simulations do is to configure the appropriate bean in the XML file and set the injection relationship. The bean is initialized while parsing the XML, and the corresponding attribute is injected.

8. What is AOP?

Two kinds of injection methods:

a) using annotation
b) Using XML

Aspect-oriented programming (AOP) to improve spring's dependency injection (DI), aspect-oriented programming in spring is mainly represented in two aspects
1. To provide declarative transaction management for tangent-oriented programming
2.spring supports user-defined facets

Aspect-oriented programming (AOP) is a complement to object-oriented programming (OOP), object-oriented programming breaks down programs into objects of all levels, and facets-oriented programming breaks down the program's running process into facets.
AOP considers the structure of the program from the point of view of the program, extracts the facets of the business process, and OOP is a static abstraction, and AOP is a dynamic abstraction that abstracts the steps in the application execution process, thus obtaining the logical division between the steps.
The two characteristics of an AOP framework are:
1. Good isolation between the various steps
2. Source code Independence

9.DAO layer uses JdbcTemplate operation data, how to control the transaction at the service layer?
The DAO layer uses JdbcTemplate for database operations.
The service layer invokes the DAO layer method.
Because a service method may require methods to invoke multiple DAO objects, transaction control is required at the service layer.
For a number of reasons, a programmatic transaction (now using Transactiontemplate) is required.
But there is a problem:
The datasource that is used by the DataSource object and the DAO object creation JdbcTemplate object when creating the Transactiontemplate object Datasourcetransactionmanager use the same object to be in Transa The Ctiontemplate.execute () method controls the transaction.

Java three-frame ssh and MVC

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.