📺 Develpreneur YouTube Episode

Video + transcript

Spring Boot - Thymeleaf form and options from a Map

2024-01-10 •Youtube

Detailed Notes

We are continuing a series of tutorials focused on Spring Boot. The crux of the project is to convert/migrate an old JSP/Tomcat application to a modern Spring Boot application and Java 7/8 to a current version. We are working on a complex form for entering data. We our working on form entry and digging through setting up a drop down list with a map as a source and then looking at some string-Double issues.

You can learn more through our online classes at https://school.develpreneur.com and register for free. Registration will add you to our email list, and you will periodically receive coupons for courses and notifications of the latest releases.

Transcript Text
[Music]
well hello and welcome back we are
continuing going through our little app
conversion here using spring Boot and
time leaf and this time around we are
going to look at a specific little thing
we ran in before we were building
options out using this
list actually it's a it's a map uh if
you go back to the status list you will
see somewhere in here where I've got the
hello controller we can see that we've
got this status list and actually we can
see right here it's a it's a map of a
string and a string and so what we
actually want to do when we get into
displaying our dropdowns instead of just
having the same thing as a value as we
have for the text we actually want it to
be the key and the value now there's a
couple ways we can do
this and in this case uh what I'm going
to do
is I'm going to do um and I'm just
pasting this in because for each of
these options instead what we're going
to do is we can actually Within just as
we did in some others we do some fours
we're going to come in here and we're
going to do for each entry
in the uh what do we call that status
list then here what we want to do uh let
me just get rid of that so in each of
these it's sort of like what we got
here we've got an option we've got an
each we've got an entry and a status
list oops which I don't oh I'm sorry I
already had all that my mistake
uh what we're going to
do is here we're going to just do uh we
want the key is going to be I believe
the ID that we want and then the value
and those are not yeah those are not
functions because what we need is when
we save we're actually saving the key
not the value and this is where I'll
show you this in a second uh this is
where we're displaying statuses which
have like a single letter you like for
new it's got an n capital N but the
label is actually new so if we run this
let's see if this actually gives us what
we
need wh if we go here and we
go log
in and we pick a site and we go here so
see now before you may have seen that
this is a little bit different uh we had
the value and if we look at the actual I
don't know if it's going to be big
enough for you to see but if we look
[Music]
here if we look down here we will see oh
here we go see ID is same so we need to
fix that because we have the oh no here
we go I'm sorry so here if you can see
our option value is p even though we're
displaying pending which is very
important because then when we go back
and save this it's going to grab that
value so if we go in and let's just try
to save this thing let's just pick some
values and make sure we've got it uh
let's change a
couple uh that works let's just do a
save and update see if it works it does
not and that's really our goal is we've
been wanting to save this so let's see
what it complains about this time it may
be one of our other
lists if we go in
here it is complaining about uh cannot
add or update a child row
references user so we probably don't
have the user ID coming in right so what
we want to do is go take a look
at uh here when we do our save or
update uh which is somewhere here here
we go here's our saver
update and our user ID is going to get
the project user ID so we probably are
not actually loading that in uh so let's
go to
our let's see do we have a user ID here
uh we
do uh but that's set there so get on by
so what we probably want to do oh there
should be a user ID in here
somewhere so if we look are we getting
that set
right so we have that and it should be
user ID if we go look in the hidden
right around the form itself
self let's see if that's actually coming
across form hidden user
ID we're not seeing that wait that's
interesting
because uh form hidden path equals so
that's probably what we oh we don't want
that we probably want this to
be instead like we did with these is
it's just going to be a straight
up th input or
input and uh this is going to
be
type equals I think we can do
hidden we're going to find out in a
second if we do that and then this one
is going to
be input hidden up type
equals
and then we need the th
field
here it's going to be ID and user ID so
let's see if we get those in so there's
a couple of those values that we missed
the first time around I think this is
all lowercase user ID th field
[Music]
equals that should hopefully work did I
miss a oh there we go I missed a quote
so let's try that
and let's see what we got here so now if
we run
that and now if we reset when it comes
up uh well let's go ahead and leave that
up because we want to go check to see
our values are coming through so if we
come here and we pick an ID and we pick
a
form uh let's see form action action
where' it go there no that's the div
action action where is it there it is so
get into our container
wrong one get into this
one field
set
form and here we see we've got our csrf
we have there we go so now we see that
we've got our ID Valu is being set in
our user ID values being set so that
should let's go change a couple
values and this is one called create
website let's just we're going to change
this name to this one so we're sure we
can take a look at it here in a
minute and let's see if we do a
save
uh oh interesting so we did something
with home so uh it's probably in
the hello where' it go uh if we do the
save let's
see uh that's start
page this this is the projects filter
here we go
homepage save project here we go so
they're doing redirect to
home let's see what happens because this
one we're going to come through what we
want to do is we end up wanting to
redirect and I think it's because I had
to slash slash that's probably going to
be my
problem um we're going to find out in a
minute this is something else we have
not done uh that we have not gone
through
this uh um a redirect so let's take a
look at that or at least I don't think
we
have let's go take a look at that guy
and now I'm pretty sure what we got is
it will have already saved so if we go
here let's go back one and it'll make us
log in oh no it
won't um so let's go to
Project log
in this one see so it did save our
values oops and we're seeing something
as a problem so now it's got an issue
which
is uh the operator multiply is not so we
got something we missed here
oh so we have got something in our types
that is causing an issue so uh multiply
is not sport to null and a double so
something is a null here so what we need
to do is we need to go
to uh let's see what we probably want to
do is figure
out which it is so if we go look to our
multiply
wh then we're going to see
here uh let's see and it is in which one
it is in oh because we can see which one
that was coming from that was coming
from
here project cost minus project cost so
if we go to wherever the project cost
thing
is let's just go look it
up uh Capital
C there it is so this thing it doesn't
like that we have a null in there
somewhere so it's probably the agent
percent I'm going to assume
uh yes
so what we need to do is make sure that
our agent percent defaults to
zero uh let's see so when we
do let's see how do we want to fix that
we probably want to fix it let's fix it
in the row
mapper so what we can do is we can come
in here where is row
[Music]
mappers so we're going to go into our
Row mapper for our
project and here where it does agent
percent see that's a double and it could
be project cost so project
cost where did we get project C oh maybe
project cost because that's a double Su
Shi be okay so if we go to hello where
is
hello let's go look for
project cost here there it is so it has
a Calculate cost
cost oh so one of them's a
null Let's
do let's
[Music]
see how we want to do this so let's see
did it do it maybe it was in it was
calculating it it does not let's go see
where we like blew it
up oh we probably didn't so let's just
do it like this let's do
okay well let's look at this Calculate
cost so we're going to fix this in a
couple places maybe so Calculate
cost says select
that oh see and this is probably it so
if I
do this and I do
a I bet you if I do it with the project
ID if I go here let me just pick
one uh let's just do
this let's just do a new one and we're
going to do it here and it's going to be
in uh proposals and let's see project ID
equals like 38 I don't care what it is
let's just pick
one there we go
so uh what was the project we can
probably find that
out
28 so it's still a null and probably if
we do uh select star from oh because
there's no
tasks
where project ID equals 28 so we didn't
have any task so it's a null um oh typo
wh got a
typo there we go so what we want to do
here is we're just going to say actually
um let's just do
if null
that then it's going to be
0.0 there we go and so we can take that
and we're going to jump back over here
and we're going to I think that
works project okay so now if we run it
let's see if we got them
working and let's just just rerun
it so we've hit a couple of our snags
here let's see if we got oh we got
another
one which is probably going to be
another one of
our
multiply again and it's going to be
based on is it the same one hopefully
not it is okay so project cost so that
means project so we also want to see
agent percent
which is going to
be hello
controller uh so we need to do agent
percent on the R
mapper so let's go back to our project R
mapper and agent
percent uh here let's do
if
project project dot get
agent percent equals
null then we're just going to set the
percent equal
to
0.0 and now let's see what we got
oh there we go so let's see if it runs
through now hopefully it
does
and we will be able to continue on with
our stuff oh we're still not getting
one and we still have a multiply issue
and we have it in oh we still have it
here which is interesting so project
cost what is the multiply complaining
about not be oh string and double so
somebody's giving me a
double uh so agent
percent and the other one should
be project cost should be a double as
well so if I do get agent percent if I
look here on my
project uh I want to do it on my project
model which is where my models go model
model model model there's my module
models if I go
[Music]
here oh get agent percent is also a
double so we've got something here that
it doesn't
like so we probably have a wrong
mapping and if we do
that okay is that minus that
let's do it this
way project cost minus project
18% should
be that whole
thing so let's try that one more
time
let's see if we got something weird
about
that and we'll probably have to dive
into this next
episode because I don't want this oh
there we
go and it is still probably complaining
multiply not supported between string
and
double am I in the right
place and it is still that one okay who
project cost project cost here
here now we know that so let's do
project
cost let's just do that
first and see if that's the one that
we've
got we just sort of debug this sucker
real quick and in so
doing we come in here okay so it doesn't
like project cost maybe no it's probably
complain about something else now it is
complaining
about
yeah so it's the agent percent most
likely is
it so if we go back to project. agent
percent it is a
double so let's just make
that
0.0 and um this one is going to be
0.0 and let's see if that
works and now if we run it and then
we're going to have to come back and fix
that and we will tackle that next
episode if this works nope where else
did we miss
it multiply again
and oh it doesn't like that see oh um so
it is probably something
here so we're going to go string and
double so we do have something here that
we've got to work out is that it is not
liking how we have
that uh set up essentially
so we will tackle that next step episode
we're going to go make sure we get our
uh our types to comply that being said
if you have any questions shoot us an in
uh email at info develop
or.com and uh thanks for hanging out
with us we'll see you again next time go
out there and have yourself a great day
a great week and we will talk to you
next
[Music]
time
Transcript Segments
1.35

