Struts2_struts.xml Writing Method and usage example, struts2gettext usage

Source: Internet
Author: User

Struts2_struts.xml Writing Method and usage example, struts2gettext usage

<? 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>

<! -- The development mode of structs is enabled. The default value is true. -->
<Constant name = "struts. devMode" value = "true"/>
<! -- Dynamic call of method exclamation point, which must be opened -->
<Constant name = "struts. enable. DynamicMethodInvocation" value = "true"/>
<! -- S2 internationalization, Encoding Problems -->
<Constant name = "struts. i18n. encoding" value = "UTF-8"/> <! -- Internationalization
18 letters -->
<Include file = "/com/jaovo/struts2/ognl. xml"/> <! -- Include: Include the xml of the specified file to the current xml file. -->
<! -- ===================================================== ========================================= -->

<Package name = "front" namespace = "/front" extends = "struts-default"> <! -- (1) Module name, (2) name of the package written in the address bar (this is not the name of the previous package, it is here, and generally it is the same as the name of the previous package) ), (3) inherit from the underlying "struts-default" -->
<! -- Name indicates the package name, but the directory must be set to the value in namespace (we recommend that you add a/before the package name) -->
<Action name = "index"> <! -- Action name -->
<Result> <-- if this parameter is left blank, result name = "success" is used by default. -->
/Hello. jsp <! -- The page to jump to -->
</Result>
</Action>
</Package>
<! -- ===================== Default homepage ============================== ======================== -->
<Package name = "default" namespace = "/" extends = "struts-default">
<Default-action-ref name = "index"/> <! -- Automatically jumps to the specified page in the action with the name index by default -->
<! -- Without the above sentence, index must be added to the url. Otherwise, the url will be used by default.
In web. xml
<Welcome-file-list>
</Welcome-file-list>
To specify the default homepage
However, you can also use struts2 to specify
-->
<Action name = "index">
<Result>/default. jsp </result>
</Action>
</Package>
<! -- ===================================================== ========================================= -->
<! -- In this example, if the default value is not matched, it will match, but it is related to the configuration.
F: \ Step06_JavaEE \ _ 01_Strust2 \ struts-2.3.24-all \ struts-2.3.24 \ src \ core \ src \ main \ java \ org \ apache \ struts2 \ dispatcher \ mapper
This class contains the relevant source code
-->
<! -- If the namespace is null, all the items that cannot be matched will be matched here.
I only use namespace here. Whatever it is, it has nothing to do with action.
-->
<Package name = "fron" namespace = "" extends = "struts-default"> <! -- All the preceding errors will be done. The index corresponding to the following will be the same. This will be matched when nothing else can be matched. -->
<Action name = "index">
<Result>
/404.jsp
</Result>
</Action>
</Package>
<! -- ===================================================== ========================================= -->
<Package name = "front" namespace = "/" extends = "struts-default">
<Action name = "index" class = "com. jaovo. structs2.front. action. IndexAction3"> <! -- Action jumps to the specified class and obtains a string from the return value of the method in the class to match the following result name = success -->
<! -- The return value here is success, and then return the result matching the result as success -->
<! -- Class returns this string in three ways, see execute in./Struts2.java: -->
<Result name = "success">
/404.jsp
</Result>
</Action>
</Package>
<! -- ======================================== Method call ==== ========================================================== = -->
<! -- ======== First, specify the name to call userAdd. A method corresponds to an action ===-->
<Package name = "user" namespace = "/user" extends = "struts-default">
<! -- The two asuperlinks are written in the jsp to jump to -->
<A href = "user/userAdd"> action call the add method to add a user </a> <br/> call the first action Method
<Action name = "userAdd" class = "com. jaovo. structs2.user. action. UserAction"
Method = "add"> <! -- Method is the call method, followed by the method name -->
<Result name = "success">
/User_add_success.jsp
</Result>
</Action>
<! -- =========== Second type! Exclamation point call ================ -->
<! -- A hyperlink is written when the jsp is to jump -->
<A href = "user/user! Add "> URL dynamically transmits parameters to call related method execution </a> <br/> call the second action method. The user locates the class ,! Method Name
<Action name = "user" class = "com. jaovo. structs2.user. action. UserAction">
<Result>
/User_add_success.jsp
</Result>
</Action>
</Package>
<! -- =, Wildcard ===================================-->
<Package name = "actions" namespace = "/actions" extends = "struts-default">

