[java framework] Struts2 -- result view and parameter receiving of struts2

Keywords: Java Struts Apache JSP xml

1. Results view of struts 2

Each atcion of Struts2 can have different result return methods. There are 11 ways to return the result type attribute in result. The main and most commonly used jump modes are redirect, redirectAction and pather.

The type of the specific jump method can be found in struts-default.xml, as follows:

<package name="struts-default" abstract="true">
    <result-types>
        <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
        <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
        <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
        <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
        <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
        <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
        <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
        <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
        <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
        <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
        <result-type name="postback" class="org.apache.struts2.dispatcher.PostbackResult" />
    </result-types>

1.1.redirectAction configuration

The redirect Action jump type is mainly used to jump to the result view of another Action.

There are two main situations:

① Access the result view of another Action in the package directory. The configuration example is as follows:

<! -- test the redirectAction jump type: access the action under the package directory
    Fill in the name value of another action in this package directly in the result view to complete another action
    Jump method in action result view -- >
<action name="index" class="cn.yif.action.UserAction" method="execute">
   <result name="success" type="redirectAction">
       testSave
   </result>
</action>
<action name="testSave" class="cn.yif.action.UserAction" method="testSave">
  <result name="Success" type="dispatcher">
      /success.jsp
  </result>
</action>

② Access the Action result view in another package (different packages). The specific configuration example is as follows:

There are two ways:

The first is to still use redirect jump mode, and configure the jump mapping path of another package in the result view: namespace/actionName

<package name="default" namespace="/" extends="struts-default">
      <!--Test accessing another package in the current package action Results view of-->
      <action name="index" class="cn.yif.action.UserAction" method="execute">
          <result name="success" type="redirect">
              /system/testSave
          </result>
        </action>
</package>
<package name="system" namespace="/system" extends="struts-default">
   <action name="testSave" class="cn.yif.action.UserAction" method="testSave">
       <result name="Success" type="dispatcher">
             /success.jsp
       </result>
   </action>
</package>

The second is to use redirectAction. You need to configure the param parameter in the result tag. The specific configuration is as follows:

<package name="default" namespace="/" extends="struts-default">
        <! -- test the redirectAction jump type: access the action under the package directory
            Fill in the name value of another action in this package directly in the result view to complete another action
            Jump method in action result view -- >
        <! -- use redirectAction to add param parameter namespace and actionName to the resu lt tag,
            Respectively corresponding to the value of namespace in another package and name of action -- >
        <action name="index" class="cn.yif.action.UserAction" method="execute">
            <result name="success" type="redirectAction">
                <param name="namespace">system</param>
                <param name="actionName">testSave</param>
            </result>
        </action>
    </package>
    <package name="system" namespace="/system" extends="struts-default">
        <action name="testSave" class="cn.yif.action.UserAction" method="testSave">
            <result name="Success" type="dispatcher">
                /success.jsp
            </result>
        </action>
</package>

1.2. Custom resultType type

The corresponding result types can be found in struts-default.xml. In < package > you can configure the custom result types to create a new return type (this return type is completely named according to yourself). The specific configuration is as follows:

<package name="default" namespace="/" extends="struts-default">
     <! -- configure result types: use the return type default configuration in struts-default.xml
            Here you can customize the name of type. For example, I define a jump type as forward -- >
     <result-types>
          <result-type name="forward" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
     </result-types>
     <action name="index" class="cn.yif.action.UserAction" method="execute">
          <result name="success" type="forward">
                /WEB-INF/view/test.jsp
          </result>
     </action>
</package>

1.3. Local results view and global results view

Partial results view:

The < result > result view configured in a < Action > tab is called a partial result view, which can only be used in the current Action.

When the Action finishes processing the user request, it will return a string, which is the name of a logical view.

<package name="default" namespace="/" extends="struts-default">
    <action name="index" class="cn.yif.action.UserAction" method="execute">
        <! -- local result view: configure in an Action tag, and configure < result > as a < Action > child element; only this Action can use -- >
        <result name="success" type="dispatcher">
            /success.jsp
        </result>
        <! -- both success and error are logical view names, which determines the response result -- >
        <result name="error" type="dispatcher">
            /error.jsp
        </result>
    </action>
</package>

Global results view:

Configure < global results > in a package, and define the name of < result > result view, which is a global result view. After configuration, all action result views under the package can be accessed.

<! -- global results view: it can be accessed in multiple action s after configuration -- >
<global-results>
   <result name="testExample">
       /WEB-INF/view/test.jsp
   </result>
</global-results>

Note: if there is a result under a < action > with the same name as the global view, the local result view will prevail during access.

<! -- global results view: it can be accessed in multiple action s after configuration -- >
  <global-results>
      <result name="testExample">
          /WEB-INF/view/test.jsp
      </result>
   </global-results>
   <action name="example" class="cn.yif.action.ExampleAction" method="test">
        <! -- I have the same name as the global resu lt view, but my will prevail -- >
        <result name="testExample" type="dispatcher">
            /WEB-INF/view/test.jsp
        </result>
    </action>

2. Three ways to create Action class

Method 1: create a simple POJO class:

/**
 *  It's just a simple pojo class
 *  A public, parameterless constructor must be provided -- Reflection needs to be called
 *  
 */
public class CreateActionOne {
    public CreateActionOne() {
    }

    public String execute(){
        return "success";
    }
}

Mode 2 implements the Action interface (note that it is the Action interface in xwork2 package):

import com.opensymphony.xwork2.Action;

public class CreateActionTwo implements Action {
    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }
}

Mode 3 inherits ActionSupport

import com.opensymphony.xwork2.ActionSupport;
/**
*  Inherit ActionSupport
*/
public class CreateActionThree extends ActionSupport {
    @Override
    public String execute() throws Exception {
        return super.execute();
    }
}

3. Wildcard * mapping path configuration

① Manage multiple result view mapping addresses by * configuring the result view method in the corresponding Action;

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <!--Use Wildcards *Definition action Medium name Property, note that the index starts at 1, so naming can manage multiple result views-->
    <package name="default" namespace="/" extends="struts-default">
        <action name="crud_*" class="cn.yif.action.CrudAction" method="{1}">
            <result name="{1}" type="dispatcher">
                /WEB-INF/view/{1}.jsp
            </result>
        </action>
    </package>
</struts>

4. Three ways of receiving parameters for struts 2

4.1. Receive common interface parameters

Struts2 receives multiple common parameters on the foreground interface. The corresponding Action and struts.xml are configured as follows:

ParamAction.java

/**
 * 1.First, you need to provide the corresponding member variable. The name of the member variable must be consistent with the name value in the form form
 * 2.The setter method of the corresponding member variable needs to be provided in the Action
 */
public class ParamAction extends ActionSupport {
    private String name;
    private String password;
    private Integer age;
    @Override
    public String execute() throws Exception {
        System.out.println(name);
        System.out.println(password);
        System.out.println(age);
        return "success";
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

Struts.xml configuration:

<!--action Medium name Corresponding form In form action-->
    <package name="default" namespace="/" extends="struts-default">
        <action name="params" class="cn.yif.action.ParamAction" method="execute">
            <result name="success" type="dispatcher">
                /success.jsp
            </result>
        </action>
    </package>

Form form in login.jsp:

<body>
    <form action="/params" method="post">
        Name: < input type = "text" name = "name" / > < br / >
        Password: < input type = "password" name = "password" / > < br / >
        Age: < input type = "number" name = "age" / >
        < input type = "submit" value = "submit" / >
    </form>
</body>

4.2. Receive Object object parameters

If there are multiple parameter values to be passed in the interface, the parameters can be encapsulated as objects for passing.

/**
 * Provide the User object properties and the getUser method
 */
public class ParamUserAction extends ActionSupport {
    private User user;
    @Override
    public String execute() throws Exception {
        System.out.println(user);
        System.out.println(user.getDept().getName());
        return "success";
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}

User and Dept:

public class User {
    private String name;
    private String password;
    private Integer age;
    private Date bornDate;
    private Boolean sex;
    private Dept dept;

    //getter/setter And toString()slightly
}

public class Dept {
    private String name;
}

Form form interface:

<form action="params2" method="post">
    User name: < input type = "text" name = "user. Name" / > < br / >
    Password: < input type = "password" name = "user. Password" / > < br / >
    Age: < input type = "number" name = "user. Age" / > < br / >
    Date: < input type = "date" name = "user. Borndate" / > < br / >
    Gender: < input type = "radio" name = "user. Sex" value = "true" > male
    < input type = "radio" name = "user. Sex" value = "false" > female < br / >
    Department: < select name = "user. Dept. name" >
    < option value = "development department" > Development Department < / option >
    < option value = "personnel department" > personnel department < / option >
    < option value = "operation Department" > operation Department < / option >
    </select>
    < input type = "submit" value = "submit" / >
</form>

4.3. Implement the ModelDriven interface

/**
 * When the interface parameter type has both objects and separate common input types, you can choose to implement modelDriven < T >
 */
public class ParamModelDrivenAction extends ActionSupport implements ModelDriven<User> {
    private User user = new User();
    private String email;

    @Override
    public String execute() throws Exception {
        System.out.println(user);
        System.out.println(email);
        return "success";
    }

    @Override
    public User getModel() {
        return user;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

Form form:

<form action="params3" method="post">
    User name: < input type = "text" name = "user. Name" / > < br / >
    Password: < input type = "password" name = "user. Password" / > < br / >
    Age: < input type = "number" name = "user. Age" / > < br / >
    Date: < input type = "date" name = "user. Borndate" / > < br / >
    Gender: < input type = "radio" name = "user. Sex" value = "true" > male
    < input type = "radio" name = "user. Sex" value = "false" > female < br / >
    Department: < select name = "user. Dept. name" >
    < option value = "development department" > Development Department < / option >
    < option value = "personnel department" > personnel department < / option >
    < option value = "operation Department" > operation Department < / option >
    </select><br/>
    Email: < input type = "text" name = "email" > < br / >
    < input type = "submit" value = "submit" / >
</form>

Posted by budz on Sat, 18 Apr 2020 02:10:09 -0700