[Music]

27.199

well hello and welcome back we are

29.4

continuing going through our little app

31.759

conversion here using spring Boot and

34.16

time leaf and this time around we are

37.2

going to look at a specific little thing

39.399

we ran in before we were building

41.28

options out using this

44.239

list actually it's a it's a map uh if

47.32

you go back to the status list you will

50.44

see somewhere in here where I've got the

53.8

hello controller we can see that we've

56.199

got this status list and actually we can

57.8

see right here it's a it's a map of a

60

string and a string and so what we

61.84

actually want to do when we get into

64.36

displaying our dropdowns instead of just

66.56

having the same thing as a value as we

68.479

have for the text we actually want it to

70.759

be the key and the value now there's a

73.32

couple ways we can do

74.84

this and in this case uh what I'm going

78.159

to do

80.32

is I'm going to do um and I'm just

84.159

pasting this in because for each of

86.28

these options instead what we're going

87.56

to do is we can actually Within just as

90.2

we did in some others we do some fours

92.84

we're going to come in here and we're

94.079

going to do for each entry

96.36

in the uh what do we call that status

102.04

list then here what we want to do uh let

105.439

me just get rid of that so in each of

107.439

these it's sort of like what we got

110.32

here we've got an option we've got an

113.04

each we've got an entry and a status

