Struts2 annotation Configuration @namespace (iv)

Source: Internet
Author: User

Start learning @namespace annotations today.

Or look at the code first:

[Java]View Plaincopy
  1. Package com.example.actions;
  2. Import org.apache.struts2.convention.annotation.Action;
  3. Import Org.apache.struts2.convention.annotation.Namespace;
  4. Import Com.opensymphony.xwork2.ActionSupport;
  5. @Namespace ("/custom")
  6. Public class HelloWorld6 extends Actionsupport {
  7. @Action ("/h6/url")
  8. Public String Execute () {
  9. return SUCCESS;
  10. }
  11. @Action ("url")
  12. Public String dosomething () {
  13. return SUCCESS;
  14. }
  15. }

    • namespace annotations allow the operation of the class namespace to change the name of the Java package instead of using the Convention plugin Convention.
Carefully analyze the above code. The package name of HelloWorld6 is com.example.actions. If there is no @namespace ("/custom") annotation, this package will be used as the root package according to the Convention plugin Convention. The namespace corresponding to the action URL is "/". In the case of @namespace ("/custom") annotations, the HelloWorld6 namespace is changed to "/custom". But we enter http://Application server IP in the browser (port/app/h6/url access, there is no problem, you can also jump to the "/h6/url.jsp" page.
What is the reason for this?
The key is @action ("/h6/url"). The "/h6/url" here is fully qualified for the action's namespace. The experiment proves that the @namespace ("/custom") annotation has no effect on the namespace being fully qualified.
    • When a @namespace comment is made for an action class, the effect of the comment is applied to all the not fully qualified action URLs in the class

Try to enter http://Application server IP in the browser-when the port/application/url access, the error message is reported. Remove @namespace ("/custom") annotations, restart Service, visit again, success! Add the @namespace ("/custom") annotation again, restart the service, access, and fail.
This round operation validates a problem @Namespace annotations have been made to the DoSomething () method.
Take a closer look at every detail. There are @action ("url") annotations on this method. It does not have "/" in front of "url". This is an not fully qualified action URL. This is a good indication that the effect of the comment is applied to all the @namespace action URLs in the class after the comment is made for the action class.
This way we enter http://Application server IP in the browser (port/Application/cutsom/url access, successful. The jump page is "/custom/url.jsp".

Struts2 annotation Configuration @namespace (iv)

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.