Struts2 Video Learning Note 11-12

Source: Internet
Author: User

Lesson 11

    • Dynamic Method Invocation

If more than one method exists in the action, you can invoke the specified method using the!+ method name. (Not recommended)

 1  public   String execute () { 2  setmsg ("Execute" 
   
    ); 
    3  
    return  "Success" 
    ; 
    4  
     5  6  public   String Add () { 7  setmsg ("Add"  8  return  "Success" ; 9 } 
   

Assume that the URL path to the action above is:/struts/test/list.action to access the action's Add () method, we can call it this way:/struts/test/list!add.action

    • To define an action using a wildcard character
1 < Packagename= "Struts2"namespace= "/test"extends= "Struts-default">2     <Actionname= "List_*"class= "Tutorial." HelloWorld "Method= "{1}">3         <resultname= "Success">/test.jsp</result>4     </Action>5 </ Package>

Lesson 12

    • Receive Request parameters

When you define an attribute with the same name as the request parameter in the action class, STRUTS2 automatically receives the request parameter and assigns it to the property with the same name.

Struts2 calls the setter method of a property with the same name as the request parameter by using reflection technology to get the request parameter value.

Request Path: Http://localhost:8081/Struts2Demo/test/list_execute?id=111&name=jaid

1 Private intID;2 PrivateString name;3  Public intgetId () {4     returnID;5 }6 7  Public voidSetId (intID) {8      This. ID =ID;9 }Ten  One  PublicString GetName () { A     returnname; - } -  the  Public voidsetName (String name) { -      This. Name =name; -}

   

Post mode

1 <formAction= "<%=request.getcontextpath ()%>/test/list_add.action"Method= "POST">2Id:<inputtype= "text"name= "id"><BR>3Name<inputtype= "text"name= "Name"><BR>4     <inputtype= "Submit"value= "Submit">5 </form>

Receive request parameters with a composite type (default constructor provided)

1 <formAction= "<%=request.getcontextpath ()%>/test/list_add.action"Method= "POST">2Id:<inputtype= "text"name= "Person.id"><BR>3Name<inputtype= "text"name= "Person.name"><BR>4     <inputtype= "Submit"value= "Submit">5 </form>

  • There is a bug in the struts2.1.6 version, that is, the received Chinese request parameter is garbled (submitted by post), The reason is that struts2.1.6 called HttpServletRequest's Setcharacterencoding () method to encode settings after acquiring and using the request parameters, causing the application to use the garbled request parameter. This bug has been fixed in struts2.1.8, if you are using struts2.1.6, to solve this problem, you can do this: Create a new filter, place the filter before the STRUTS2 filter, and then in the Dofilter () method, add the following code

    1  Public void DoFilter (...) {23   httpservletrequest req = (httpservletrequest) request; 4 5   Req.setcharacterencoding ("UTF-8"); // you should replace UTF-8 based on the encoding you use 6 7   Filterchain.dofilter (request, response); 8 9 }

Struts2 Video Learning Note 11-12

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.