Struts 2 implements MVC development similar to ThinkPHP

Source: Internet
Author: User
Struts2 implements ThinkPHP-like MVC development method. ThinkPHP is a php mvc open-source framework, and Struts2 is a Java MVC open-source framework. Although the programming languages are different, they all solve the same problem. ThinkPHP is a very simple Struts 2 MVC development method similar to ThinkPHP.

ThinkPHP is a php mvc open-source framework, and Struts 2 is a Java MVC open-source framework. Although the programming languages are different, they all solve the same problem.

ThinkPHP is a simple framework and is comfortable to use. Struts 2 is not that simple.

This article describes how to configure Struts 2 to make it as simple as ThinkPHP.

Before introducing Struts 2, let's take a look at ThinkPHP development methods.

Like Struts 2, ThinkPHP also needs Action to act as the controller and pages to act as views ).

The following is the directory structure of a simple ThinkPHP project:


The Action directory is the controller. its function is similar to the Action in Struts 2. The Tpl directory contains html pages, that is, views, which are similar to the functions of jsp pages.

Let's look at the code of one of the actions:

 Display ();} public function add () {/*** logic code... */$ this-> display ();} public function delete () {/*** logical code... */$ this-> display ();} public function update () {/*** logical code... */$ this-> display () ;}}?>
Enter http: // localhost/First/update in the browser to call the update method of FirstAction to process the request, and then call Tpl/First/update.html for display.

That is to say, if you need to add a function module Abc, you only need to add a AbcAction. class. php, where the operation function def is added, and a file name and alias def.html file are created under the Tpl/abcfolder. The following describes the development logic and page, and you can directly access the file through http: // localhost/Abc/def, very convenient. The entire project is clear, and the functions of the Action corresponding to an http address and the html file are also very easy.

Struts 2 has struts. xml configuration file. if you add a function module, in addition to adding the Action class and jsp page, you also need. configure the http address of the Action, the method in the Action, and the jsp file corresponding to the Action in xml. If you want to find the method and jsp file in the corresponding Action through the http address, you also need to view the configuration in struts. xml.

In Struts 2,Conventions greater than configurationsIs a very important principle. ThinkPHP development is a good convention. The following describes how to use Struts 2 to implement ThinkPHP-like development methods.

First, configure the struts. xml file:

 
 
  
  
   
    /Tpl/{1}/{2}.jsp
   
  
 
To implement ThinkPHP-like development, you need to use Wildcards in struts. xml, that is, Wildcards. The name = "/*" matches the http address. The first * is the Action class name: class = "com. xxg. action. {1} Action "; the second * is the name of the method in Action: method =" {2} ", and the jsp file name: /Tpl/{1}/{2}. jsp .

Here we set:

 
The slash "/" is allowed in the action name "/".

The directory structure of the project is as follows:


Code for one of the actions:

Package com. xxg. action; import com. opensymphony. xwork2.ActionSupport; public class FirstAction extends ActionSupport {public String show () {/*** logic code... */return SUCCESS;} public String add () {/*** logical code... */return SUCCESS;} public String delete () {/*** logical code... */return SUCCESS;} public String update () {/*** logical code... */return SUCCESS ;}}
In this way, the development process is similar to ThinkPHP, adding Action, adding the corresponding method, and then adding a jsp file with the same name as the method to access the address directly: http: // localhost/Action name/method name. add action to the xml file. In addition, it is easy to find the method and jsp file of the corresponding Action through the http address, and you do not need to query struts. xml.


Author: Cross brother reprint please indicate the source: http://blog.csdn.net/xiao__gui/article/details/21073605



JPFRMD3 hours ago
Good Article. please take a look at it next time.
5 iasp4 hours ago on the first floor
Good, flexible.

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.