Java programmers go from stupid birds to cainiao (35) to elaborate on struts2 (1) self-implemented struts2 framework

Source: Internet
Author: User

This article is from: Cao shenghuan blog column. Reprinted please indicate the source:Http://blog.csdn.net/csh624366188

 

Struts was originally part of the Apache Jakarta project. The project creator hopes to improve and improve the technical standards of JavaServer pages, Servlet, label library, and object-oriented by studying this project. The initial struts1.x soon became popular in enterprise development. At the same time, a very good web development framework was born, namely webwork, but webwork was not as lucky as struts1, not popular, but webwork is concise, flexible, powerful, and other advantages will never be lost in the popular strut1.x. Of course, the struts1 developers are not aware of this. So struts and webwork were combined, and webwork was developed by using struts's fame, so struts2 was born.

Struts2 Overview

Struts 2 is the next-generation product of Struts. It is a new struts 2 framework that is merged Based on Struts and webwork technologies. The new architecture of struts 2 is significantly different from that of struts 1. Struts 2 uses webwork as the core and the interceptor mechanism to process users' requests. This design also enables the business logic controller to completely remove from the servlet API, therefore, Struts 2 can be understood as a webwork update product. Although there are many changes from struts 1 to struts 2, Struts 2 has only a small change compared to webwork. Because struts1 is rarely used in development, we can directly learn struts2, but most of the previous projects retain the struts1 application. Because struts is a framework based on the MVC mode, the first step for us to learn struts is to develop our own MVC-based framework.

First, let's look at the example of the next MVC flowchart:

As shown in the preceding figure, add a value at the view layer. In JSP, write a form that submits two data. The form is submitted to the Controller. In the controller, obtain the action to be submitted by the URI submitted by the form, and then submit the request to the action, then, call the business logic method in the action to perform logical operations, obtain the results, save the results, and then return all returned interfaces to the Controller as the returned results, then the Controller forwards the returned interface string to the interface.

Next we will use a program to implement this process:

1. Write the form interface: Add. jsp

<Form action = "add. action "method =" Post "> <Div align =" center "> <font color =" # 8000ff "> </font> <font size =" 5 "color = "# 8000ff "> <strong> Implementation of the divider </strong> </font> <br/> </div> <Table align =" center "> <tr> <TD> first number: </TD> <input type = "text" name = "firstnmb"/> </TD> </tr> <TD> Number 2: </TD> <input type = "text" name = "secondnmb"/> </TD> </tr> <tr align = "center"> <TD colspan = "2"> <input type = "Submit" value = "sum"/> <input type = "reset"/value = "reset"> </TD> </tr> </table> </form>

2. Create a controller. In fact, the Controller here is a servlet. Here we will specify that all requests with a suffix of. Action are submitted to this controller, controller. Java:

public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {String path=request.getRequestURI();String realPath=path.substring(path.lastIndexOf("/")+1, path.lastIndexOf("."));Action action=null;String path2=null;if("add".equals(realPath)){action=new AddAction();path2=action.execute(request, response);}          .........         If(....){                 .......              }request.getRequestDispatcher(path2).forward(request, response);}

Because the controller is a servlet, You need to configure it in Web. xml:

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  <servlet>    <servlet-name>Controller</servlet-name>    <servlet-class>zxj.struts2.servlet.Controller</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>Controller</servlet-name>    <url-pattern>*.action</url-pattern>  </servlet-mapping></web-app>

Next, let's take a look at the content that should be written in action. Since API-oriented programming has always been promoted, and API-oriented programming can also reflect Java's scalability, therefore, we provide a common interface for all actions: action. java:

public interface Action {public String result(HttpServletRequest request,HttpServletResponse response);}

The following is the specific action implementation: addaction. Java: The add method called by the specific business logic is the addition of two numbers, so no code is posted here:

 

public String execute(HttpServletRequest request,HttpServletResponse response) {double i=Double.parseDouble(request.getAttribute("firstNmb").toString());double n=Double.parseDouble(request.getAttribute("secondNmb").toString());Calculator c=new Calculator();double result=c.add(i, n);request.setAttribute("result", result);return "add_result.jsp";}}

 

These are the basic MVC frameworks we have written on our own. Of course, there are many shortcomings here. Here we just want to demonstrate the basic architecture based on the MVC framework, details can be refined and extended, such as the Action selected in the controller,This can be implemented using the configuration file. The basic idea is to get the request in the controller.The prefix name of the action, parse the configured file, and then hold the prefix name to find the class corresponding to the action in the configuration file, then, execute the corresponding class method using reflection, and then execute the actionAnd then obtain the corresponding page from the configuration.In this way, the scalability of this program can be well reflected.

Here I believe that you should have a certain understanding of the execution process of the MVC-based framework. I believe that you will not be able to learn the struts2 framework, so everyone will wait for the next blog: introduction to the struts2 framework

Related Article

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.