Thymeleaf th:action equal to link expression which contains a context relative URL, or another, based on condition

The context:
I’ve been developing a Proof of Concept application (for the sake of learning) with Java 17, Spring Framework, and in the front end HTML, CSS and Thymeleaf. The inventory view shows each Item object (Which has attributes like, very importantly, the Unique Identifier, price, name, discount, etc…) in my “Data Base” (ArrayList, for the sake of simplicity), along with the update button at the right side of every single Item. In case the Update button is pressed, consequently the user is redirected to a view item_form.html which posess a form with prepopulated information from the respective item the user desired to update. The way I know in the front end if such form was accessed via the inventory view it’s because the Spring Model might (or might not) contain an id key (Which is the Unique Identifier of the Item object my StoreController sent). I find hard to illustrate my idea so I’ll be showing the necessary code to reach my problem:

This is my inventory view:

<html xmlns:th="http://www.thymeleaf.org">
    <head> 
        <link th:href="@{/inventory-stylesheet.css}" rel="stylesheet"> 
    </head>    
    <body>
        <div class="topnav">
            <a  th:href="@{/}">HOME</a>
            <a  th:href="@{/form}">FORM</a>
            <a class="active" th:href="@{/inventory}">INVENTORY</a>
        </div>
        <div class="alert success" th:if="${submissionState} == true">
          <strong>Item submission was successfull!</strong>
        </div> 
        <div class="container">
           <table>
               <tr class="head">
               <th>UUID</th>
               <th>Product Name</th>
               <th>Category</th>
               <th>Price</th>
               <th>Discount</th>
               <th>Order Date</th>
               <th> </th>
              </tr>
              <tr th:each="item: ${storedItems}">
                 <td th:text="${item.id}"></td>
                 <td th:text="${item.name}"></td>
                 <td th:text="${item.category}"></td>
                 <td type="currency" th:text="${item.price}"></td>
                 <td type="percentage" th:text="${item.discount}"></td>
                 <td type="date" th:text="${#dates.format(item.date, 'yyyy-MM-dd')}"></td>
                 <td>
                    <a role="button" style="color:white; background-color: #3333f0" class="table-button" th:href="@{/getUpdateView(id=${item.id})}">Update</a>
                 </td>
              </tr>
           </table>
        </div>
   </body>
</html>

This is the handler method mapped to /getUpdateView(id=${item.id})

@GetMapping("/getUpdateView")
    public String getUpdateView(Model model, @RequestParam String id) {
        Item item = itemService.getObjectById(id);
        logger.log(Level.INFO, "Attempting to update " + item.toString());
        model.addAttribute("categories", Constants.CATEGORIES);
        model.addAttribute("item", item);
        model.addAttribute("id", id);
        return "item_form";
    }

This is the item_form.html view which posess the form I’m talking about:

<html xmlns:th="http://www.thymeleaf.org">
    <head> 
        <link th:href="@{/form-stylesheet.css}" rel="stylesheet"> 
    </head>    
    <body>
        <div class="topnav">
            <a th:href="@{/}">HOME</a>
            <a class="active" th:href="@{/form}">FORM</a>
            <a th:href="@{/inventory}">INVENTORY</a>
        </div>
        <div class="container">
            <h2> Item submission </h2>
            <form method="post" th:object="${item}" th:action="@{(#model.containsKey('id') ? '/submitItem(id=${model.id})' : '/submitItem')}">
                <p th:errors="*{category}" style="color:red"></p>
                <select class="select" th:field="*{category}">
                    <option style="color:rgb(90, 90, 255)" value="">Choose Category</option>
                    <option th:each="category: ${categories}" th:text="${category}" th:value="${category}"> Placeholder </option>
                </select>
                <p>Name</p>
                <p th:errors="*{name}" style="color:red"></p>
                <input type="text" placeholder="Name" th:field="*{name}">
                <p>Price</p>
                <p th:errors="*{price}" style="color:red"></p>
                <input type="text" placeholder="Price" th:field="*{price}">
                <p>Discount (0% - 100%)</p>
                <p th:errors="*{discount}" style="color:red"></p>
                <input type="text" placeholder="Discount" th:field="*{discount}">
                <p>Order Date</p>
                <p th:errors="*{date}" style="color:red"></p>
                <input type="date" style="color:black" th:field="*{date}">
                <input type="submit" value="Submit Item">
            </form>
        </div>
    </body>
</html>

And this is my actual problem: When I press on “Submit Item” button, I get an error:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon Jun 10 22:05:03 ART 2024
There was an unexpected error (type=Not Found, status=404).
No message available

What I want to happen: In case #model.containsKey('id') returns true, set th:action equal to @{/submitItem(id=${model.id})}, in case it returns false I want th:action to contain @{/submitItem}.

New contributor

Marcus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật