How to bind multiple objects when Spring MVC uses modelAttribute to bind form elements and submit a form

I know that spring form can be used to bind form elements to pojo. As follows

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

    <h1>add Stock</h1>

        <form:form action="saveStock" modelAttribute="stock" method="POST">
        
            <table>
                <tbody>
                
                    <tr>
                        <td><label>Base Rate Of Return:</label></td>
                        <td><form:input path="baseRateOfReturn" /></td>
                    </tr>
                
                     <tr>
                        <td><label>Asset Code:</label></td>
                        <td><form:input path="code" /></td>
                    </tr>
                
                    <tr>
                        <td><label>Asset Label:</label></td>
                        <td><form:input path="label" /></td>
                    </tr> 

                    <tr>
                        <td><label>Quarterly Dividend:</label></td>
                        <td><form:input path="quarterlyDividend" /></td>
                    </tr>
                    

                    
                    <tr>
                        <td><label>Price:</label></td>
                        <td><form:input path="sharePrice" /></td>
                    </tr>
                    
                    <tr>
                        <td><label>Beta:</label></td>
                        <td><form:input path="beta" /></td>
                    </tr>
                    
                    <tr>
                        <td><label></label></td>
                        <td><input type="submit" value="Save"/></td>
                    </tr>

                
                </tbody>
            </table>
        
        
        </form:form>
        
         <a href="${pageContext.request.contextPath}/assets">Back to List</a>


</body>
</html>

modelAttribute corresponds to a pojo object Stock, input that is directly bound to the Stock object and generates an instance. For example, < form:input path= "beta" / > assigns the beta attribute in the instance stock.

some of the related controller codes are as follows

    @GetMapping("/addStockForm")
    public String addStockForm(Model theModel){
        Asset stocks = new Stock();
        
        theModel.addAttribute("stock", stocks);
        
        return "addStockForm";
    }

and

    @PostMapping("/saveStock")
    public String saveStock(@ModelAttribute("stock") Stock tempStock) {

        service.addStock(tempStock);
        
        return "redirect:/assets";
    }

I know how to use this form to bind elements in an object, but I don"t know how to bind two objects. For example, if I want to bind both Stock and Address object elements and pass them in submmit, how should I achieve this? -sharp-sharp-sharp problem description

Nov.09,2021

the first answer to the stackoverflow question and answer answers my question

https://stackoverflow.com/que.[Spring MVC Multiple ModelAttribute On the Same Form
] [1]

Menu