Overview of Struts framework based on MVC pattern

Source: Internet
Author: User
Tags define object execution html tags web services java web jboss access

Author: Yarshray





Keywords: Java ee-javascript mvc JSP Servlet MVC Struts

Content Introduction:



MVC design pattern has become a very popular design method at present. It is a kind of software design method pattern. And it is proved by a lot of development and practice. Therefore, this article introduces the framework of the software component which has implemented the MVC pattern by introducing the Struts framework, discusses the advantages of this model and how to put it into software development.

This article focuses on the combination of theory and practice, comparing the system from multi-layer design to distributed software development in the technology used to cut into the current sun Company's exit Java EE Framework model, and then more this framework to discuss related technologies, including jsp,servlet, user-defined tags and other related topics, Then discuss its advantages and disadvantages, and finally lead to the theme of the article, struts framework, the framework through the implementation of MVC design patterns to change the past design methods, so that the development of a set of applications based on Java EE Framework is very wide adaptability and high efficiency.





Famous sayings:
If I look farther than Descartes, it is because I stand on the shoulders of giants.


--Newton


People with a wealth of knowledge and experience are more likely to produce new associations and insights than those with only one knowledge and experience.


--Taylor

Introduction:



At present, software development has entered the stage of object-oriented, component and distributed application. Sun introduced the JAVA2 Enterprise Edition (Java) platform has become increasingly mature. And has been widely used. The Web Components in the Web container defined in Java EE are also the main tools and means to develop Web applications at the present time. JavaServer Page (JSP) technology, almost every Java Web application development must develop components.

The current web container is also a blooming flower, and some free open-source organizations have joined the campaign. The tomcat provided by the Apache Software organization is a very good web container, This also drives the development of Java Web applications. With the maturity of Java Web technology and the great demand of the market, more and more developers are starting to use the development framework that has been developed. And now the idea of software development is not "whether we want to use the software framework" but to say " What software development framework to use. "

Therefore, choosing a good development framework is necessary as a software developer. Jakarta-struts is a set of source code projects provided by the Apache Software organization that implements the model---view---controller (Mode---view---Controller) MVC Design pattern. Its extensive adaptability and high scalability, coupled with a significant increase in productivity, has become a recognized standard for web development in Java today.

Of course, as we developers are also a challenge, I must understand these new concepts and recognize and accept the advantages of this development model, which requires our understanding of the theory and a large number of development practices.





Introduction to Java Web applications



Java Web applications are the work of building applications by developing and deploying Java Web Components on a Web container. It includes technologies such as Java Server Page and Servlet technology. In addition, the development of a complete Java Web application includes the following technologies:

L JavaBean Component Technology

• EJB Component Technology

L user-defined tag library

L XML Tag language

L Java Related containers













Figure 1 Technical architecture for Java WEB applications





Servlet Controller Component Technology



The important role of the servlet in a typical Web application is that the servlet is well-suited for messaging forwarders between Web pages. Because the servlet is a component module that is dynamically loaded by the Web container, it is not very suitable for user interface representations, When a Web user makes a request, the servlet component can capture the request message through the servlet container so that it can invoke the model component to come out the message.



Figure 2 The process flow of the Servlet

When a client program (such as a browser) first makes a Web request to the Servlet container, the server receives the request and invokes the appropriate servlet component to execute the request, and the servlet component handles the result to the servlet container. The servlet container then replies the request back to the client. This is also a typical implementation of a Web application. Java servlet technology provides a set of user programming interfaces that define related classes for developers.

The following more focused classes in the Servlet API determine the process flow of the servlet:

L HttpServlet: This class provides the ability for user extensions to write servlet components by inheriting the class. Mainly fill in HTTP methods (such as: DoPost doget) to complete the service.

L HttpServletRequest: This class provides the ability to accept user requests for data, which is typically created by a Web container in a client request service. The servlet can access the requested data of the user at the time of the request through this object.

L HttpServletResponse: This class is used to generate the corresponding results after execution. The class is also created by the Web container in the service.

L HttpSession: The same kind of processing customer's session tracking.

