Struts 2 web Element Access and Template Inclusion and Default Action Use

Keywords: Java Session Struts JSP xml

The last article introduced how to use Action for parameter reception and simple form validation. This article introduces three ways of accessing web elements (request, session, application), template inclusion and default action.

Let me first look at how Action accesses web elements: 1. Map-based data transfer: IOC (control inversion); 2. Map-based data transfer: Struts 2; 3. Original data type parameter transfer: IOC; 4. Original data type parameter transfer: Struts 2. Next, we will introduce one by one. There are many methods, and finally, we will introduce them. Choose according to your habits. Let's start with the introduction of data transmission in Map mode. Here we briefly introduce the concept of IOC. IOC is called control inversion. Generally speaking, IOC is controlled by itself and controlled by others. Let's take a look at how to use IOC to implement data transfer using Map.

Here we continue to implement a project, using the form of ModelDriven to receive the user's incoming parameters. My Action Object:

/***
 * Map Way of data transfer: IOC (control inversion)
 */
@SuppressWarnings("serial")
public class UserAction extends ActionSupport implements ModelDriven<User>{
    
    private User user = new User();
    private Map request;
    private Map session;
    private Map application;
    
    public UserAction(){
        request = (Map) ActionContext.getContext().get("request");//Inversion of control
        session = ActionContext.getContext().getSession();//Inversion of control
        application = ActionContext.getContext().getApplication();//Inversion of control
    }
    
    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }
    
    public String login(){
        System.out.println("name="+user.getName()+"  Pwd="+user.getPassword());
        //Use request Mode reference
        request.put("name", user.getName()+"");
        request.put("password", user.getPassword()+"");
        
        //Use session Mode reference
        session.put("name", user.getName()+"");
        session.put("password", user.getPassword()+"");
        
        //Use application Mode reference
        application.put("name", user.getName()+"");
        application.put("password", user.getPassword()+"");
        return SUCCESS;
    }

    public User getModel() {
        return user;
    }
    
}

How do our jsp pages receive the parameters we pass?

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  
  <body>
    Hello Struts2<br>
    name:<%=request.getAttribute("name") %><br>
    password:<%=request.getAttribute("password") %><br><br>
    
    name:<%=session.getAttribute("name") %><br>
    password:<%=session.getAttribute("password") %><br><br>
    
    name:<%=application.getAttribute("name") %><br>
    password:<%=application.getAttribute("password") %><br>
  </body>
</html>

Next, let's look at the second Map approach to data transfer: relying on Struts 2 implementations:

/***
 * Map Way of Data Transfer: Depending on Struts 2
 */
@SuppressWarnings("serial")
public class UserAction extends ActionSupport implements ModelDriven<User>,RequestAware,SessionAware,ApplicationAware{
    
    private User user = new User();
    private Map<String, Object> request;
    private Map<String, Object> session;
    private Map<String, Object> application;
    
    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }
    
    public String login(){
        System.out.println("name="+user.getName()+"  Pwd="+user.getPassword());
        //Use request Mode reference
        request.put("name", user.getName());
        request.put("password", user.getPassword());
        
        //Use session Mode reference
        session.put("name", user.getName());
        session.put("password", user.getPassword());
        
        //Use application Mode reference
        application.put("name", user.getName());
        application.put("password", user.getPassword());
        return SUCCESS;
    }

    public User getModel() {
        return user;
    }

    public void setRequest(Map<String, Object> request) {
        this.request = request;
    }

    public void setSession(Map<String, Object> session) {
        this.session = session;
    }

    public void setApplication(Map<String, Object> application) {
        this.application = application;
    }
    
}

The way jsp page receives parameters is the same as above, so we will not elaborate on it here.

Following is our third original data type parameter transfer: IOC mode:

/***
 * Initial Data Type Parameter Transfer: IOC
 * IOC: IoC
 * Controlling Inversion: Controlling by oneself or by others
 */
@SuppressWarnings("serial")
public class UserAction extends ActionSupport implements ModelDriven<User>{
    
    private User user = new User();
    private HttpServletRequest request;
    private HttpSession session;
    private ServletContext application;
    
    public UserAction(){
        request = ServletActionContext.getRequest();
        session = request.getSession();
        application = session.getServletContext();
    }
    
    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }
    
    public String login(){
        System.out.println("name="+user.getName()+"  Pwd="+user.getPassword());
        //Use request Mode reference
        request.setAttribute("name", user.getName());
        request.setAttribute("password", user.getPassword());
        
        //Use session Mode reference
        session.setAttribute("name", user.getName());
        session.setAttribute("password", user.getPassword());
        
        //Use application Mode reference
        application.setAttribute("name", user.getName());
        application.setAttribute("password", user.getPassword());
        return SUCCESS;
    }

    public User getModel() {
        return user;
    }
}

Next is the parameter passing of our original data type: relying on Struts 2

/***
 * Passing parameters from raw data types: depending on Struts 2
 */
@SuppressWarnings("serial")
public class UserAction extends ActionSupport implements ModelDriven<User>,ServletRequestAware{
    
    private User user = new User();
    private HttpServletRequest request;
    private HttpSession session;
    private ServletContext application;
    
    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }
    
    public String login(){
        System.out.println("name="+user.getName()+"  Pwd="+user.getPassword());
        //Use request Mode reference
        request.setAttribute("name", user.getName());
        request.setAttribute("password", user.getPassword());
        
        //Use session Mode reference
        session.setAttribute("name", user.getName());
        session.setAttribute("password", user.getPassword());
        
        //Use application Mode reference
        application.setAttribute("name", user.getName());
        application.setAttribute("password", user.getPassword());
        return SUCCESS;
    }

    public User getModel() {
        return user;
    }

    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
        session = request.getSession();
        application = session.getServletContext();
    }
}

The knowledge about Action accessing web elements to pass parameters is introduced here. Next, we will briefly understand the use of module inclusion and default Action:

First of all, our module contains (include), which is mainly used in my struts. xml. We know that our development is generally collaborative development, different people are responsible for different modules, if they are all modified on a configuration file, that is more troublesome, that is, we can use the include attribute, different developers manage their own struts.xml. Finally, different developers'configuration files are referenced through include.

<?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="true" />
    <constant name="struts.devMode" value="true" />
    
    <include file="struts2.xml"></include>

</struts>

Finally, let's look at the default Action settings:

<package name="default" namespace="/" extends="struts-default">
    
        <default-action-ref name="index"></default-action-ref>
    
        <action name="index" class="com.edu.action.UserAction">
            <result>
                /index.jsp
            </result>
        </action>
        <!-- access control -->
        <action name="hell" class="com.edu.action.UserAction">
            <result>
                /hello.jsp
            </result>
        </action>
    </package>

default-action-ref is to set our default access action configuration information, so that when we can not find the corresponding action in the URL address entered by the browser, the default call index.action.

<default-action-ref name="index"></default-action-ref>

Today, the action aspect of Struts 2 has been introduced to you. In the next article, we will discuss a configuration of the result under action, and continue to update the following content.

Posted by dicamille on Fri, 12 Apr 2019 11:54:32 -0700