📺 Develpreneur YouTube Episode

Video + transcript

Spring Boot - Ajax Tutorial

2024-02-22 •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. This short shows an Ajax call to avoid extra trips visible to the user.

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
well hello and welcome back we are
continuing our little series of
converting our application over from a
Apache Tomcat JSP to a new fangled
spring boot app we are in the middle of
this part two as we're playing around
with some uh some JavaScript and stuff
like that what we wanted to do is we
want to add some buttons so we can more
easily navigate in particular we want to
be able to say this is lost and Market
lost so it comes off of our list so it
just saves us from having to jump over
here and then set the status which is
just sometimes a bit of a pain so oh one
of the
things we wanted to fix was our
alignment and if you notice now that
we've got our alignments back together
but let's go take a take a look at that
loss so what we wanted to do is we're
going to do an Ajax call but first we're
going to create uh we'll come back to
the Ed let's create this uh we're going
to do lost project with an ID so we can
probably steal similar to edit project
uh let's see so if we go to is it in our
now we got to figure out where we put it
uh oops I don't want to put it
there come
on there we go okay we have a project
controller so here we probably
have actually probably have something
very similar to this so we have Let's
see we have a project report save
project uh little project Report Form
let's see what save project that may
give us what we
want so we're going to do uh let's see
that's let's go ahead and keep our
comments so this is then going to be
called uh this is going to be hand a
request
to set the project to
lost and then
um just going to say none
needed and this is going to be lost
project and it is going to
have uh like
here we're going to use like we did with
the uh I guess like the project report
that would work as well did we use that
for edit yep edit project is the same so
if we look at that actually let's do it
this
way we're going to do it more like
that uh so we got lost project it is
going to be oh is not a PO
um no we're going to make it a get we're
going to make it really
simple and so we've got our path
variable which is is going to be our
ID and then we're going to come in here
so what we want to do uh is we're going
to
say uh let's see probably need some of
this
stuff so if we come in
here uh let save project lost project so
what we want to
do
is I wonder if I can make this just a
straight up
void and then it's just going to
return we're going to find out in a
minute uh there's a request there's our
variable so we're going to come in we
have a project that's a null we don't
really need the users so we don't need
any of that uh yes we do because we do
want to make sure that um we're logged
in what we call this because this is
going to need to we're actually have to
check this so we're going to say uh
let's see if project ID greater than
zero yep then we want to load the
project and we also want to say
here uh
if uh project
dot let's see what did we call that it's
going to be the user so if we go to the
project
model you oh not user
project so if we go to the project model
is there a user ID there is a user ID so
we should be able to go get uh get
user oh it's case sensitive fine there
we go so we want to get user
ID and so we're
gonna accidentally dragged and dropped
so let's go back over to our project
controller and here we're going to say
if get user ID is equal
to uh let's just do
user
dot I believe we called it user ID uh
let's
see get user ID we're going to find out
let's see so we go look at
user was it also get user ID or is it
get ID uh it is probably get ID yep so
it's get
ID it's good cuz I just typoed that
anyways so now it's like hey this has to
you have to own it and in that case then
what I can do is I can do project dot
it's probably set
status uh let's
see well we can go look at our project
real
quick project uh let's see do we have
our statuses up here somewhere yes we do
so that's a stus status string uh so
status is going to be equal to Lost
so or capital L so we're going to say
hey we're going to want
to set status to Lost whoops
capital and then need to save which we
have like here's a good one right
here we'll just take that same thing
steal our code and then we get to return
so let's see if that works um on the
build so let's do that real quick but
then we're going to have to make that
call all right so it builds all right
and then we're going to go to our
projects and now what we need to do is
we're need to Lost project with the ID
which we've basically got that but what
we're going to have to do is make that
call a little bit different and how we
do a
call is we're going to come in and we're
going to make a variable and we're going
to call him uh X
HTTP and he is going to be assigned to a
new uh we're going to call it an X XL
HTTP
request and he doesn't need
anything and then we're going to do X
HTTP um actually we can probably just so
we're probably just going to be able to
do this with a get away with a an open
because we don't really care what comes
back so I think we will be able to get
away with this usually you're going to
create a function that you're going to
call that's basically going to attach to
that so it's like an event liser uh in
this case I think what we can do is we
can do a
get I actually could have made it a post
but oh well and then we're going to give
it the URL which is
this and then we're going to say true
because we can just let this sucker run
which is what that last one
does and then we're going to do a
send
so it's actually super easy so we're
just coming in we set it we create our
object we tell it where to go we're
going to send a get request and we're
going to say hey just fire and forget
we're going to send it and then we're
going to hide our control so this should
be assum this works this is to show you
we can very quickly then get some
updates going and not have to do all of
the you know navigation kind of stuff
and this something that comes very
useful cuz we are end up using this and
we're going to see this in a couple
places uh particularly like I said we
start copying stuff so now if I do a
lost so that was app testing upwork
let's see if I do lost H it did not do
anything did I get an
error uh it did not so let's come in
here and is it called Lost project let's
make sure I got that
right lost project lowercase
L uh let's make sure that's right right
nope that's where it's a
problem at least I think that's why um
let's go do this let's just change this
because I want to be
mean uh no I'm let's not get crazy here
okay so now if I reset
this
now let's see if this
runs
correctly and if it
does and if I reset oh it is there did
it tell me anything it did not so let's
go in here and let's just do this real
quick let's do uh
system.out let's use our favorite we're
just going to do a print line and
instead of a debugger or anything like
that we're just going to do this one
real quick and we're going to say hey
what is the
ID
and
uh let's see let's do that here as
well we're going to do our two
IDs and let's make sure that
those are working
right okay that's a good start so let's
jump onto that let's see if it's going
to pick up our
call and maybe it's
not so I may have to go build our little
catch
function and if I do that it did not
call so let's try that uh let's go back
to where is that our projects and so
instead now what we're going to have to
do here is we're going to have to come
in and we're going to have to do the uh
check it so we're going to do X
HTTP which is what we called it and then
we're going to be looking on the
on ready oh there you go on ready State
change got that working that's going to
be equal to a
function and the
function is just going to
be let's just
say
uh let's do uh let's let's do this if oh
sorry sorry wrong bracket or I want
parenthesis not that so if I'm going to
do if this
dot ready
State equals let's say four and and this
is basically say is this correct status
equals
200 oh that's probably what its problem
is uh part of
it let's go back to think I do this as
a I go back
here and
do a string let's just do a
string yeah uh where'd you go here wh oh
need to save that but first I need to go
back to my project
controller and wherever I had that oh
it's up
here L project is going to be a
string and we're just going to
say
success and that's going to get us
everything we need so and we can
probably tweak this a little bit so
we're going to check this in a minute uh
let's see so if we do that that's the
end of the
function so we need to open up the if
let's do it this way let's pretty this
up a little bit so we can read it so the
if statement is just going to be
alert call
successful not sure if that'll actually
fire off a lot of times it does not but
let's do that
anyway let's go ahead and fire this
up and if we do
that come to
projects lost didn't give us the alert
did it give us a an error it did aha
um
ah we got a
404 so lost project it does not
like so let's see what did we do wrong
let's
see so let's go back and look at
that if we go look at this
guy oh he's gonna need that anyways
because the model so he's gonna just do
model review let's do
this let's do
that and
here worst case is he's going to
do R direct to projects so we're just
going to do it that way uh let's see the
last project there we go so if we call
this outside of it it won't really
matter because we're going to come back
here if we call outside of it we're
going to back to projects otherwise
um then it's just going to get lost like
we saw up over here uh where it was like
hey I've got this page but it's a an
error page so let's rebuild
it
and come over here wh if it'll log in or
if it'll start the
server
projects lost
lost and now it still did not give us
that interestingly
enough if we go look at the control
panel uh console did not give us errors
this time interesting so something's not
right so if I do projects if I do lost
project and give it a number like
uh that one so let's just go look uh
27 okay so we got an error we got a type
not found status
404 what did I miss
here aha I was in the wrong file so
let's take the same
thing let's put it into the correct
file H that was a pain
and oh did I reset it no not
yet restart
it oh there we go um so I have an error
somewhere here we go project ID oh it's
not I project Ty see now there we go now
it's starting
to give me something
useful uh okay so that should give me
that and then
let's go run
it and when you run the code in the
right project let's see if that actually
does what we wanted to
do there there we go no wonder it was
given all kinds of issues that makes
sense and
so get rid my little print statements
here and so we can either call it
somewhere else or we can call call it on
the fly like we saw here is now it's
just there if we reset hey we lost those
so we can quickly say oh let's get clean
up some of those projects and boom
they're all lost but now if I switch
over to
Lost I'll see all of those guys probably
don't want that to be there when you're
displaying loss there's actually
probably and which I think maybe that'll
be the next thing maybe we'll play
around with this a little bit and make
that conditional uh but we also want to
I think actually probably the next thing
we do we're come back next time around
and we're going to be able to put a copy
so now that we've gotten that we're
going to come in here we're going to put
a little copy button or basically say
like uh create project something like
that and we're going to walk through uh
basically copying data from one item to
another saving that and building that
out so that will help us out as well but
that would wrap it up for this time go
out there and have yourself a great day
a great week and we will talk to you
next
time
three
two one hello this is Rob with develop
and or also known as building better
developers wanted to announce that we
have school. developin or.com feel free
to check it out if you like any of this
information any of the content that
we've sent and you would like to see
more you can come out you can enroll for
free we have free courses we've got
places for you to get better at just
learning a technology or how to
you can work on your business skills we
can help you with becoming a better
developer as in coding and things like
that a lot of the stuff you've seen on
YouTube we also have out at school.
develop andure we just have it in a
little more of a educational format and
a way for you to track your progress as
you move forward becoming a better
developer
Transcript Segments
16.96

