Start learning @namespace annotations today.
Or look at the code first:
[Java]View Plaincopy
- Package com.example.actions;
- Import org.apache.struts2.convention.annotation.Action;
- Import Org.apache.struts2.convention.annotation.Namespace;
- Import Com.opensymphony.xwork2.ActionSupport;
- @Namespace ("/custom")
- Public class HelloWorld6 extends Actionsupport {
- @Action ("/h6/url")
- Public String Execute () {
- return SUCCESS;
- }
- @Action ("url")
- Public String dosomething () {
- return SUCCESS;
- }
- }
- 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)