<! -- Remember in SQL statements? Wildcard? Hey, the same, {1}, the first star, {2} The second star -->
<! -- Jsp file, link a -->
<A href = "actions/Studentadd"> Add students </a>
<A href = "actions/Studentdelete"> delete a student </a>
<Br/> however, we must abide by the principle of "better agreement than configuration ".

<Action name = "Student *" class = "com. jaovo. structs2.action. StudentAction"
Method = "{1}"> <! -- As shown in the preceding figure, is the class name followed by the method name? This is the method name I replaced with *. No matter what method you enter, as long as I have it, * will replace the method, and then match, so you don't need to take a lot of actions. The method is followed by the method name. To replace it with {1}, it means the first Asterisk -->
<Result name = "success">
/Student {1} _ success. jsp <! -- Then find the corresponding jsp (note: the jsp name must be consistent with this. Add the method name or the corresponding jsp file cannot be found). -->
</Result>
</Action>
The two ** numbers match everything ,------------------------------
<! -- Jsp file, link a -->
<Br/> however, we must abide by the principle of "better agreement than configuration ".
<A href = "actions/Teacher_add"> Add a teacher </a>
<A href = "actions/Teacher_delete"> delete a teacher </a>
<A href = "actions/Course_add"> Add course </a>
<A href = "actions/Course_delete"> delete a course </a>
<! -- Two asterisks: match all the rules, and cut the request for Class Name and method name of all Uris before -->
<Action name = "* _ *" class = "com. jaovo. structs2.action. {1} Action"
Method = "{2}"> <! -- The first "*" is the class name, and the second "*" is the method name. Pay attention to the details. This is the method name added after the class, must correspond to your own jump to a link -->
<Result>
/{1} _ {2} _ success. jsp
</Result>
</Action>
</Package>
<! -- ==================================== Method call and passing parameters ================ =========================================== -->
<! -- Jsp file parameter passing writing -->
<A href = "user/User_add? Name = a & age = 8 "> Add User </a>
<! -- The field value passing model is essentially uri, and the specific object is added -->
Value passing by ModelDriven. The default method is to implement the ModelDriven interface and implement the getModel method,
Then, when values are passed in the uri, the data is automatically encapsulated into the object class instead of being encapsulated. This object must be instantiated in the class.
<A href = "user/User_add? User. name = dongrege & user. age = 18 "> Add a user </a> // encapsulate the user in the object. Who is new to the object? Underlying .....
--------------------------
<Package name = "actions" namespace = "/user" extends = "struts-default"> <! -- Name indicates the package name, but the directory must be/user -->
<! -- Two asterisks, matching all rules -->
<Action name = "* _ *" class = "com. jaovo. structs2.user. action. {1} Action"
Method = "{2}">
<Result name = "success">
/{1} _ {2} _ success. jsp
</Result>
</Action>
</Package>
<! -- = ============================================= -->
<! -- ============================== Dispatcher ========== ======================================= -->
The address bar does not show the **. jsp to jump to is the same stack and local variable in the same thread.
<Package name = "resultTypes" namespace = "/r" extends = "struts-default">
<! -- This hop is the same stack, that is, the same ThreadLocal, the local variable in the thread, the parameter passed, and the request can be saved. -->
<Action name = "r1">
<! -- The result here is not the return value of the method in the traditional sense (it is the return value of the method, but a pre-defined value), but through the pre-defined return value method, perform Jump Control between different pages -->
<! -- Not specified. This is the default one: Use server jump, forward (), and use server to jump to the result page, not action -->
<Result type = "dispatcher">/r1.jsp </result>
</Action>
<! -- ============================ Redirect ============ ======================================= -->
The address bar shows **. jsp is the jump page without the action
<! -- This is to jump to a simple jsp page without going through the action -->
<Action name = "r2">
<! -- Action cannot be the underlying redirect, which is equivalent to sending it to the client browser. This is a browser jump.
Browser jsp
-->
<Result type = "redirect">/r2.jsp </result>
</Action>
<! -- ============================== Chain ============ ======================================= -->
Chain jump. chain specifies r1, which means to jump to the jump page of another action whose name is r1. Then, r1action redirects to the specified page, and the address bar does not display **, jsp can jump to action, or directly jump to a jsp page, but action can pass parameters, however, the jsp page cannot store the content of the parameter action in valuestack. attributes in jsp are saved in actioncontext.