well hello and welcome back we are

19.199

continuing our little series of

20.92

converting our application over from a

24.279

Apache Tomcat JSP to a new fangled

27.84

spring boot app we are in the middle of

31.08

this part two as we're playing around

33.04

with some uh some JavaScript and stuff

34.84

like that what we wanted to do is we

36.36

want to add some buttons so we can more

38.6

easily navigate in particular we want to

40.8

be able to say this is lost and Market

43.2

lost so it comes off of our list so it

44.92

just saves us from having to jump over

46.76

here and then set the status which is

49.28

just sometimes a bit of a pain so oh one

52.879

of the

53.84

things we wanted to fix was our

56.719

alignment and if you notice now that

58.519

we've got our alignments back together

60.76

but let's go take a take a look at that

62.399

loss so what we wanted to do is we're

64.68

going to do an Ajax call but first we're

67.2

going to create uh we'll come back to

69.4

the Ed let's create this uh we're going

70.84

to do lost project with an ID so we can

73.84

probably steal similar to edit project

76.96

uh let's see so if we go to is it in our

81.36

now we got to figure out where we put it

83.079

uh oops I don't want to put it

85.72

there come

87.799

on there we go okay we have a project

90.64

controller so here we probably

92.88

have actually probably have something

94.68

very similar to this so we have Let's

