#Problem with sending params form thymeleaf to controller

1 messages · Page 1 of 1 (latest)

red pebble
#

Hello, i have:

<form th:action="@{/products/co-cwu/save(coId='2L')}" method="get">

and I want to send it to controller.

@GetMapping("/co-cwu/save") public String saveCOCWUToClient(@RequestParam(value = "coId", required = false) Long coId {

it shows me null for coId, why? 😦 Its about (coId='2L')} I think but what I need to write here? I tried "2" not 2L for Long too, its always shows null. Help me please.

celest rapidsBOT
#

<@&1004656351647117403> please have a look, thanks.

torn sand
#

Why are u hardcoding it to 2L? Just do action="/products/co-cwu/save?coId=2" then

#

or @{/products/co-cwu/save?coId={coid}(coid=${2L})}

#

Or i guess @{/products/co-cwu/save(coId=${2L})} should work, didnt know there was a shorthand

#

If u put the literal '2L' it will try to parse that to a long which isnt a long it has string chars in it

#

So either (coId=2) or escape with (coId=${2L}) but as originally mentioned if ur hardcoding just hardcode ?coId=2

#

@red pebble

red pebble
#

Thank you for your answer. "@{/products/co-cwu/save?coId=2} still shows null

#

?coId={coid}(coid=${2L}) or ?coId={coid}(coid=${2}) shows null. I will try create AJAX GET with this url

red pebble
#

I add clientId as a pathvariable too.

<button onclick="saveProducts()">Zapisz urządzenia</button>

function saveProducts() {

    $.ajax({
        type: "GET",
        url: '/products/co-cwu/save' + clientId + '/?coId=2&cwuId=2',
        success: function(result) {
        console.log("Udalo sie!");
        }
        });
        }

}
It shows: Uncaught ReferenceError: saveProducts is not defined at HTMLButtonElement.onclick (752?cwuname=Panasonic+Nierdzewny+200l&coname=Panasonic+Nierdzewny+50l&hotWaterCirculation=yes&heatingCircuits=3:125:42)

I'm seriously trying with this about 10 hours, please help.

torn sand
#

Do u know how to debug the network tab in a browser?

#

That ajax call doesnt match ur controller

red pebble
#

I know how to find some errors here only, now I see something new:
I will check it

torn sand
#

Also is that controller prefixed with /products at the top

#

Click the extra arrows and go to network

red pebble
#

yes, in controller is prefix on the class:

@Controller
@RequestMapping("/products/")
public class ProductController {

Okey, i'm here in netowrk tab

torn sand
#

make sure to preserve log incase you get a redirect

red pebble
torn sand
#

you have to make a request. refresh and click the button

red pebble
#

I will answer with some breaks, I'm in job

torn sand
#

i also recomment poping it out

red pebble
#

No any response, when I try:
Its only send something about refreshing page - its my another GET /show with request params too

torn sand
#

what is 752?...

#

and its not making any requests now because your ajax isnt running based on the console log

#

you'd have to revert to the original thymeleaf rendering

#

and then you could inspect the rendered HTML element

red pebble
#

752 is clientId, I can send you repo on priv if you want to have it more clear

#

i will check it

torn sand
#

unless its confidential you should just be able to post it

torn sand
#

save is the only thing that doesnt expect a PathVariable

#

why is the client ID 752 there but not the others

#

like its hardcoded

red pebble
#

ah, sorry, its not commited, ok I'm after job now

#

one minute

#

okey, now

#

I'm thinking about using show method to save this models... its took too many time for me

torn sand
#

is it supposed to redirect to forms/salesmanClients

#

oh wait ur not hidding that code yet is the issue

#

What is the entry point that has the template that should get to that endpoint

#

ok i see them

red pebble
#

it starts frm co-cwu/search to co-cwu/search, both return the same file clientCWUCO, when where you put button you will use ajax save

#

to co-cwu/show*

#

first I create form which shows data form /show to using form th:action and send get but here also was errors

torn sand
#

your inline javascript wont compile you have an extra closing }

red pebble
#

:v i'm checking this

torn sand
#

also you have multiple forms defined

red pebble
#

this intellij with js plugins is hard for JS

torn sand
#

i'm using intelliJ ultimate edition

#

no plugins

#

no plugins affecting JS/compatibility*

red pebble
#

it was good when I was student, now I want to sell this program to have money

#

xd

torn sand
#

you need a slash / here if you're going to append these with your controller path

#

and not at the end

#

i would use string templating

red pebble
#

when I see your helping me its making me for starting believing in people, im creating this app about 150 hours, mostly of them was about solving some bugs

torn sand
#

also ajax success you still need to check the status code because 400/500 is a "success" still

red pebble
#

ufff, it shows good result....

#

now I can put this third and fourth param and save it to clientpanel, I think it will works

#

I have more problems to solve if you have time, I wil be tomorrow more active

#

maybe I also can do something for you.

#

not clientpanel, clientproducts*

torn sand
#

i can solve them now if you want

#

i still dont know why you're passing in variables like that to the template