<! -- The following one jumps to a different action and is not the same ThreadLocal. Therefore, you need to pass the parameter -->
<Action name = "r3">
<! -- Jump to action is equivalent to forward to another action, jump to r1 action, and then jump from r1action to r1.jsp -->
<Result type = "chain"> r1 </result>
<! --
<Param name = "actionName">/method name </param>
<Param name = "namespace">/package name </param>
-->
</Action>
<! -- ============================ RedirectAction ========== ======================================= -->
First, jump to the action name R2. then, the action jumps to the specified page. The address bar displays **. jsp
You can jump to action, or directly jump to a jsp page, but the action can pass the parameter, but the jsp page cannot pass the content of the parameter action to save in valuestack jsp attributes saved in actioncontext
<! -- This is to jump to an action first on the server side, and then decide to jump to the jsp page in that action -->
<Action name = "r4">
<! -- Jump from the client to the corresponding action -->
<Result type = "redirectAction"> r2 </result>
</Action>

</Package>
<! -- ==================================== Cross-package access ================ ========================================= -->
<! -- How to access cross-package access in another package -->
<! -- <Package name = "admin" namespace = "/admin" extends = "struts-default"> -->
<Package name = "admin" namespace = "/admin" extends = "user"> <! -- Inherit struts by default-default. Here we can change it to our package (your own user package) -->
<Action name = "admin" class = "com. jaovo. struts2.user. action. AdminAction"> <! -- If the returned result is not success, an error is returned. If the returned result is inherited from the user's return value, you can find the name corresponding to the result in the inherited package, if the returned value does not match, an error is returned. -->
<Result>/admin. jsp </result>
</Action>
</Package>
<! -- ============================== Encapsulation form $ {value} -->
<Package name = "user" namespace = "/user" extends = "struts-default">

<! --
Write in object class like this
@ Override
Public String execute () throws Exception {
If (type = 1) r = "/user_success.jsp ";
Else if (type = 2) r = "/user_error.jsp ";
Return "success ";
}

-->
<Action name = "user" class = "com. jaovo. struts2.user. action. UserAction">
<! -- Dynamically read the value in the content r in valueStack. The value of r is based on the default constant value and dynamically assigned to the jsp page. when reading the content here, you can read the corresponding content -->
<! -- As long as it is a member variable, the corresponding value will be saved in the value Stack -->
<Result >$ {r} </result> <! -- Parses the r value, which is an address and assigned values in the object class. It is parsed here. The above is the object class Writing Method -->
</Action>
</Package>
<! -- Jump parameter passing -->
<Package name = "user" namespace = "/user" extends = "struts-default">

<Action name = "user" class = "com. jaovo. struts2.user. action. UserAction">
<! -- Parameters cannot be passed here. Why? The receiver is not an action. If there is no action, there is no member variable. Therefore, no -->
<Result type = "redirect">/user_success.jsp? T =$ {type} </result>
</Action>
In jsp:
<S: property value = "t"/> // access the t attribute in the action (valuestack value stack), because the preceding jump is jsp and there is no action, no t attribute
<S: property value = "# parameters. t"/> // you can access the t attribute in jsp (actioncontext (all page information, equivalent to heap memory ).
</Package>
</Struts>

Related Article

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.