Home Java javaTutorial Detailed introduction to parameter passing in struts2

Detailed introduction to parameter passing in struts2

Mar 02, 2017 am 11:25 AM

This problem has always puzzled me. When writing ordinary jsp programs, it is easy to pass parameters through forms, requests, links, etc. However, in struts2, I cannot see them in every place I write. When it comes to any request or response, I don’t know how to pass parameters. Today I learned the parameter passing section in struts2 and finally solved the doubt, but it is not very clear yet and needs to be explored in the future.

Let me talk here about how to pass parameters between Actions in struts2. Parameter transfer between Actions is configured in the struts.xml file. In the result element, use the param tag to specify the name and value of the passed parameter. However, this parameter name is not written casually. It must be consistent with the Action to be passed to. corresponding to the attribute name. Let’s illustrate with an example.

Example: There is a Login.jsp page with two elements: username and password. The data needs to be submitted to Action1. Action1 then requests forwarding or redirection to Action2, and Action1 requests forwarding or redirection to In the process of Action2, the two parameters of user name and password, plus a customized constant parameter, must be passed to Action2. Action2 receives the parameters and displays the parameters to a result page: result.jsp


Code:

Login.jsp:  
<form action="/struts2/test/action1" method="post">  
        姓名:<input type="text" name="username"/><br/>  
        密码:<input type="password" name="password"/><br/>  
        <input type="submit" value="提交"/>  
        <input type="hidden" name="type" value="something"/>  
    </form>
Copy after login
struts.xml:  
<action name="action1" class="com.suo.actions.Action1">  
     <result name="success" type="redictAction"><!--type指定是请求转发还是重定向-->  
        <param name="actionName">action2</param><!--在这里指定要请求转发或是重定向到的Action-->  
                  
        <param name="username">${username}</param>  
        <param name="password">${password}</param><!--action1中的属性值-->  
        <param name="myparam">piao</param><!-- 自定义的不变参数 -->  
                  
        <!--在这里定义的参数,在传到的Action中,都要有相应的set/get方法,才能够得到该参数 ,  
                    并且在传递到的Action中的属性名,要和参数的name保持一致-->  
     </result>  
</action>  
          
<action name="action2" class="com.suo.actions.Action2">  
<span>  </span><result name="success">/WEB-INF/result/action.jsp</result>  
</action>
Copy after login
Action1.java:  
  
package com.suo.actions;  
  
import com.opensymphony.xwork2.ActionSupport;  
  
public class Action1 extends ActionSupport {  
      
    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;  
    }  
      
    public String execute()  
    {  
        System.out.println(type);  
        return SUCCESS;  
    }  
}
Copy after login
Action2.java:  
  
package com.suo.actions;  
  
import com.opensymphony.xwork2.ActionSupport;  
  
public class Action2 extends ActionSupport {  
      
    private String username;  
    private String password;  
    private String myparam;  
      
    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;  
    }  
      
    public String getMyparam() {  
        return myparam;  
    }  
    public void setMyparam(String myparam) {  
        this.myparam = myparam;  
    }  
    public String execute()  
    {  
        System.out.println(username);  
        System.out.println(password);  
        System.out.println(myparam);  
          
        return SUCCESS;  
    }  
}
Copy after login
result.jsp:  
  
<body>  
    username:<s:property value="username"/><br>  
    password:<s:property value="password"/><br>  
    myparam:<s:property value="myparam"/><br>  
      
    <!-- 这里可以用标签得到的属性值,必须是在Action中有对应的set/get方法才可以 -->  
      
  </body>
Copy after login

The above is a detailed introduction to parameter passing in struts2 For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to view Struts2 historical vulnerabilities from a protection perspective How to view Struts2 historical vulnerabilities from a protection perspective May 13, 2023 pm 05:49 PM

1. Introduction The Struts2 vulnerability is a classic series of vulnerabilities. The root cause is that Struts2 introduces OGNL expressions to make the framework flexible and dynamic. With the patching of the overall framework improved, it will now be much more difficult to discover new Struts2 vulnerabilities than before. Judging from the actual situation, most users have already repaired historical high-risk vulnerabilities. Currently, when doing penetration testing, Struts2 vulnerabilities are mainly left to chance, or it will be more effective to attack unpatched systems after being exposed to the intranet. Online analysis articles mainly analyze these Struts2 vulnerabilities from the perspective of attack and exploitation. As the new H3C offense and defense team, part of our job is to maintain the rule base of ips products. Today we will review this system.

