Struts 2 implements an MVC development approach like thinkphp
Thinkphp is an MVC open source framework for PHP, and Struts 2 is an MVC open source framework for Java. Although programming languages are different, they all solve the same problem.
The students who have used thinkphp should know that thinkphp is a very simple frame that is comfortable to use. In contrast, struts 2 is not so simple.
This article describes how to configure Struts 2 so that it is as easy to use as thinkphp.
Before introducing struts 2, let's look at how thinkphp is developed.
Like Struts 2, thinkphp also needs to act as a controller, requiring a page to act as a view.
Here is a simple directory structure for the thinkphp project:
Where the action directory is the controller, which functions like the Struts 2 action. The TPL directory is an HTML page, which is a view, similar to the functionality of a JSP page.
Then look at one of the action's Code:
Display ();} Public function Add () {/** * logic code ... */$this->display ();} Public Function Delete () {/** * logic code ... */$this->display ();} Public Function Update () {/** * logic code ... */$this->display ();}}? >
At this point in the browser input http://localhost/First/update, you can call Firstaction's Update method to process the request, and then call tpl/first/update.html display.
In other words, if you need to add a function module ABC, you only need a AbcAction.class.php, which adds the Operation function Def, and then tpl/ Under the ABC folder to create a file name and function name consistent def.html files, the following can be developed logic and page, and directly can be accessed with http://localhost/Abc/def, very convenient. The whole project is clear, and it's easy to find an action function for an HTTP address and an HTML file.
and struts 2 because of the Struts.xml configuration file, if you add a function module, in addition to adding the Action class and JSP page, you also need to configure the action in the Struts.xml HTTP address, the corresponding action in the method, and the corresponding JSP file. If you want to find the method and JSP file in the corresponding action by HTTP address, you also need to look at the configuration in Struts.xml.
In the use of Struts 2, the Convention is a very important principle in that it is larger than configuration . The way to develop thinkphp is a good deal. Here's how to use struts 2 to implement a similar thinkphp development approach.
The first is the configuration of the Struts.xml file:
/tpl/{1}/{2}.jsp
To implement similar thinkphp development methods, you need to use wildcards in Struts.xml, which is the wildcard character. where name= "/*/*" matches the HTTP address. The first * is the class name of the action: class= "com.xxg.action. {1} Action "; The second * is the name of the method in action: Method=" {2} "and also the JSP filename: /tpl/{1}/{2}.jsp 。
Here are the settings:
A slash "/" is allowed in the name of the action.
Here is the directory structure for this project:
One of the action's Code:
Package Com.xxg.action;import Com.opensymphony.xwork2.actionsupport;public Class Firstaction extends ActionSupport { Public String Show () {/** * logic code ... */return SUCCESS;} Public String Add () {/** * logic code ... */return SUCCESS;} Public String Delete () {/** * logic code ... */return SUCCESS;} Public String Update () {/** * logic code ... */return SUCCESS;}}
In this way, in the development process, what to do is similar to thinkphp, add action, adding the corresponding method, and then adding a JSP file with the same name as the method name, direct access to the address: Http://localhost/Action name/method name can be directly accessed, You no longer need to add an action to the Struts.xml file. In addition, the HTTP address is also easy to find the corresponding action of the method and the corresponding JSP file, and no longer need to query struts.xml.
Author: fork Brother reprint Please specify Source: http://blog.csdn.net/xiao__gui/article/details/21073605
-
2 Floor Jpfrmd3 hours ago
-
Good article, next time take a good look
-
1 Floor 5iasp4 hours ago
-
Yes, very flexible.