96.28

see we have a project report save

100.52

project uh little project Report Form

103.439

let's see what save project that may

105.2

give us what we

108.96

want so we're going to do uh let's see

112.759

that's let's go ahead and keep our

114.52

comments so this is then going to be

118.64

called uh this is going to be hand a

121.399

request

123.64

to set the project to

128.759

lost and then

131.72

um just going to say none

135.28

needed and this is going to be lost

140

project and it is going to

143.16

have uh like

146

here we're going to use like we did with

148.56

the uh I guess like the project report

150.519

that would work as well did we use that

152.48

for edit yep edit project is the same so

156

if we look at that actually let's do it

157.4

this

158.76

way we're going to do it more like

162.68

that uh so we got lost project it is

165.519

going to be oh is not a PO

170.159

um no we're going to make it a get we're

173.159

going to make it really

176.12

simple and so we've got our path

178.68

variable which is is going to be our

181.04

ID and then we're going to come in here

183.08

so what we want to do uh is we're going

185.799

to

188.599

say uh let's see probably need some of

192

this

196.04

stuff so if we come in

198.72

here uh let save project lost project so

202.28

what we want to

205.68

do

208.68

is I wonder if I can make this just a

210.959

straight up

213.36

void and then it's just going to

217.879

return we're going to find out in a

