stay struts2 In the frame action Class instead of servlet The status of , as a result of action Class encapsulates a lot of methods , You can use it directly without having to create it yourself , therefore ,action There are also several ways to create , Please look at the notes below ~

Struts2 Of Action establish

1、Struts2 Of Action How it was created

Struts2 in Action There are three ways to create classes , Namely :
【2】 Implement an interface
【3】 Inherit a class

These three kinds of creation Action What classes have in common is the naming of names —— All need to be named after ~

Now let's take a look at these kinds of creation action What is the way a class looks like ~

1.1、POJO Way to create Action

First , To understand what POJO——POJO yes Plain OrdinaryJava Object Abbreviation ;
POJO:Java Simple classes in ( In fact, it's a common JavaBeans), You don't need to inherit the parent class, and you don't need to implement any interfaces ;
This method creates action Class is more secure , bring struts2 The framework is coded *** It's a much lower chance ( Because of the simple , So the security ), Created in this way action This is done as follows :


public class TestAction{
	public String StrutsTest(){
		System.out.println("You are right!");
		return "success";

POJO Way to create Action class
This method is not commonly used , The reason is simple , It's like servlet The same , Write everything by hand , That's bullshit , Just go ahead and use servlet No. It'll be ok ~

1.2、 The way to implement an interface is to create Action

This method creates Action We need to implement a action Interface , The full name of this interface is :, Rewrite method .
Built in this way action Class is as follows :


import com.opensymphony.xwork2.Action;

public class TestAction2  implements Action{
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return null;

 The way to implement an interface is to create Action
Be careful :
1) In this way, there is a execute() Method , This method is not for programmers to implement this method , It's a cue , Prompt the programmer how to write in this situation action Method ;
2) That's the way it works action Many fields are encapsulated in the interface , These fields can be used directly when returning results ;

This method is also not very common ;

1.3、 Create by inheriting a class Action

In this way, you need to inherit a class named , The advantage of inheriting this class is that what the inherited class has done can be used directly , In this inherited class, there are many important interfaces that can be called directly , rewrite , Its action Class is created as follows :


import com.opensymphony.xwork2.ActionSupport;

public class TestAction3 extends ActionSupport{
	/* Write a method 、 Modulation method */

 Create by inheriting a class Action
This is the most common way ;

pass:Struts2 There are just three ways to create Action Kind of the way , The third method of inheritance is most commonly used ~

《 End of this chapter 》