Access path of Struts2, path of struts2

Source: Internet
Author: User

Access path of Struts2, path of struts2

In the previous article, I have already shared with you three ways to create the Struts2 namespace and Action. In this article, we will discuss the access path of Struts2 and what is the access path, this means that when we enter the address in the browser and press enter to send a request to the server, this access path is what we will discuss today. Let's take the access path in the previous article as an example, analyze the nature of the Struts2 access path. For the url path http: // localhost: 8080/Struts/hello/login, let me analyze its composition. http: // is the http request protocol; localhost: The local access address, which is equivalent to 127.0.0.1; 8080: the access port of our local server; Struts: Make our project name; hello: Make Our namespace; login: is the Action name in our namespace. The combination is the Request Method of Struts2 Action. Of course, if the default Action is set in our namespace, the final Action name can be absent. The access paths of Struts2 are described as follows. Let's take a look at how to bind DMI dynamic methods and use wildcards.

First, let's discuss the DMI dynamic method binding. in the previous article, we accessed the methods in Action by using struts. the xml file is set by default, which is inconvenient for our interface callers. How can we bind dynamic methods? In fact, it is very simple. Taking the above url path as an example, let's take a look at how to dynamically bind the access path on the url side. The above interface accesses the login method under login Action, so we can write: http: // localhost: 8080/Struts/hello/login! Login, so that our program can dynamically bind the Action access method. Can you see it? Used in Struts2! Distinguish between the methods to be accessed in Action and Action.

Dynamic binding is not very easy, of course, some friends may feel that adding "!" to the url address Strange, is there any other way to bind dynamic methods? The answer is "must drop". We can also use wildcard configuration to bind the above dynamic method. Next we will use an example to link the knowledge learned today:

  

First, let's take a look at our struts. xml configuration file:

<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.3 // EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name = "struts. enable. dynamicMethodInvocation "value =" true "/> <! -- When Using DMI dynamic method calling, set it to true. Otherwise, a call exception occurs. --> <constant name = "struts. devMode" value = "true"/> <! -- General Action configuration --> <package name = "hell" namespace = "/hell" extends = "struts-default"> <! -- Access Control --> <action name = "index" class = "com.edu. action. hell "> <result>/hello. jsp </result> <result name = "error">/error. jsp </result> </action> </package> <! -- Use wildcards to configure actions bound to dynamic methods --> <package name = "user" namespace = "/user" extends = "struts-default"> <! -- Access Control --> <action name = "index" class = "com.edu. action. userAction "> <result>/index. jsp </result> </action> <action name = "* _ *" class = "com.edu. action. {1} Action "method =" {2} "> <result>/{2 }. jsp </result> </action> </package> <! -- Add packages here --> </struts>

UserAction. class code:

public class UserAction extends ActionSupport implements UserManage{    public String add() {        // TODO Auto-generated method stub        return SUCCESS;    }    public String del() {        // TODO Auto-generated method stub        return SUCCESS;    }    public String getUser() {        // TODO Auto-generated method stub        return SUCCESS;    }    public String getUserList() {        // TODO Auto-generated method stub        return SUCCESS;    }    public String update() {        // TODO Auto-generated method stub        return SUCCESS;    }        @Override    public String execute() throws Exception {        // TODO Auto-generated method stub        return SUCCESS;    }    }

Hell. class

public class Hell extends ActionSupport{    public String execute() throws Exception {        return SUCCESS;    }        public String addLogin(){        return ERROR;    }}

Now we can test http: // localhost: 8080/Struts2/user/User_add.

  

Next, let's analyze the above request address: http: // localhost: 8080/Struts2/user/User_add. I will not repeat the previous sections of the namespace, if you do not understand anything, you can use the asynchronous top layer to view the above explanation. Let's focus on User_add. What does this mean? Let's take a look at our struts. for xml configuration, we configure * _ * in the Action name, and then bind the Action class with the first * recognized parameter, the second * bound parameter is used for dynamic binding. The "_" here is the delimiter used by Struts2 for parameter binding. If you have any SQL operations, you must be familiar with *. Now we will tentatively set it as a placeholder.

For DMI dynamic method binding, here I will demonstrate the general configuration of the Action, the access path is: http: // localhost: 8080/Struts2/hell/index! AddLogin

Now, let's get to know about Struts2 path access, DMI dynamic method binding, and wildcard. Subsequent content is being updated...

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.