#

but my mental scope is narrow since i havent even run the program

#

aren't clientId/coId/cwuId in the URL already?

red pebble
#

My thymeleaf and js skills are ugly but I want to use clientCWO first for find good products - co and cwu and after that shows it on the same template with able to save it by clickin the button

#

it were in /show, yes, i'm getting crazy with it so somebody can really find easier way to do thid

#

this*

#

too many tutorial, chat gpt, no fundamentals about thymeleaf i think

#

my mentor doesn't solve this even when I paid him so its a bit funny

torn sand
#

you can parse window.location.search

#
const urlParams = new URLSearchParams(window.location.search);
uri = `/projects/co-cwu/save/TODO-CLIENT?{$urlParams}`;
#

client ID can also be parsed

#

they generally shouldnt be injected from thymeleaf

red pebble
#

hm, how I can inform JS to find coId, cwuId using this method?

torn sand
#

what i have there: js const urlParams = new URLSearchParams(window.location.search);

#

it reads the current URL

#

of the web browser

red pebble
#

aaaaa

#

:>

torn sand
#

specifically the query parameters

red pebble
#

beautiful

#

I will include it tomorrow, very thank you Xorium

#

next:

<div class="dropdown">
<a class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Budynek
</a>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<a class="dropdown-item" th:href="@{/buildings/clients/add/{id}(id=${client.id})}">Dodaj dane</a>
<a class="dropdown-item" th:href="@{/buildings/clients/edit/{id}(id=${client.id})}">Edytuj dane</a>
<a class="dropdown-item" th:href="@{/buildings/clients/show/{id}(id=${client.id})}">Wyświetl dane</a>
<a class="dropdown-item" th:href="@{/buildings/clients/delete/{id}(id=${client.id})}">Usuń dane</a>
</div>
</div>

This dropdown.. doesn't want to open

#

its salesmanClients in forms

#

here is bootrstrap with thymeleaf

torn sand
#
const pathname = window.location.pathname;
const pathSegments = pathname.split('/');
const clientId = pathSegments[pathSegments.length - 1]; // Get the last segment
const urlParams = new URLSearchParams(window.location.search);
uri = `save/${clientId}?${urlParams}`;
red pebble
#

I understand, very thank you

#

If you can help with this dropdown, It will be also useful

torn sand
#

yup you didnt include it

#

you really should create a thymeleaf layout or fragment for your header

#

also why are you using bootstrap 4?

#

(as apposed to 5)

#

according to their documentation (if you're staying on 4) you should use/set it up like ```html
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">
</head>
<body>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-Fy6S3B9q64WdZWQUiU+q4/2Lc9npb8tCaSX9FK7E8HnRr0Jz8D6OP9dO5Vg3Q9ct" crossorigin="anonymous"></script>
</body>

#

according to their documentation (if you're staying on 4) you should use/set it up like ```html
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">
</head>
<body>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-Fy6S3B9q64WdZWQUiU+q4/2Lc9npb8tCaSX9FK7E8HnRr0Jz8D6OP9dO5Vg3Q9ct" crossorigin="anonymous"></script>
</body>```

torn sand
#

(with bootstrap 4) you can make a simple page template fragment like ```html
<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org">
<head>
<th:block th:fragment="standard-head">
<title th:text="${PAGE_TITLE} ?: 'My Default Title'"></title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">

<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Lato&display=swap">
<link rel="stylesheet" href="https://fonts.google.com/specimen/Bitter">

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">
<link rel="stylesheet" th:href="@{/css/styles.css}">

</th:block>
</head>
<body>
<header>
Placeholder Header
</header>

<footer>
Placeholder Footer
</footer>
<th:block th:fragment="closing-scripts">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-Fy6S3B9q64WdZWQUiU+q4/2Lc9npb8tCaSX9FK7E8HnRr0Jz8D6OP9dO5Vg3Q9ct" crossorigin="anonymous"></script>
<script th:src="@{/js/functions.js}"></script>
</th:block>
</body>
</html>

#

Then a specific page could be like html <!doctype html> <html lang="en" xmlns:th="https://www.thymeleaf.org"> <head> <th:block th:replace="~{template :: standard-head}"></th:block> <link rel="stylesheet" th:href="@{/css/specific_page.css}"> </head> <body> <header th:replace="~{template :: header}"></header> <main> Hello world! </main> <footer th:replace="~{template :: footer}"></footer> <th:block th:replace="~{template :: closing-scripts}"></th:block> <script> console.log("Extra scripting!"); </script> </body> </html>
Theres ways to make it better with other libraries like thymeleaf layouts

celest rapidsBOT
red pebble
#

Hello, okey, so what I need to do with template? I dont undestand how to use template and base on it to create better working thymeleaf pages

#

For sure i will need it more clearly and tidy. I want to use something like admin LTE for creating salesman panel

#

but this template look like it will be similiar

#

a bit

#

like normal header, body, footer, I needed it

torn sand
#

I sent a sample of what you'd need just put it in the root of ur static templates

#

Idk what admin lte is

torn sand