115.52

list oops which I don't oh I'm sorry I

118.2

already had all that my mistake

120.479

uh what we're going to

121.68

do is here we're going to just do uh we

124.84

want the key is going to be I believe

129.64

the ID that we want and then the value

132.48

and those are not yeah those are not

135.879

functions because what we need is when

139.959

we save we're actually saving the key

141.599

not the value and this is where I'll

143.04

show you this in a second uh this is

145.76

where we're displaying statuses which

148.28

have like a single letter you like for

150.8

new it's got an n capital N but the

153.4

label is actually new so if we run this

155.959

let's see if this actually gives us what

158.519

we

162.239

need wh if we go here and we

168.239

go log

175.2

in and we pick a site and we go here so

179.2

see now before you may have seen that

181.159

this is a little bit different uh we had

183.239

the value and if we look at the actual I

185.599

don't know if it's going to be big

186.4

enough for you to see but if we look

188.13

[Music]

189.959

here if we look down here we will see oh

193.56

here we go see ID is same so we need to

196.319

fix that because we have the oh no here

198.2

we go I'm sorry so here if you can see

201.08

our option value is p even though we're

202.959

displaying pending which is very

205.159

important because then when we go back

206.599

and save this it's going to grab that

209

value so if we go in and let's just try

211.319

to save this thing let's just pick some

