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