Java-SSH (MVC)
Roles of the three Java frameworks
Hibernate is the underlying persistence framework of the JDBC-based ORM (Object relationship ing), that is, the ing between tables and classes, the ing between fields and attributes, and the ing between records and objects. The database model is also the model;
Struts provides rich labels for display layer view. Struts also acts as a control function (whether struts1 or struts2) to receive parameters and distribute views.
Spring is used to reduce the coupling between layers, that is, all classes can be uniformly created by spring and only need to be injected (IOC)
In terms of function, Hibernate is m, struts2 is V, can be C, and spring can be C.
However, in most projects, Hibernate is m and struts2 is V + C. Spring is used to help manage classes and implement some business-independent functions, such as logging. Most of them use struts for C.
I. Spring
Spring is a powerful framework that solves many common problems in J2EE development. Spring provides consistent methods for managing business objects and encourages injection of good habits of interface programming rather than class programming. Spring's architecture is based on the inversion of control container using the JavaBean attribute. However, this is only part of the complete picture: Spring is unique in the use of IOC containers as a complete solution to build a focus on all architecture 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 solution. Spring also provides a unique transaction management abstraction that provides a consistent programming model for various underlying transaction management technologies, such as JTA or JDBC transactions. Spring provides an AOP framework written in the standard Java language, which provides pojos with declarative transaction management and other enterprise transactions-if you need to-implement your own aspects. This framework is powerful enough to allow applications to get rid of the complexity of ejbs and enjoy key services related to traditional ejbs. Spring also provides a powerful and flexible MVC Web framework that can be integrated with IOC containers.
Ii. structs
Struts is an MVC framework based on the sun J2EE platform. It is mainly implemented using Servlet and JSP technologies. Because struts can fully meet the needs of application development, it is easy to use and agile, and has received a lot of attention in the past year. Struts integrates servlet, JSP, custom tags, and message resources into a unified framework. Developers do not need to code themselves to implement a full set of MVC patterns during development, it saves a lot of time, so struts is a very good application framework.
Iii. hibernate
Hibernate is an open-source object relationship ing framework that encapsulates JDBC objects in a lightweight manner, so that Java programmers can use the object programming thinking to manipulate the database as they wish. Hibernate can be used in any scenario where JDBC is used. It can be used in both Java client programs and Servlet/JSP web applications. The most revolutionary thing is that, hibernate can replace CMP in the J2EE architecture of application EJB to fulfill the task of data persistence. Hibernate can replace CMP in the J2EE architecture of application EJB to fulfill the task of data persistence. The ing is simple, that is, page ing. Just do more exercises.
The three major Java frameworks are used for Web applications.Struts is mainly responsible for displaying the presentation layer. Spring uses its IOC and AOP to process and control the business (responsible for database operations). Hibernate is mainly responsible for data persistence to the database.When JSP servlet is used for web page development, there is a web. xml ing file, which contains a mapping label for file ing. When you enter the URL address in the browser, the file will correspond to a Java File Based on the name you write, and the content written in the Java file will be displayed in the browser, it is a webpage, so the webpage name is written at will, no matter what you start. PHP
. JSP. do or others correspond to this Java file, and the code in this Java file performs any operations, displays a sentence, connects to the database, jumps to other pages, and so on, this Java file encapsulates data for security and ease of management. In fact, this Java file is compiled into a. Class bytecode file, not a webpage file similar to HTML embedded tags and code. The difference between JSPs and JSP files is that JSP embeds code into HTML tags. In addition, the submission name in the action of the HTML form in servlet or struts corresponds to a Java file. Struts is generally. Do and is mapped like above.
1. Let's talk about your understanding of MVC.
MVC is short for Model-View-controler. Model-View-controller. MVC is a design model that forcibly separates the input, processing, and output of an application.
Models, views, and controllers in MVC undertake different tasks.
View: view is the interface on which the user sees and interacts with it. The view displays relevant data to users and accepts user input. The view does not process any business logic.
Model: The model represents business data and business processing. It is equivalent to JavaBean. A model can provide data for multiple views. This improves the reusability of applications.
Controller: When you click the submit button on the web page, the Controller accepts the request and calls the corresponding model to process the request.
Call the corresponding view based on the processing result to display the processing result.
MVC processing process: the Controller first accepts user requests, calls the corresponding model for business processing, and returns data to the Controller. The Controller calls the corresponding view to display the processing result. And present it to the user through the view.
Struts
1. What is the difference between struts1.2 and struts2.0? How do I control the singleton mode in the two frameworks?
Comparison between struts1.2 and struts2.0
A. Action class:
Struts1.2 requires the action class to inherit a base class. Struts2.0 action must inherit the actionsupport base class
B. thread mode
Struts1.2 action is in singleton mode and must be thread-safe, because there is only one action instance to process all requests.
The Singleton policy limits what struts1.2 action can do, and takes special care during development. Action resources must be thread-safe or synchronized.
Struts2.0 action generates an instance for each request, so there is no thread security problem.
C. servlet dependency
Struts1.2 action depends on the servlet API, because when an action is called, httpservletrequest and httpservletresponse are passed to the execut method.
Struts2.0 action does not depend on the container and allows the action to be tested independently from the container. If necessary, struts2 action can still access the initial request and response.
However, other elements reduce or eliminate the need to directly access httpservletrequest and httpservletresponse.
D. Testability
One major problem in testing 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
It provides a set of struts1.2 simulated objects for testing.
Struts2.0 action can be tested through initialization, setting properties, and calling methods. "dependency injection" also makes testing easier.
2. Why is SSH used in the project?
1. struts is used because struts is based on the MVC pattern, and the application is well layered, so that developers are more concerned with the implementation of business logic. Second, Struts has a rich set of taglib, if it can be used flexibly, the development efficiency can be greatly improved.
2. Use hibernate: hibernate provides an easy-to-use and efficient object relationship ing framework for Java applications. Hibernate is a lightweight persistence framework with rich functions.
3. use spring: Because spring is based on IOC (inversion of control, reverse control) and AOP to build a multi-layer J2EE system framework, it does not force you to use spring in each layer, because of its good modularization, you can choose to use one of its modules based on your own needs. IOC makes bean Assembly easy to implement, provides simple AOP and implements transaction management accordingly (such as transcation management ).
3. How does struts and spring inherit from each other?
Do not understand the meaning of the question: whether the struts actions are inherited from org. Apache. Struts. action. Action, and the inheritance in spring is unclear.
4. How to internationalize struts
The following uses two languages (Chinese and English) as an example:
1. Add struts support to the Project
2. Edit the applicationresource. properties file and add the information to be internationalized. For example:
Lable. Welcome. China = welcome !!!
3. Create an English resource file applicationresource_en.properites
4. Create a temporary Chinese resource file applicationresource_temp.properites, for example, lable. Welcom. China = China. Thank you!
5. encode and convert temporary Chinese resource files. You can use the myeclipse plug-in or execute it in DOS:
Native2ascii-encoding gb2312 applicationresource_temp.properties applicationresource_zh_cn.properties
6. Add the struts bean tag library to JSP <% @ taglib uri = "/WEB-INF/struts-bean.tld" prefix = "Bean" %>
Display Information:
You can define multiple resource packages in the struts-config.xml file, and each resource package can use the key property to specify the package name.
5. Common struts2.0 labels
1. Pass the value to the action:
2. display the tag property to output the specified value:
3. It is used to pass values from the page to the object (User) in the action:
4. Judgment is used to determine the specified data on the page .... ....
5. iteration is used to traverse the list, MAP, arraylist, and other sets cyclically.
6. the URL tag is used to generate a URL address. You can send request parameters to the URL address through the child element specified by the URL tag.
7. hyperlinks are generally used together with tags to include multiple parameters. "> Hyperlink
8. Set tag, used to put a value within the specified range. For example, application and session.
6. how to configure form-bean, action, and tiles in struts
Struts1.2 is configured here.
Form-bean configuration :( in the profile struts-config.xml ):
Name: Specifies the form name; Type specifies the Form Type: package name + class name;
Action configuration: (in the profile struts-config.xml)
Attribute = ""
Input = ""
Name = ""
Parameter = ""
Scope = "request"
Type = ""
>
Path: name of the request action; attribute: form name, which corresponds to name in form-bean; input: Path of the input page;
Name: If attribute is configured, name does not work. It is the same as attribute. Parameter: Specifies the call distribution when the action is distributed.
Method Name in action;
Scope: action range; Type: Action type: package name + class name;
7. What is IOC (DI) and what are the advantages (Spring IoC simulation of Java Project)
IOC (inversion of control): an attribute of a class needs to be initialized by the class itself. Some attributes of the spring class are managed by spring instead of initialization. This transformation is the idea of IOC. The idea of Spring IoC is that the Spring IoC container initializes the corresponding objects according to the configuration during initialization and stores them in the container. At the same time, initialize the attributes of the corresponding class according to the configuration. That is, di (dependency injection). At this point, class initialization does not require class initialization and is handed over to the spring container.
The advantage of IOC is: reducing coupling: a property of a class, which was previously controlled by the class itself, and spring controls the attributes of the class.
The simulation of Spring IoC is to configure the corresponding bean in the XML file and set the injection relationship. Initialize bean during XML parsing and inject corresponding attributes.
8. What is AOP?
Two injection methods:
A) Use Annotation
B) use XML
Improve spring dependency injection (DI) for Aspect-oriented programming (AOP). Aspect-Oriented Programming mainly involves two aspects in spring.
1. Declarative Transaction Management for Aspect-Oriented Programming
2. Spring supports custom aspect
Aspect-oriented programming (AOP) is a supplement to Object-Oriented Programming (OOP). Object-Oriented Programming breaks down programs into objects of various layers. Aspect-Oriented Programming splits program processes into various aspects.
AOP considers the program structure from the perspective of running the program and extracts the aspect of the business processing process. Oop is a static abstraction, AOP is a dynamic abstraction, and abstract the steps in the application execution process, to obtain the logical division between steps.
The AOP framework has two features:
1. Good isolation between steps
2. Source Code independence
9. How to control transactions at the service layer when the DaO layer uses jdbctemplate to operate data?
The Dao layer uses jdbctemplate for database operations.
The service layer calls the DaO layer method.
Since a service method may need to call multiple Dao object methods, transaction control needs to be performed at the service layer.
For some reason, you need to adopt a programmatic transaction (now using transactiontemplate ).
However, there is a problem:
Controls transactions in the merge cute () method.