220.319

minute uh there's a request there's our

222.36

variable so we're going to come in we

223.92

have a project that's a null we don't

225.36

really need the users so we don't need

227

any of that uh yes we do because we do

229.76

want to make sure that um we're logged

232.76

in what we call this because this is

234.68

going to need to we're actually have to

236.239

check this so we're going to say uh

238.2

let's see if project ID greater than

239.799

zero yep then we want to load the

241.48

project and we also want to say

244.84

here uh

247.079

if uh project

250.079

dot let's see what did we call that it's

252.64

going to be the user so if we go to the

255.84

project

258.28

model you oh not user

261.4

project so if we go to the project model

264.96

is there a user ID there is a user ID so

269.759

we should be able to go get uh get

273.919

user oh it's case sensitive fine there

277.36

we go so we want to get user

280.52

ID and so we're

285.919

gonna accidentally dragged and dropped

288.039

so let's go back over to our project

290.72

controller and here we're going to say

293.08

if get user ID is equal

297.479

to uh let's just do

300.479

user

302.6

dot I believe we called it user ID uh

306.479

let's

308

see get user ID we're going to find out

311.44

let's see so we go look at

313.56

user was it also get user ID or is it

316.72

get ID uh it is probably get ID yep so

321

it's get

322.08

ID it's good cuz I just typoed that

326

anyways so now it's like hey this has to

328.36

you have to own it and in that case then

330.919

what I can do is I can do project dot

334.199

it's probably set

340.96

status uh let's

343.6

see well we can go look at our project

345.88

real

347

quick project uh let's see do we have

349.479

our statuses up here somewhere yes we do

352.479

so that's a stus status string uh so

356.36

status is going to be equal to Lost

361.919

so or capital L so we're going to say

364.08

hey we're going to want

366.199

to set status to Lost whoops

371.599

capital and then need to save which we

374.84

have like here's a good one right

377.08

here we'll just take that same thing

379.599

steal our code and then we get to return

382.52

so let's see if that works um on the

385.479

build so let's do that real quick but

387.44

then we're going to have to make that

388.44

call all right so it builds all right

390.88

and then we're going to go to our

392.479

projects and now what we need to do is

394.72

we're need to Lost project with the ID

396.52

which we've basically got that but what

398.52

we're going to have to do is make that

399.599

call a little bit different and how we

401.479

do a

402.8

call is we're going to come in and we're

406.24

going to make a variable and we're going

408.24

to call him uh X

411.16

HTTP and he is going to be assigned to a

416.199

new uh we're going to call it an X XL

420.16

HTTP

422.759

request and he doesn't need

425.599

anything and then we're going to do X

431.96

HTTP um actually we can probably just so

436.12

we're probably just going to be able to

437.24

do this with a get away with a an open

439.8

because we don't really care what comes

442.44

back so I think we will be able to get

444.68

away with this usually you're going to

445.84

create a function that you're going to

447.72

call that's basically going to attach to

449.08

that so it's like an event liser uh in

451.319

this case I think what we can do is we

454.039

can do a

456.16

get I actually could have made it a post

458.479

but oh well and then we're going to give

459.84

it the URL which is

468.08

this and then we're going to say true

470.52

because we can just let this sucker run

472.44

which is what that last one

