Action implementation

1, there are three ways in Struts2 can create a class action process

 

  (1) to create the class action process by implementing the Action interface (advantage: making our code more standardized)

 

  ① Struts2 new projects, jar package manually introduced (into WEB-INF / lib directory), arranged struts.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
    <package name="default" namespace="/" extends="struts-default">
        <action name="hello" class="cn.ht.action.HelloAction">
            <result>/index.jsp</result>
        </action>
    </package>
</struts>

  ② configuration web.xml (IDEA automatically generated)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

  ③index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>Action</title>
  </head>
  <body>
  action execute
  </body>
</html>

  ④ HelloAction.java created in the src

Package Penalty for cn.ht.action; 

Import com.opensymphony.xwork2.Action; 

//   create a handler class by implementing the Action interface
 //    benefits: implement the Action interface may use direct action to provide a constant
 //           must override the default processing method
 //   this method is less 
public  class HelloAction the implements the Action { 
    @Override 
    public String execute () throws Exception { 
        System.out.println ( "class Action processing is performed" );
         return SUCCESS; 
    } 
}

  ⑤ access

  (2) to create a class by inheriting ActionSupport handling of Action, struts2 recommend using this approach (benefits: can inherit some of the ActionSuport implement features such as: validation; official recommended)

  Hello1Action.java
Package Penalty for cn.ht.action; 

Import com.opensymphony.xwork2.ActionSupport; 

/ ** 
 * ActionSupport implements the Action interface 
 * and ActionSupport class provides the functionality provided by many other struts2 
 * such as: data validation, internationalization 
 * Create after there default implementation 
 * / 
public  class Hello1Action the extends ActionSupport { 
}

  struts.xml (add black section)

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
    <package name="default" namespace="/" extends="struts-default">
        <action name="hello" class="cn.ht.action.HelloAction">
            <result>/index.jsp</result>
        </action>
        <action name="hello1" class="cn.ht.action.Hello1Action">
            <result>/HelloAction.jsp</result>
        </action>
    </package>
</struts>
  HelloAction.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>HelloAction</title>
</head>
<body>
<h1>HelloAction</h1>
</body>
</html>

  access

  (3) non-invasive achieved (advantage: Custom java class can be a regular, having no invasive)

  Hello2Action.java
Package cn.ht.action; 

/ ** 
 * non-invasive implementation 
 * / 
public  class Hello2Action {
     public String Execute () { 
        System.out.println ( "non-invasive implementation" );
         return "Success" ; 
    } 
}

  struts.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
    <package name="default" namespace="/" extends="struts-default">
        <action name="hello" class="cn.ht.action.HelloAction">
            <result>/index.jsp</result>
        </action>
        <action name="hello1" class="cn.ht.action.Hello1Action">
            <result>/HelloAction.jsp</result>
        </action>
        <action name="hello2" class="cn.ht.action.Hello2Action">
            <result>/Hello2Action.jsp</result>
        </action>
    </package>
</struts>

  Hello2Action.jsp

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019-8-13
  Time: 16:14
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Hello2Action</title>
</head>
<body>
<h1>无侵入性的实现</h1>
</body>
</html>

  access

 

Guess you like

Origin www.cnblogs.com/Anemia-BOY/p/11346753.html