Detailed description of Struts2 actions for processing multiple business logic

Source: Internet
Author: User
Tags i18n

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.

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.