213.159

values and make sure we've got it uh

216.439

let's change a

218.56

couple uh that works let's just do a

221.599

save and update see if it works it does

223.48

not and that's really our goal is we've

225.599

been wanting to save this so let's see

227

what it complains about this time it may

228.799

be one of our other

230.4

lists if we go in

233.079

here it is complaining about uh cannot

236.879

add or update a child row

242.4

references user so we probably don't

243.92

have the user ID coming in right so what

247

we want to do is go take a look

249.64

at uh here when we do our save or

254.36

update uh which is somewhere here here

257.799

we go here's our saver

259.88

update and our user ID is going to get

262.479

the project user ID so we probably are

264.6

not actually loading that in uh so let's

267.56

go to

268.6

our let's see do we have a user ID here

273.4

uh we

274.6

do uh but that's set there so get on by

278.36

so what we probably want to do oh there

281.44

should be a user ID in here

284.28

somewhere so if we look are we getting

286.56

that set

290.72

right so we have that and it should be

294.6

user ID if we go look in the hidden

296.96

right around the form itself

300.08

self let's see if that's actually coming

302.96

across form hidden user

307.72

ID we're not seeing that wait that's

311.44

interesting

314.039

because uh form hidden path equals so

317.8

that's probably what we oh we don't want

321.56

that we probably want this to

324.4

be instead like we did with these is

328.759

it's just going to be a straight

331.56

up th input or

335.919

input and uh this is going to

340.88

be

343.28

type equals I think we can do

346.639

hidden we're going to find out in a

349.28

second if we do that and then this one

352.72

is going to

354.759

be input hidden up type

358.479

equals

363.36

and then we need the th

366.16

field

368.52

here it's going to be ID and user ID so

371.84

let's see if we get those in so there's

374.44

a couple of those values that we missed

375.84

the first time around I think this is

377.639

all lowercase user ID th field

381.61

[Music]

383.16

equals that should hopefully work did I

385.8

miss a oh there we go I missed a quote

388.52

so let's try that

391.199

and let's see what we got here so now if

393.52

we run

394.759

that and now if we reset when it comes

398.44

up uh well let's go ahead and leave that

400.56

up because we want to go check to see

401.759

our values are coming through so if we

403.84

come here and we pick an ID and we pick

405.88

a

408.199

form uh let's see form action action

411.479

where' it go there no that's the div

414.8

action action where is it there it is so

418.28

get into our container

420.4

wrong one get into this

422.639

one field

425.039

set

426.68

form and here we see we've got our csrf

430.52

we have there we go so now we see that

432.879

we've got our ID Valu is being set in

435.039

our user ID values being set so that

437.639

should let's go change a couple

440.68

values and this is one called create

443.199

website let's just we're going to change

444.84

this name to this one so we're sure we

448.52

can take a look at it here in a

451.319

minute and let's see if we do a

454.199

save

456.68

uh oh interesting so we did something

459.68

with home so uh it's probably in

464.72

the hello where' it go uh if we do the

470.8

save let's

473.72

see uh that's start