L The Servletcontext:servlet container provides an environment for each Web application module to maintain the state of the application module. This group of States is the shared information that all Web Components in the module can access.

The above is a general introduction to the servlet component, and if you want to learn more about the servlet technology, you can access the site: http://www.servlet.com. There will be a detailed introduction.







JSP Component Technology



JSP component Technology is the extension of traditional static Web pages (HTML), so that traditional static pages can be added to the Java program code, to provide programs to edit the effect of the page. And the component technology is also running the component technologies on Java Web containers.

The nature of JSP is the servlet component technology, but the JSP technology is more conducive to do the user's presentation interface.



Figure 3 The first time a Web container executes a browser request to process a JSP

JSP page in the initial acceptance of the user's request, the first web container will translate JSP script files, and then generate servlet source code, in the servlet source code compiled into Java bytecode generates a class file. Then process the page flow as the servlet executes.

If the reader wants to learn more about JSP technology, you can http://java.sun.com/products/jsp/the site for more help.





The role of JavaBean components in Web applications



JavaBean is a Java class that conforms to a specific specification. The series properties and methods are defined in JavaBean. We can usually encapsulate the business model and business process model in JavaBean, and simply say that JavaBean is a good fit for the role of model components in Web applications.

A set of tags for javabean support has been defined in the JSP Standard tab. and defines the lifecycle of the JavaBean object during execution. including page, request,session and application Four ranges. and provides property support for JavaBean access.

If you want to learn more about JavaBean, the reader can refer to the Http://java.sun.com/products/javabeans site for a very detailed description.





User-defined Label



User-defined tags in JSP provide developers with a good interface and functional scalability. It enables users to define the corresponding business tags in the JSP page. Typically these tags inherit from the TagSupport class. and extend the functionality by overriding the related methods of the TagSupport class.

In struts, the Struts framework defines a set of user tags. It mainly includes:

L Bean label, which is used to support JavaBean access

L HTML tags are used to support the creation and access of HTML interface elements

L logic tags are used to support logical execution

L neted tags and tiles tags are used to support interface representations







EJB Component Technology



Enterprise Java Bean abbreviation EJB is a set of Standards-based distributed object Component technologies. It is CORBA and RMI server-side Java components. EJB technology, like JavaBean technology, handles business logic in enterprise development. The difference is that EJB components are distributed and must run in the EJB container. The JavaBean component is a component that runs in the caller's memory.

The EJB defines the session bean that is used to handle business process logic and the entity bean that is used to maintain the business persistence state. and processing message-driven message beans.

EJB components so the EJB container that runs, maintains security, transactional, persistence, parallelism, and many other services for EJB components. and provides support for the development, deployment, and operation of the environment.

Readers can access http://java.sun.com/products/ejb/index.html to learn these techniques in depth.







XML Markup Language



XML markup languages can be used to define the user's own markup. XML is used very extensively in Web services. such as the SOAP protocol is defined by XML. XML can define information structures that are very generic and useful for application processing.

XML acts as a binder in the struts framework and can be used to configure the constituent frames of the components in the struts framework.

Readers can access the http://www.w3schools.com/xml/default.asp to get more learning materials.





Java related containers



At this stage, any distributed development can not be separated from the server. For Web development, Web servers are typically used to handle HTTP request and reply work, and provide security, transactional, Multitasking parallelism. The modern web container also provides a programmable user interface. The logical part of the application in distributed development is usually placed on the middleware server of the application. The EJB container is such a middleware server. This server provides support for the running environment of EJB components. and provide for rmi- The support of remote object invocation protocols such as IIOP makes it easy to develop middleware components here. It makes developers more concerned with business logic issues.

The most popular open source Web server is the Tomcat server of the Apache software organization. Tomcat is not only a good Web server, but also a very convenient servlet/jsp container. And the open source EJB container is more representative when the number JBoss It is developed by JBoss Software organization, its performance is stable and reliable, and has very efficient execution speed.







An overview of MVC design Patterns



In general, a computer program includes its input, processing, and output. The MVC design pattern makes the input, processing, and output parts independent of the part of the program. And when the system is running, it will be combined, so that the coupling of the development is very low.



