1. Action
The code is as follows: |
Copy code |
Package org. Rudiment. action; Import javax. servlet. ServletRequest; Import javax. servlet. http. HttpServletRequest; Import org. apache. struts2.ServletActionContext; Import org. apache. struts2.interceptor. ServletRequestAware; Import com. opensymphony. xwork2.ActionContext; Import com. opensymphony. xwork2.ActionSupport; Public class LoginAction extends ActionSupport { @ Override Public String execute () throws Exception { System. out. println ("execute executed "); Return SUCCESS; } Public String test1 () throws Exception { System. out. println ("test1 executed "); Return SUCCESS; } Public String test2 () throws Exception { System. out. println ("test2 executed "); Return SUCCESS; } } |
2. struts. xml configuration file content
The code is as follows: |
Copy code |
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.1 // EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <Struts> <Constant name = "struts. custom. i18n. resource" value = "mess"/> <Constant name = "struts. i18n. encoding" value = "GBK"/> <Package name = "default" extends = "struts-default"> <Action name = "* Action" class = "org. Rudiment. action. LoginAction" method = "{1}"> <Result name = "input">/login. jsp </result> <Result name = "error">/error. jsp </result> <Result name = "success">/welcom. jsp </result> </Action> </Package> </Struts> |
3. Example of front-end form submission
The current form action = "Action", struts handed the business to execute ()
For the current form action = "test1Action", struts handed the business to test1 () for execution
The current form action = "test2Action", struts handed the business to test2 () for execution
Action = "executeAction" of the current form, struts delivers the business to execute ()
====================================== Extended content ================ ====================
1. Still use the above Action, and then change the configuration file to this
The code is as follows: |
Copy code |
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.1 // EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <Struts> <Constant name = "struts. custom. i18n. resource" value = "mess"/> <Constant name = "struts. i18n. encoding" value = "GBK"/> <Package name = "default" extends = "struts-default"> <Action name = "* _ *" class = "org. Rudiment. action. {1}" method = "{2}"> <Result name = "input">/login. jsp </result> <Result name = "error">/error. jsp </result> <Result name = "success">/welcom. jsp </result> </Action> </Package> </Struts> |
2. Example of front-end form submission
The current form action = "LoginAction_execute", struts handed over the business to execute ()
The current form action = "LoginAction_test1", struts handed the business to test1 () for execution
The current form action = "LoginAction_test2", struts handed the business to test2 () for execution
Note: When the requested action = "LoginAction_test1", struts will send the request to the test1 () method of org. Rudiment. action. LoginAction for processing.
In addition, the {1} expression can be used in both action and result.
When we configure
The code is as follows: |
Copy code |
<Action name = "*"> <Result>/*. jsp </result> </Action> |
Here, action does not specify the class to be processed. By default, struts uses ActionSuppoer. This class always returns Action. SUCCESS.
Therefore, for the current request action = "index", strtus maps the request to the index. jsp page, that is, the View returned to the user is index. jsp.
Note that if the name attribute is not specified in the result, the default value is name = success.