478.159

page this this is the projects filter

482.24

here we go

484.039

homepage save project here we go so

486.479

they're doing redirect to

490.879

home let's see what happens because this

493.24

one we're going to come through what we

494.319

want to do is we end up wanting to

495.8

redirect and I think it's because I had

497.159

to slash slash that's probably going to

498.8

be my

499.96

problem um we're going to find out in a

502.28

minute this is something else we have

503.759

not done uh that we have not gone

506.759

through

508.08

this uh um a redirect so let's take a

510.96

look at that or at least I don't think

512.24

we

513.279

have let's go take a look at that guy

516.719

and now I'm pretty sure what we got is

519.44

it will have already saved so if we go

522.32

here let's go back one and it'll make us

524.68

log in oh no it

526.399

won't um so let's go to

529.04

Project log

532.16

in this one see so it did save our

534.839

values oops and we're seeing something

536.959

as a problem so now it's got an issue

540.64

which

543.76

is uh the operator multiply is not so we

547.64

got something we missed here

556.839

oh so we have got something in our types

560.32

that is causing an issue so uh multiply

563.16

is not sport to null and a double so

565.68

something is a null here so what we need

568.04

to do is we need to go

570.72

to uh let's see what we probably want to

574.079

do is figure

576.92

out which it is so if we go look to our

580.079

multiply

584.079

wh then we're going to see

589.12

here uh let's see and it is in which one

592.519

it is in oh because we can see which one

596.04

that was coming from that was coming

598.12

from

600.68

here project cost minus project cost so

603.399

if we go to wherever the project cost

605.36

thing

607.56

is let's just go look it

612.36

up uh Capital

619.36

C there it is so this thing it doesn't

622.56

like that we have a null in there

624.399

somewhere so it's probably the agent

626.76

percent I'm going to assume

630.68

uh yes

633.56

so what we need to do is make sure that

635.88

our agent percent defaults to

639.12

zero uh let's see so when we

641.92

do let's see how do we want to fix that

644.44

we probably want to fix it let's fix it

647.68

in the row

648.8

mapper so what we can do is we can come

651.079

in here where is row

653.21

[Music]

655

mappers so we're going to go into our

656.92

Row mapper for our

661.16

project and here where it does agent

664.519

percent see that's a double and it could

668.12

be project cost so project

672.399

cost where did we get project C oh maybe

675.16

project cost because that's a double Su

677

Shi be okay so if we go to hello where

679.56

is

680.88

hello let's go look for

684.399

project cost here there it is so it has

687.88

a Calculate cost

691.24

cost oh so one of them's a

695.12

null Let's

700.959

do let's

702.56

[Music]

704.8

see how we want to do this so let's see

707.56

did it do it maybe it was in it was

709.2

calculating it it does not let's go see

712.8

where we like blew it

715

up oh we probably didn't so let's just

717.399

do it like this let's do

721.839

okay well let's look at this Calculate

723.16

cost so we're going to fix this in a

724.44

couple places maybe so Calculate

727.079

cost says select

731

that oh see and this is probably it so

733.639

if I

734.92

do this and I do

738.279

a I bet you if I do it with the project

743.199

ID if I go here let me just pick

747.36

one uh let's just do

750.519

this let's just do a new one and we're

753.04

going to do it here and it's going to be

756.48

in uh proposals and let's see project ID

759.76

equals like 38 I don't care what it is

761.839

let's just pick

763.079

one there we go

768.24

so uh what was the project we can

771.48

probably find that

773.32

out

777.36

28 so it's still a null and probably if

780.839

we do uh select star from oh because

784.56

there's no

786.44

tasks

788.56

where project ID equals 28 so we didn't

791.839

have any task so it's a null um oh typo

796.8

wh got a

800.04

typo there we go so what we want to do

802.56

here is we're just going to say actually

807.079

um let's just do

809.36

if null

811.839

that then it's going to be

816.8

0.0 there we go and so we can take that