475.72

does and then we're going to do a

478.319

send

480.319

so it's actually super easy so we're

483.56

just coming in we set it we create our

485.159

object we tell it where to go we're

487.08

going to send a get request and we're

489.08

going to say hey just fire and forget

491.24

we're going to send it and then we're

492.28

going to hide our control so this should

494.199

be assum this works this is to show you

497.52

we can very quickly then get some

500.36

updates going and not have to do all of

503.8

the you know navigation kind of stuff

506.199

and this something that comes very

507.159

useful cuz we are end up using this and

509.479

we're going to see this in a couple

510.68

places uh particularly like I said we

512.839

start copying stuff so now if I do a

515.88

lost so that was app testing upwork

518.719

let's see if I do lost H it did not do

520.719

anything did I get an

522.599

error uh it did not so let's come in

526.36

here and is it called Lost project let's

529.399

make sure I got that

532.64

right lost project lowercase

537.12

L uh let's make sure that's right right

539.839

nope that's where it's a

544.16

problem at least I think that's why um

547.64

let's go do this let's just change this

549.399

because I want to be

551.6

mean uh no I'm let's not get crazy here

556.04

okay so now if I reset

561.76

this

563.32

now let's see if this

566.36

runs

568.12

correctly and if it

571.959

does and if I reset oh it is there did

575.6

it tell me anything it did not so let's

578.519

go in here and let's just do this real

580.279

quick let's do uh

586

system.out let's use our favorite we're

588.279

just going to do a print line and

590.079

instead of a debugger or anything like

591.44

that we're just going to do this one

592.48

real quick and we're going to say hey

593.64

what is the

596.44

ID

598.12

and

600.6

uh let's see let's do that here as

609.2

well we're going to do our two

613

IDs and let's make sure that

617

those are working

623.079

right okay that's a good start so let's

625.44

jump onto that let's see if it's going

627.68

to pick up our

629.64

call and maybe it's

632.079

not so I may have to go build our little

634.92

catch

639.6

function and if I do that it did not

642.68

call so let's try that uh let's go back

646.92

to where is that our projects and so

651.16

instead now what we're going to have to

652.24

do here is we're going to have to come

653.8

in and we're going to have to do the uh

655.56

check it so we're going to do X

657.56

HTTP which is what we called it and then

660.92

we're going to be looking on the

663.36

on ready oh there you go on ready State

666.079

change got that working that's going to

668.12

be equal to a

670.04

function and the

672.6

function is just going to

676

be let's just

680.2

say

682.6

uh let's do uh let's let's do this if oh

688.72

sorry sorry wrong bracket or I want

690.519

parenthesis not that so if I'm going to

692.32

do if this

694.399

dot ready

697

State equals let's say four and and this

700.839

is basically say is this correct status

705.72

equals

708.6

200 oh that's probably what its problem

711

is uh part of

714.88

it let's go back to think I do this as

723.279

a I go back

725.48

here and

729.12

do a string let's just do a

732.399

string yeah uh where'd you go here wh oh

738.519

need to save that but first I need to go

740.04

back to my project

741.44

controller and wherever I had that oh

744.32

it's up

746.68

here L project is going to be a

750.639

string and we're just going to

754.04

say

758.279

success and that's going to get us

760.36

everything we need so and we can

762.199

probably tweak this a little bit so

763.48

we're going to check this in a minute uh

765.6

let's see so if we do that that's the

768.04

end of the

769.279

function so we need to open up the if

771.88

let's do it this way let's pretty this

773.44

up a little bit so we can read it so the

776.6

if statement is just going to be

782.56

alert call

784.639

successful not sure if that'll actually

786.56

fire off a lot of times it does not but

788.36

let's do that

791.079

anyway let's go ahead and fire this

795.36

up and if we do

798.079

that come to

800.279

projects lost didn't give us the alert

804.399

did it give us a an error it did aha

813.959

um

816.16

ah we got a

818.079

404 so lost project it does not

824.68

