JAVA EE Learning Note [V4 Struts 2 frame test]

Source: Internet
Author: User

Going on

you can put your eyes farther away when there are some basics ahead. Java EE Development Framework (first contact with the big guy told me this is for lazy, but since there is no way to be lazy) to some extent the framework eases maintenance and development pressure, and struts as the three framework (and spring Hibernate ) has also developed into a Struts2. To talk about the role of the framework in the Java EE development process, it

If the Web application as a building, the framework is a separate skeleton of each room, with this thing, building the application is not flattered.

After I have a clear understanding of my current level, it is difficult to comprehend and understand the framework of the struts under the MVC pattern, but the use is to be mastered. Business:

What is the MVC pattern?

The MVC pattern divides the page functions into controller controllers, model models, and view views in three parts.

JSP is responsible for generating dynamic Web pages, only for display pages (V).

The servlet is responsible for process control, which is used to handle the dispatch of various requests (C).

JavaBeans is responsible for the business model, as well as the operation of the database (M).

Features of the MVC model

Views: A view is the interface that the user interacts with. The most important function of the view is to output data and allow the user to operate, it is not concerned about the processing of data and the processing of operations, such processing operations are to be done by the controller.

Model: In these three parts, the model has the most processing tasks. Simply put, the model part is the operation of the database. The model is generally called by the controller.

Because the code of the model can be reused by multiple views only once, it reduces the repetition of the code.

Controller: The controller primarily receives input from the user and invokes the model and view to complete the user's needs. It itself does not output anything to do any processing.

It only receives the request and decides which model to call to process the request, and then determines which view to use to display the data returned by the model processing.

The advantages of the MVC model

Low coupling: Each module has its own responsibilities and does not interfere with each other, which facilitates the development of Personnel division and component reuse.

High reusability and applicability: As technology continues to evolve, more and more ways to access applications are now needed. The MVC pattern allows you to access the same server-side code using a variety of different styles of view.

Maintainability: Separating the view layer and the business Logic layer also makes Web applications easier to maintain and modify.

Suitable for multi-person cooperation in the development of large Web projects conducive to software engineering management:

Because different layers have their own roles, each layer of different applications has some of the same characteristics, which facilitates engineering and tool-based management of program code.

There are a lot of frameworks in this mode, and Struts1,struts2 is the two of them.

Structure and workflow of the Struts framework

(1) The client makes a request, actionform encapsulates the data, and then submits it to Actionservlet.

(2) Actionservlet finds the specified action based on the requested information and forwards the request to the action.

(3) Action invokes the business logic method provided by JavaBean (that is, the model) to process the request and returns Actionforward.

(4) Actionservlet forwarded the request to the JSP page based on the Actionforward information.

(5) Return the final page to the client.

Gee. Now that we understand the working schematic, we continue to follow the Convention (still familiar with MyEclipse CI4):

We try to use the STRUTS2 framework:

After creation, we add frames

At this point we look at the generated XML file:

The man gave the mapping location to index.jsp and default.jsp, and for the sake of testing, I changed it to:

Then in the Web-root directory I created the login.jsp:

Then write a login demo:

<%@ Page Language="Java"pageencoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"><HTML>    <Head>    <title>Login Page</title>    </Head>    <Body>    <formAction= "Login.action"Method= "POST">    <TableAlign= "Center"Border=1width= "+"bgcolor= #F5FFE1>    <TR><TDcolspan= "2"Align= "Center"width= "+"bgcolor= "#CCCCFF">User Login Interface</TD></TR>    <TR><TDwidth= " the">Name:</TD><TD><inputtype= "text"name= "username"/></TD></TR>    <TR><TDwidth= " the">Password:</TD><TD><inputtype= "Password"name= "Password"size=22/></TD></TR>    <TR><TDAlign= "Center"colspan=2><inputtype= "Submit"value= "Login" /> <inputtype= "Reset"value= "Reset" /></TD></TR>    </Table>    </form>    </Body></HTML>

Continue to create a. java in the SRC directory:

Write to login controller content:

 Packageorg.action; Public classloginaction {PrivateString username; PrivateString password;  PublicString GetUserName () {returnusername; }     Public voidSetusername (String username) { This. Username =username; }     PublicString GetPassword () {returnpassword; }     Public voidSetPassword (String password) { This. Password =password; }     PublicString Execute () {//return "Success" without making any judgments.return"Success"; }}

Then make the following changes to Struts.xml:

After the successful landing will jump to welcome.jsp, without this, continue:

<%@ Page Language="Java"pageencoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"><HTML>     <Head>     <title>Welcome interface</title>     </Head>     <Bodybgcolor= #F5FFE1>you are welcome! ${username}</Body></HTML>

It's also a simple demo to get this done.

During the execution of the above process, the event was completed:

(1) Browser request login.jsp form action event;

(2) The Struts container receives the login.action request, according to the configuration in Web. XML, the server will go to the "Filterdispatcher" class to process the request containing the. Action suffix into the process of struts 2.

(3) The framework looks for the class loginaction for the action named "Login" in the Struts.xml configuration file. The framework initializes the action and executes the Execute method of the action class.

(4) The framework checks the struts.xml to see the corresponding page when it is returned.

The framework tells the container to get the result page welcome.jsp of the request return, where the value in the value stack is output with an El expression. (This can also be output with the tabs provided by Struts 2)

The structure body of STRUTS2 is the action class, and the three-layer filter filters and configuration file struts.xml that match the class. In addition, the view layer with struts dedicated tag library, template Library and so on.

It's awkward to be =-= in this process without a servlet. And the emergence of simple Java class makes the whole program become low coupling high cohesion (soft teacher mantra)

Features of the STRUTS2 framework

Servlet Retreat! It can be seen that there is no servlet in the whole process.

Everything is programmed through a more powerful Java class. Avoids the problem that the JSP and servlet in struts1 cause the view layer and controller to tangle because of too tight coupling.

Provides a clear implementation of MVC, which contains a number of key components involved in processing requests, such as interceptors, OGNL expression languages.

For a request that requires multiple layers of processing to be returned, you can use filters and interceptors to implement it at a layer level. The OGNL expression language is powerful and can replace the JSTL tag library.

Through the trial of STRUTS2 framework, it is obvious that the importance of the main action class is =-= obviously, I can't digest it today. Go on.

(PS: again to except Sheedi elder brother =-=, and in fact the framework environment variable configuration and build pretty troublesome, I this is stained with the IDE's light)

JAVA EE Learning Note [V4 Struts 2 framework test]

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.