#spring boot and html

65 messages · Page 1 of 1 (latest)

stoic bear
#

How can I send an item from spring boot code to an html script using getmapping? i have some methods here, but on the html, all the items are hardcoded because im not sure how to get these methods on html

@GetMapping(path = "/items")
    public @ResponseBody List<Items> listItems() 
    {
        return itemsRepository.findAll();
    }

    @GetMapping(path = "/items/{id}")
    public @ResponseBody Optional<Items> findItems(@PathVariable("id") Integer id) 
    {
        return itemsRepository.findById(id);
    }

    
    @PostMapping(path = "/items")
    public @ResponseBody void addItem(@RequestBody Items item) 
    {
        itemsRepository.save(item);
    }

    @DeleteMapping(path = "/items/{id}")
    public @ResponseBody void deleteItem(@RequestParam("id") int id) 
    {
        itemsRepository.deleteById(id);
    }
    ```
hot coralBOT
#

Hey, @stoic bear!
Please remember to /close this post once your question has been answered!

shy parrot
#

you mean calling these api on the front end?
u can use "url/items/{id}"

stoic bear
#

ah okay thanks

shy parrot
#

glad to help

stoic bear
shy parrot
#

url is your Back-end links

stoic bear
shy parrot
#

your host name + @mapping stuff

#

get use request params

#

post use request body

stoic bear
#

well is this gonna display all the items on the home page? or will this bring me to a new page with all the items?

stoic bear
shy parrot
#

If you using thymleaf ,it will display all the items on the home page

stoic bear
#

so url is just the host name?

#

but the thing is, how do you write that on the html file? like whats the method to get functions from java?

shy parrot
#

u can create a axios request or ajax request in javascript

#

such like this

stoic bear
#

and this will process the back end functions into the front end?

shy parrot
#

But if you are trying to call methods in static html like php, then maybe only jsp can do it

stoic bear
#

because currently we have items hardcoded onto the website, and we have the backend class for items, we just dont know how to take the methods from that class to display the items on the home page of the html

shy parrot
#

let me create a project to explain it in detail, just a moment

#

for example, i have a userInfo method in localhost:8081

#

when i login in website,front will send a request localhost:8081/usrInfo to call this function

#

the method will return json data

#

u can use the return value to modify your front-end

shy parrot
# shy parrot

Never mind that my link here is localhost:8080
Because I am separating the front and back end here and using a gateway, if you are a standalone project just use your own hostname+method

stoic bear
#

i just dont understand how to connect it with the html file

shy parrot
#

you mean if you have a button on top of html,and you click it , it will call the back-end method and render the database data to the html

#

is that what you mean?

stoic bear
#

essentially, on the website we have items you can "buy" but they are hardcoded onto the html file because i am unsure as to how to make them the actual items objects from our items class on spring boot

shy parrot
#

u know how the front and back ends intercat?

stoic bear
#

not really, thats what im trying to figure out

shy parrot
#

yep

stoic bear
#

im decent with java, unexperienced with html

shy parrot
#

do u know http request?

#

if user want to get server data , they need send a http request to the backend

#

if u dont know how to write a request in js

stoic bear
#

well were not really using js

#

is the thing

#

were making an extremely basic site

shy parrot
#

you can try writing a request in the browser url field now

#

u start your spring boot

#

u will see the backend return datas

#

this is actually your front-end calling your back-end methods

stoic bear
#

but is this supposed to be somewhere in the html file?

shy parrot
#

So now you just need to write a piece of js to simulate this request sending, you can use ajax, axios or even use windows.location

#

my english is not very good, some places may have problems expressing, forgive me

stoic bear
#

dont worry about it

#

thanks for the help

shy parrot
#

glad to help u

stoic bear
#

is this possible to do without javascript?

shy parrot
#

emmmm

#

php or jsp can do that

#

the others are not yet known

#

but now these two technologies have been eliminated 😂

#

If you want to try it is also possible