Struts2 learning notes, struts2 Learning

Source: Internet
Author: User

Struts2 learning notes, struts2 Learning

There are three methods for one Action to process multiple requests

Here we will talk about Dynamic Method nvocation, Dynamic Method call. What is Dynamic Method call? An Action contains multiple Method calls.

In general, we only need to write an execute method in Action and return the String, or inherit the ActionSupport parent class and override this method.

However, in this way, only one request can be completed in an Action. To solve the problem of real development, the same Action can process similar services and resources, and we can use dynamic method calls.

-------------------------------------------

The following is an example:

1. Write two pages first

The first page is the login page DMI_login.jsp mainly uses the login Action,

The second page is DMI_register.jsp, which mainly uses the register Action.

A beginner may want to write two actions, login and register, and then complete two different logic processes. Think carefully. In fact, the results of both operations are the same, is to return an existing user, but here, we can write the Action in this jsp page as DMIAction! Login and DMIAction! Register, which means the login method and register method in the DMIAction class.

// DMI_login.jsp <% @ page language = "java" contentType = "text/html; charset = UTF-8 "pageEncoding =" UTF-8 "%> <% @ taglib prefix =" s "uri ="/struts-tags "%> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> 

  

// DMI_register.jsp <% @ page language = "java" contentType = "text/html; charset = UTF-8 "pageEncoding =" UTF-8 "%> <% @ taglib prefix =" s "uri ="/struts-tags "%> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> 

  

2. struts. xml configuration

The configuration is the same as the configuration. You only need to map the Action and implementation class. Here, you must pay special attention to the following configuration. This configuration mainly enables dynamic method calls, if it is not enabled, dynamic method call is not allowed. The 404 page is displayed.

 <constant name="struts.enable.DynamicMethodInvocation" value="true"/>

 

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    "http://struts.apache.org/dtds/struts-2.0.dtd"><struts>    <constant name="struts.enable.DynamicMethodInvocation" value="true"/>    <package name="default" extends="struts-default">                <action name="DMIAction" class="struts2.action.DMIAction">            <result name="success">RegisterSuccess.jsp</result>        </action>            </package></struts>

 

3. Write Action

Similar to the written Action, it is the methods called by the page in the Action. Here two methods are used: login and register, therefore, we need to implement these two methods in this class.

package struts2.action;import java.util.Map;import com.opensymphony.xwork2.ActionSupport;public class DMIAction extends ActionSupport {    /**     *      */    private static final long serialVersionUID = 1L;    public String login() {        return SUCCESS;    }    public String register() {        return SUCCESS;    }    private String username;    private String password;    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }        private Map sessionMap;    private Map applicationMap;    public void setSession(Map sessionMap) {        this.sessionMap = sessionMap;    }        public void setApplication(Map applicationMap) {        this.applicationMap = applicationMap;    }}

 

Summary: Now you can run the command to check the results. There are several other methods to achieve the same results. You can view other articles.

 

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.