821.959

and we're going to jump back over here

824.44

and we're going to I think that

827.76

works project okay so now if we run it

831.959

let's see if we got them

837.32

working and let's just just rerun

843.56

it so we've hit a couple of our snags

846.56

here let's see if we got oh we got

848

another

849.279

one which is probably going to be

851.16

another one of

852.72

our

855.959

multiply again and it's going to be

858.6

based on is it the same one hopefully

862.16

not it is okay so project cost so that

865.199

means project so we also want to see

867.959

agent percent

871.24

which is going to

873.36

be hello

876.279

controller uh so we need to do agent

879.44

percent on the R

881.48

mapper so let's go back to our project R

884.839

mapper and agent

888.079

percent uh here let's do

891.88

if

896.519

project project dot get

903.519

agent percent equals

908.399

null then we're just going to set the

911.24

percent equal

915.16

to

918.24

0.0 and now let's see what we got

922.6

oh there we go so let's see if it runs

925.8

through now hopefully it

927.6

does

932.24

and we will be able to continue on with

935.56

our stuff oh we're still not getting

942.44

one and we still have a multiply issue

945.16

and we have it in oh we still have it

948.44

here which is interesting so project

954.6

cost what is the multiply complaining

957.6

about not be oh string and double so

961.319

somebody's giving me a

964.04

double uh so agent

968.36

percent and the other one should

972.72

be project cost should be a double as

978.12

well so if I do get agent percent if I

981.12

look here on my

986.12

project uh I want to do it on my project

989.16

model which is where my models go model

992

model model model there's my module

994.6

models if I go

996.44

[Music]

997.72

here oh get agent percent is also a

1000.8

double so we've got something here that

1003.48

it doesn't

1005.12

like so we probably have a wrong

1009.319

mapping and if we do

1014.079

that okay is that minus that

1019.079

let's do it this

1027.4

way project cost minus project

1032.319

18% should

1036.12

be that whole

1040

thing so let's try that one more

1047.4

time

1049.16

let's see if we got something weird

1050.799

about

1052.28

that and we'll probably have to dive

1054.36

into this next

1056.28

episode because I don't want this oh

1058.32

there we

1060.44

go and it is still probably complaining

1063.64

multiply not supported between string

1065.919

and

1066.919

double am I in the right

1069.28

place and it is still that one okay who

1075.6

project cost project cost here

1079.36

here now we know that so let's do

1082.12

project

1084.159

cost let's just do that

1087.12

first and see if that's the one that

1089.36

we've

1091.96

got we just sort of debug this sucker

1094.48

real quick and in so

1097

doing we come in here okay so it doesn't

1099.72

like project cost maybe no it's probably

1101.64

complain about something else now it is

1104.32

complaining

1107.32

about

1111.44

yeah so it's the agent percent most

1113.72

likely is

1119

it so if we go back to project. agent

1123.4

percent it is a

1130.559

double so let's just make

1133.559

that

1136.679

0.0 and um this one is going to be

1145.6

0.0 and let's see if that

1153.12

works and now if we run it and then

1156.4

we're going to have to come back and fix

1157.64

that and we will tackle that next

1159.52

episode if this works nope where else

1162.88

did we miss

1166.159

it multiply again

1172.48

and oh it doesn't like that see oh um so

1177.12

it is probably something

1180.6

here so we're going to go string and

1184.96

double so we do have something here that

1186.919

we've got to work out is that it is not

1188.72

liking how we have

1191.799

that uh set up essentially

1195.96

so we will tackle that next step episode

1198.559

we're going to go make sure we get our

1200.6

uh our types to comply that being said

1203.559

if you have any questions shoot us an in

1205.159

uh email at info develop

1207.799

or.com and uh thanks for hanging out

1210.08

with us we'll see you again next time go

1211.76

out there and have yourself a great day

1213.12

a great week and we will talk to you

1215.799

next

1218.32

[Music]

1227.159

time