Figure 4 MVC Design Model

View, which is usually the interface for user interaction in software. It is used to represent data and to submit user requests for software.

Model, which is used to deal with the business scope of the software, mainly the business data model and the business logic model.

Controller, which accepts the user's request and invokes the process in the model, and then selects the appropriate view.





The design pattern of MVC in Java Web Applications is very much used. The JSP MODEL2 architecture is the implementation of the MVC pattern combining the Jsp,servlet and JavaBean technologies.



Fig. 5 JSP Model2 Model





Struts Framework Overview



As software development matures and software engineers become more mature, we begin to design the software framework structure so that we do it in an orderly fashion at the development stage. For modern software development, the software development cycle requires more and more rapid, So it's not realistic to develop a framework of your own, so we have to choose the mature software framework.

And struts is a good choice for Java Web development. The framework of struts is the implementation of JSP MODEL2 model.





Figure 6 The MVC framework implemented by Struts

1. View

Views are implemented through a set of JSP programs. These JSP programs cannot contain business logic, nor can they include information about the model. The information of the model is passed through the controller. Actionform is also seen as a part of the view in struts. It other updates the view, and the view query model state of the bridge .

2. Model

A model is a state representation of a program, and its state includes the state that is passed in the message, the state in the shared state or in a single reply, and the persistent state. The processes that operate these states are also included in the model. These models are the overall operational processes and data of the business.



3. Controller

The controller is partially implemented by Actionservlet and action. Actionservlet is the interface that the struts framework uses to interact with the Web container. This class is the core component of the Struts framework. Actionservlet inherits the HttpServlet class. It's in the MVC model Play the role of a central controller. When a Web container receives a customer request, it delivers actionservlet the configuration information for this class more Struts-config.xml files Select the appropriate action to process the request. and pass the data in the request process, The data is passed through Actionform. The action itself does not contain the processing code of the business logic. It is only responsible for invoking the processing code of the business.

The method that calls the code is in the Execute () method. This method returns a Actionforward object to select the corresponding view after calling business processing. In general, our action must inherit the class directly or indirectly. Then rewrite Excecute () method to complete our own processing message.



4.Struts configuration file Struts-config.xml

The file is essentially a controller and a view of the binder, the controller is able to select the corresponding view, and the view can be queried through the corresponding controller to the data in the model are dependent on the file. The file is an XML file. Each Action in this profile corresponds to a <action > tags are used to map the action class information.





From the requirement analysis and design of software







Implementation of a complete Web application based on struts







Summarize:

Java Web applications use a lot of technology and are relatively complex. Therefore, developers in the development of the technical aspects of responsibility will be relatively large, so we must take into account the personal expertise of the development of a reasonable allocation of work, This requires us to think clearly in the design phase of the software. and MVC's design pattern is a good way to separate the business data model from the business processing model so that you can develop separate system components, To make it easier to maintain data consistency during the development view phase. The controller becomes the link between the view and the model, making the software project a systematic project.

The struts framework plays a role in Java Web applications. This enables the development phase to use the framework to complete the development project, we can select the appropriate model components such as we can use JavaBean technology to complete the business model and data model interface, A database can act as a data model to maintain a persistent state in the business phase. JSP and struts provide us with a set of tags that can complete the presentation of the page and the logic of the page so that we have a rich view function. and struts provides a good channel for actionform data exchange interfaces for views and events so that you can handle action events by ACTI Onform the data from the view. And the bridge between the two is the Struts-config.xml file the XML file can configure the connection between the two. This makes the interface extremely flexible and scalable. In action we have the ability to invoke and contain the model components. This makes development very efficient and scalable capabilities.

The struts framework has in fact been used by many software projects, so it's very reliable when it comes to applicability. It is also hoped that the introduction of this question will enable readers to have a good understanding of the struts framework.





Resources:



<< proficient in Struts Java WEB Design and development based on MVC >> Author: Sun Weichen

&LT;&LT;JSP 2.0 Technical Reference manual >> author: Gerlingan Department

&LT;&LT;J2EE >> Author: Joseph J.bambara Paul R.Allen, translator: Liu Yan Wait




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.