I am studying jsp today. Next I will introduce you to an Action instance about Struts2 processing multiple business logic. I hope this tutorial will be helpful to you.
Strut2 allows you to customize a business Controller Method
The default value is execute ()
However, we can customize methods so that the same action can process multiple business logic.
For example
1. Our actions
| The Code is as follows: |
Copy code |
|
Public class myAction extends Actionsupport
{
@ Override
Public String execute () throws Exception
{
// Default business processing method
}
@ Override
Public String myExecute () throws Exception
{
// Custom Business Processing Method
}
} |
2. There are two methods
Method 1
Our strut. xml configuration is as follows:
| 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 = "login" class = "org. Rudiment. action. LoginAction">
<Result name = "input">/login. jsp </result>
<Result name = "error">/error. jsp </result>
<Result name = "success">/welcom. jsp </result>
</Action>
</Package>
</Struts> |
Execute () processes the current form action = "login"
Action = "login! MyExecute ()
Method 2
Our strut. xml configuration is as follows:
| 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 = "login" class = "org. Rudiment. action. LoginAction">
<Result name = "input">/login. jsp </result>
<Result name = "error">/error. jsp </result>
<Result name = "success">/welcom. jsp </result>
</Action>
<Action name = "tlogin" class = "org. Rudiment. action. LoginAction" method = "myExecute">
<Result name = "error">/index. jsp </result>
</Action>
</Package>
</Struts>
|
This method configures an action name = "tlogin" more than the previous method"
This action has an additional method = "myExecute"
When form action = "login" on the front-end, the business processing is submitted by the execute () method;
When the form action = "tlogin" in our front-end, the business processing will be submitted for processing by the myExecute () method.