Implementing the STRUTS2 Framework

Source: Internet
Author: User

Struts was originally part of the Apache Jakarta Project, and the project's founders wanted to improve and improve JavaServer Pages, Servlets, tag libraries, and object-oriented technical standards through research on the project. The initial struts1.x soon became popular in the enterprise development, at the same time, there was a very good web development framework was born, that is webwork, but webwork not as lucky as struts1, not get popular, but webwork concise, The advantages of flexibility and power are never lost in the prevailing strut1.x. Of course struts1 developers are not and are not aware of this. So struts and WebWork got a combination, webwork is to use struts fame to develop their own bar, so Struts2 was born.

STRUTS2 Overview

Struts 2 is the next generation of struts and is a new Struts 2 framework that has been combined with struts and webwork technology. Its new Struts 2 architecture differs greatly from the architecture of struts 1. Struts 2 takes the webwork as the core and uses the interceptor mechanism to handle the user's request, which also allows the business logic controller to completely disengage from the servlet API, so struts 2 can be understood as the WebWork update product. Although there are too many changes from struts 1 to struts 2, there are only minor changes relative to the Webwork,struts 2. Since struts1 is rarely used in development now, we go directly to Struts2 's study, but most of the previous projects still hold STRUTS1 applications. Since struts is a framework based on the MVC pattern, the first step in learning struts is to develop our own MVC-based framework

First look at an example of an MVC flowchart:

Just like the example in the figure, add in the view layer. The JSP writes a form that submits two data, submits the form to the Controller, obtains the action to be submitted by the form in the controller through the URI it submits, then gives the request to action, and then invokes the method of business logic in the action to perform the logical operation, obtains the result, saves the result, All returned interfaces are then returned to the controller as returned results, and the controller is forwarded to the interface based on the string selection of the returned interface

Here we use the program to implement this process:

1. First to write the form interface: add.jsp

[HTML] View Plain Copy Print ?

  1. <form action= "add.action" method= "post"><div align= "center"><font color= "# 8000FF ">

  2. </font><font size= "5" color= "#8000ff"><strong> adder Implementation </strong> </font><br/>

  3. </div><table align= "center">

  4. <tr>

  5. <td> First Number:</td>

  6. <td><input type= "text" name= "FIRSTNMB"/></td>

  7. </tr>

  8. <tr>

  9. <td> Second number:</td>

  10. <td><input type= "text" name= "SECONDNMB"/></td>

  11. </tr>

  12. <tr align= "center">

  13. <td colspan= "2"><input type= "Submit" value= "sum" /> <input Type= "Reset"/ value= " Reset" ></td>

  14. </tr>

  15. </table>

  16. </form>


2. Create the controller, in fact, the controller here is a servlet, here we give the rule that the request suffix is. Action is submitted to this controller, Controller.java:

[HTML] View Plain Copy Print ?

  1. public void DoPost (HttpServletRequest request, httpservletresponse response)

  2. Throws Servletexception, IOException {

  3. String Path=request.getrequesturi ();

  4. String realpath=path.substring (Path.lastindexof ("/") +1, Path.lastindexof ("."));

  5. Action action=null;

  6. String path2=null;

  7. if ("Add". Equals (Realpath)) {

  8. Action=new Addaction ();

  9. Path2=action.execute (request, response);

  10. }

  11. .........

  12. If (...) {

  13. .......

  14. }

  15. Request.getrequestdispatcher (path2). Forward (request, response);

  16. }


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

[HTML] View Plain Copy Print ?

  1. <?xml version= "1.0" encoding= "UTF-8"?>

  2. <web-app version= "2.5"

  3. Xmlns= "Http://java.sun.com/xml/ns/javaee"

  4. Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"

  5. Xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee

  6. Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd ">

  7. <servlet>

  8. <servlet-name>controller</servlet-name>

  9. <servlet-class>zxj.struts2.servlet.controller</servlet-class>

  10. </servlet>

  11. <servlet-mapping>

  12. <servlet-name>controller</servlet-name>

  13. <url-pattern>*.action</url-pattern>

  14. </servlet-mapping>

  15. </web-app>


Let's take a look at what should be written in action, because interface-oriented programming has always been advocated, and interface-oriented programming is a good embodiment of Java extensibility, so we provide a common interface for all actions: Action.java:

[HTML] View Plain Copy Print ?

    1. Public interface Action {

    2. Public String result (httpservletrequest request,httpservletresponse response);

    3. }

Here is the specific action implementation: Addaction.java: Where the specific business logic calls the Add method is two numbers added, here is no code:

[Java] view Plaincopyprint?

  1. Public String Execute (httpservletrequest request,

  2. HttpServletResponse response) {

  3. Double i=double.parsedouble (Request.getattribute ("FIRSTNMB"). toString ());

  4. Double n=double.parsedouble (Request.getattribute ("SECONDNMB"). toString ());

  5. Calculator c=New Calculator ();

  6. Double Result=c.add (i, n);

  7. Request.setattribute ("result", result);

  8. return "add_result.jsp";

  9. }

  10. }

These are the basic framework of the MVC we write ourselves, of course, there are a lot of deficiencies, here just to demonstrate the basic framework based on the MVC framework, the details can be refined and extensibility implementation, such as the controller inside the choice of which action, which can be implemented with the configuration file, Basic idea: Get the prefix name of the requested action in the controller, then parse the configured file, and then take the prefix name to find the corresponding class of action in the configuration file, then execute the corresponding class method with reflection, and then after executing the action , the result is obtained, and then the interface corresponding to the result is obtained from the configuration , which can well reflect the extensibility of the program.

Here I believe that we should have a certain understanding of the implementation process based on the MVC framework, I believe that we must be struts2 the framework of learning, we are waiting for the next blog: Details of struts2 the first knowledge struts2 framework

This article is from: Cao Shenhuan blog column. Reprint Please specify source: http://blog.csdn.net/csh624366188

Implementing the STRUTS2 Framework

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.