like so let's see what did we do wrong

828.44

let's

830.519

see so let's go back and look at

834.399

that if we go look at this

837.759

guy oh he's gonna need that anyways

840.839

because the model so he's gonna just do

842.6

model review let's do

846.16

this let's do

848.399

that and

851.279

here worst case is he's going to

854.68

do R direct to projects so we're just

859.399

going to do it that way uh let's see the

861.32

last project there we go so if we call

863.32

this outside of it it won't really

865.199

matter because we're going to come back

866.199

here if we call outside of it we're

867.16

going to back to projects otherwise

870.04

um then it's just going to get lost like

872.36

we saw up over here uh where it was like

876.839

hey I've got this page but it's a an

878.759

error page so let's rebuild

885.72

it

889.16

and come over here wh if it'll log in or

893.32

if it'll start the

895.44

server

896.959

projects lost

904.399

lost and now it still did not give us

907.24

that interestingly

909.44

enough if we go look at the control

914.399

panel uh console did not give us errors

916.839

this time interesting so something's not

919.44

right so if I do projects if I do lost

924.72

project and give it a number like

930.56

uh that one so let's just go look uh

937.68

27 okay so we got an error we got a type

940.639

not found status

943.04

404 what did I miss

946.68

here aha I was in the wrong file so

949.839

let's take the same

953.12

thing let's put it into the correct

956

file H that was a pain

961.24

and oh did I reset it no not

965.36

yet restart

968

it oh there we go um so I have an error

972.92

somewhere here we go project ID oh it's

976.24

not I project Ty see now there we go now

980.399

it's starting

982.399

to give me something

984.519

useful uh okay so that should give me

986.88

that and then

991.399

let's go run

993.319

it and when you run the code in the

995.6

right project let's see if that actually

998.6

does what we wanted to

1003.12

do there there we go no wonder it was

1006.639

given all kinds of issues that makes

1008.92

sense and

1011.04

so get rid my little print statements

1013.759

here and so we can either call it

1017.48

somewhere else or we can call call it on

1018.759

the fly like we saw here is now it's

1020.48

just there if we reset hey we lost those

1022.839

so we can quickly say oh let's get clean

1024.839

up some of those projects and boom

1027

they're all lost but now if I switch

1028.679

over to

1030.16

Lost I'll see all of those guys probably

1033.12

don't want that to be there when you're

1034.48

displaying loss there's actually

1036.039

probably and which I think maybe that'll

1037.839

be the next thing maybe we'll play

1038.799

around with this a little bit and make

1039.799

that conditional uh but we also want to

1044.039

I think actually probably the next thing

1044.959

we do we're come back next time around

1046.679

and we're going to be able to put a copy

1047.919

so now that we've gotten that we're

1049

going to come in here we're going to put

1049.84

a little copy button or basically say

1051.919

like uh create project something like

1054.28

that and we're going to walk through uh

1056.799

basically copying data from one item to

1058.72

another saving that and building that

1060.52

out so that will help us out as well but

1063.32

that would wrap it up for this time go

1065.36

out there and have yourself a great day

1067.52

a great week and we will talk to you

1070.28

next

1077.4

time

1080.679

three

1082.96

two one hello this is Rob with develop

1086.96

and or also known as building better

1089

developers wanted to announce that we

1091.039

have school. developin or.com feel free

1093.919

to check it out if you like any of this

1096.36

information any of the content that

1098.24

we've sent and you would like to see

1099.559

more you can come out you can enroll for

1101.559

free we have free courses we've got

1103.96

places for you to get better at just

1106.52

learning a technology or how to

1109

you can work on your business skills we

1110.96

can help you with becoming a better

1112.799

developer as in coding and things like

1115.159

that a lot of the stuff you've seen on

1116.76

YouTube we also have out at school.

1119.48

develop andure we just have it in a

1121.08

little more of a educational format and

1123.64

a way for you to track your progress as

1126.12

you move forward becoming a better

1137.32

developer