Implementation principle of J-Hi on no configuration files of struts2 and webwork

Source: Internet
Author: User

Implementation Method
1. Add the following configuration information to struts. xml or xwork. xml:
<Global-results>

<Result name = "auto">/$ {proxy. config. packageName}/$ {proxy. method}. jsp </result>
</Global-results>
2. Add the proxy method to the BaseAction class.
Private ActionProxy proxy;

Public ActionProxy getProxy (){
If (proxy = null)
Proxy = ActionContext. getContext (). getActionInvocation (). getProxy ();
Return proxy;
}

3. To create a JSP file, the file name must be the same as the action method name, for example, a. jap.
Public String a () throws Exception {

Return AUTO;
}
4. On a Jsp page, call the method without configuring actoin.
ActionName! A. action

Analysis
The ActionProxy class is an action proxy class provided by struts2 or webwork, it is used to record the object, name, configuration information, and package name of the current action. The declaration of this interface is as follows:
Public interface ActionProxy {

/**
* @ Return the Action instance for this Proxy
*/
Object getAction ();

/**
* @ Return the alias name this ActionProxy is mapped
*/
String getActionName ();

/**
* @ Return the ActionConfig this ActionProxy is built from
*/
ActionConfig getConfig ();

/**
* Sets whether this ActionProxy shocould also execute the Result after executing the Action
*
* @ Param executeResult
*/
Void setExecuteResult (boolean executeResult );

/**
* @ Return the status of whether the ActionProxy is set to execute the Result after the Action is executed
*/
Boolean getExecuteResult ();

/**
* @ Return the ActionInvocation associated with this ActionProxy
*/
ActionInvocation getInvocation ();

/**
* @ Return the namespace the ActionConfig for this ActionProxy is mapped
*/
String getNamespace ();

/**
* Execute this ActionProxy. This will set the ActionContext from the ActionInvocation into the ActionContext
* ThreadLocal before invoking the ActionInvocation, then set the old ActionContext back into the ThreadLocal.
*
* @ Return the result code returned from executing the ActionInvocation
* @ Throws Exception
* @ See ActionInvocation
*/
String execute () throws Exception;

/**
* Sets the method to execute for the action invocation. If no method is specified, the method provided
* In the actions configuration will be used.
*
* @ Param method the string name of the method to invoke
*/
Void setMethod (String method );

/**
* Returns the method to execute, or null if no method has been specified (meaning "execute" will be invoked)
*/
String getMethod ();
}

J-Hi borrowed this proxy class and added a reference to this class instance in the base class of the action, that is, BaseAction, so as to configure the object globally.
<Result name = "auto">/$ {proxy. config. packageName}/$ {proxy. method}. jsp </result>
$ {Proxy. config. packageName} is used to specify the package name of the current action. For example, "testjs" is the package name of the configuration file.
<Xwork>
<Package name = "testjs" extends = "hi">
<Action name = "materialList"
Class = "org. hi. testjs. action. webwork. MaterialListAction">
<Result name = "success">/testjs/MaterialList. jsp </result>
<Interceptor-ref name = "modelParamsStack"/>
</Action>
.
</Xwork >$ {proxy. method} refers to the method name that calls this action.
Name = "auto" is a special name for the unconfigured actoin, that is

Public String a () throws Exception {
Return "auto ";
Or
Return AUTO;
} The results are the same.

We deliberately put the configuration of this result in <global-results> because it is omitted to write the configuration file, as long as it is return "auto";, this result will be called. So what is the result? Yes, it is a JSP, that is, you use actionName! After method. action, the system automatically executes this method and calls the jsp file with the same name as the method name under the aciton package. For example, the configuration file package name is "testjs", the actionName is "materialList", and the corresponding class is "org. hi. testjs. action. webwork. materialListAction ", you added a () in this action class. You want to call jsp without configuration by calling this method, then you should put the jsp file in the directory of web/testjs (same as the package name), and the jsp file name is. jsp (same as the method name ). The following code calls this action method: materialList! A. action. OK, big work !!

Tips
To adapt to the development habits of different people on actions, J-Hi has different generation methods for struts2 and webwork. Struts2 indicates that all operations are put in an Action class and called through methods. webwork is an Action class for each operation. Both methods have advantages and disadvantages. You can use them with your own habits. The reason why we implement no configuration is mainly because J-Hi is not only a platform for development and management systems, but also can be used for website or e-commerce front-end development. We know that the main consideration for the backend management system is System Security (the layout and style of the page must be consistent), while the front-end of the website or e-commerce is the opposite, it is not a problem because it welcomes more viewers and does not need to control permissions for each operation (the style of the page is also varied, the characteristics of such systems are dazzling and cool and irregular ). Therefore, the configuration file-free approach is provided to meet such requirements (of course, only the page is still done by the artist, and the generator of the rule-free platform cannot do the job ). Another problem is that the platform has generated many aciton functions. How can we share these generated action classes with the background? Take struts2 as an example.
There is a protected String returnCommand () method in BaseAction, which is used to determine the name of the returned result
Protected String returnCommand (String message ){
String viewMode = HiConfigHolder. getViewMode ();

If (viewMode. equals ("dwz ")){
If (ajax = null |! Ajax. trim (). equals ("1"

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.