Best practices for optimizing Golang function parameter passing performance Best practices for optimizing Golang function parameter passing performance Apr 13, 2024 am 11:15 AM

In order to optimize Go function parameter passing performance, best practices include: using value types to avoid copying small value types; using pointers to pass large value types (structures); using value types to pass slices; and using interfaces to pass polymorphic types. In practice, when passing large JSON strings, passing the data parameter pointer can significantly improve deserialization performance.

What is the principle of Struts2 framework What is the principle of Struts2 framework Jan 04, 2024 pm 01:55 PM

The principle of the Struts2 framework: 1. The interceptor parses the request path; 2. Finds the complete class name of the Action; 3. Creates the Action object; 4. Execute the Action method; 5. Returns the result; 6. View parsing. Its principle is based on the interceptor mechanism, which completely separates the business logic controller from the Servlet API, improving the reusability and maintainability of the code. By using the reflection mechanism, the Struts2 framework can flexibly create and manage Action objects to process requests and responses.

Struts2 vulnerability S2-001 example analysis Struts2 vulnerability S2-001 example analysis May 15, 2023 pm 03:58 PM

Vulhub vulnerability series: struts2 vulnerability S2-0011. Vulnerability description: struts2 vulnerability S2-001 is when the user submits form data and verification fails, the server uses OGNL expression to parse the parameter value previously submitted by the user, %{value} and refills the corresponding form data. For example, in a registration or login page. If the submission fails, the server will usually default to returning the previously submitted data. Since the server uses %{value} to perform OGNL expression parsing on the submitted data, the server can directly send the payload to execute the command. 2. Vulhub vulnerability exploitation: Using vulhub to reproduce vulnerabilities can save the environment construction process, which is very convenient. vu

Detailed explanation of C++ function parameters: Performance optimization of parameter passing in parallel programming Detailed explanation of C++ function parameters: Performance optimization of parameter passing in parallel programming Apr 27, 2024 pm 02:09 PM

In a multi-threaded environment, function parameter passing methods are different, and the performance difference is significant: passing by value: copying parameter values, safe, but large objects are expensive. Pass by reference: Passing by reference is efficient, but function modifications will affect the caller. Pass by constant reference: Pass by constant reference, safe, but restricts the function's operation on parameters. Pass by pointer: Passing pointers is flexible, but pointer management is complex, and dangling pointers or memory leaks may occur. In parallel summation, passing by reference is more efficient than passing by value, and passing by pointer is the most flexible, but management is complicated.

How does the Struts2 S2-059 remote code execution vulnerability reproduce? How does the Struts2 S2-059 remote code execution vulnerability reproduce? May 23, 2023 pm 10:37 PM

0x00 Introduction Struts2 is a very powerful JavaWeb open source framework launched by the Apache software organization, which is essentially equivalent to a servlet. Struts2 is based on MVC architecture and has a clear framework structure. It is usually used as a controller to establish data interaction between models and views, and is used to create enterprise-level Java web applications. It utilizes and extends the JavaServletAPI and encourages developers to adopt the MVC architecture. Struts2 takes the excellent design ideas of WebWork as the core, absorbs some advantages of the Struts framework, and provides a neater Web application framework implemented in the MVC design pattern. 0x01 vulnerability

Research on parameter passing methods in Go language Research on parameter passing methods in Go language Apr 03, 2024 pm 02:48 PM

In the Go language, there are two main ways to pass function parameters: value passing: passing a copy of the variable will not affect the original variable in the calling code. Pointer passing: Passing the address of a variable allows the function to directly modify the original variable in the calling code.

How to pass parameters to PHP function? How to pass parameters to PHP function? Apr 10, 2024 pm 05:21 PM

PHP functions can pass values ​​through parameters, which are divided into pass by value and pass by reference: pass by value: modification of parameters within the function will not affect the original value; pass by reference: modification of parameters within the function will affect the original value. In addition, arrays can also be passed as parameters for operations such as calculating the sum of data.

See all articles