Struts 2 built-in type converter

Keywords: Struts Java JSP Attribute

For Web applications, all request parameters are of string type. In traditional Web applications, we often need to manually code, convert the received parameters to various types of Java, or convert various types of Java parameters to strings and send them to the client. In the framework of struts 2, a powerful type conversion mechanism is provided. Developers can use this mechanism to perform any complex type conversion.

For common Java types, developers do not need to build their own type converters. These data types can be converted automatically in struts 2. The type converter provided with Struts2 supports the exchange of the following types and Strings:
*Boolean and Boolean
*Character and char acter
*int and Integer
*Float and float
*Long and long
*Double and double
* Date

The following is the demonstration code. The type.jsp page accepts parameters, which are transferred from TypeConversionAction to conversion.jsp. During the process, the type conversion of parameters is automatically completed

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>type</title>
</head>
<body>
    <s:form action="typeConversion">
        <!-- Corresponding product attribute -->
        <s:textfield label="Product 1" name="products"/>
        <s:textfield label="Product 2" name="products"/>
        <s:textfield label="Product 3" name="products"/>
        <!-- Corresponding numbers attribute -->
        <s:textfield label="Number 1" name="numbers"/>
        <s:textfield label="Number 2" name="numbers"/>
        <s:textfield label="Number 3" name="numbers"/>
        <!-- Corresponding collections attribute -->
        <s:textfield label="Set 1" name="collections"/>
        <s:textfield label="Set 2" name="collections"/>
        <s:textfield label="Set 3" name="collections"/>
        <s:submit value="Submission"/>
    </s:form>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>conversion</title>
</head>
<body>
    <b>product</b><br/>
    <s:property value="products[0]"/>
    <s:property value="products[1]"/>
    <s:property value="products[2]"/><p/>
    <b>number</b><br/>
    <s:property value="numbers[0]"/>
    <s:property value="numbers[1]"/>
    <s:property value="numbers[2]"/><p/>
    <b>aggregate</b><br/>
    <s:property value="collections[0]"/>
    <s:property value="collections[1]"/>
    <s:property value="collections[2]"/>
</body>
</html>
import com.opensymphony.xwork2.Action;

import java.util.List;

public class TypeConversionAction implements Action{
    private String[] products = new String[3];
    private int[] numbers = new int[3];
    //Collection is not initialized
    private List<Integer> collections;

    public String[] getProducts() {
        return products;
    }

    public int[] getNumbers() {
        return numbers;
    }

    public List<Integer> getCollections() {
        return collections;
    }

    public void setProducts(String[] products) {
        this.products = products;
    }

    public void setNumbers(int[] numbers) {
        this.numbers = numbers;
    }

    public void setCollections(List<Integer> collections) {
        this.collections = collections;
    }

    @Override
    public String execute() {
        return "conversion";
    }
}
<?xml version="1.0" encoding="UTF-8"?>

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

<struts>
    <package name="testType" extends="struts-default">
        <action name="typeConversion" class="chapter10.part2p1.TypeConversionAction">
            <result name="conversion">/chapter10/part2p1/conversion.jsp</result>
        </action>
    </package>
</struts>

Posted by IMP_TWI on Tue, 14 Apr 2020 12:14:17 -0700