I want to change the screen transition destination according to the contents entered in the form.

Asked 2 years ago, Updated 2 years ago, 62 views

I just started developing Spring Boot + Thymeleaf + MySQL.

I'd like to change the screen transition destination according to what you fill in the form, but it's not working.

For example, if you type "1" (DB ID) in the input form,
http://localhost:8080/edit/1
I would like to transition to .

I'm sorry for the rudimentary question, but please answer it.

◆ Source Code

[html]

<form method="post" action="/edit">
    <h1>Please input the number</h1>
    <input type="text" name="id" th:value="${id}"/>
    <input type="submit" value="Send">
    <br>
    <ath:href="@{/}">Backenter display</a>
    </form>

Controllers

@RequestMapping(value="/edit/{id}", method=RequestMethod.GET)
    publicModelAndViewedit(@ModelAttributeMyDatamydata,@PathVariableintid,ModelAndViewmav){
        mav.setViewName("edit");
        mav.addObject("title", "edit mydata");
        mav.addObject("id", id);
        Optional<MyData>data=repository.findById(long)id);
        mav.addObject("formModel", data.get());
        return mav;
    }
    @RequestMapping(value="/edit/{id}", method=RequestMethod.POST)
    @Transactional(readOnly=false)
    publicModelAndView update(@ModelAttributeMyDatamydata,@PathVariableintid,ModelAndViewmav){
        mydata.setId(id);
        repository.saveAndFlash(mydata);
        return new ModelAndView("redirect:/");
    }

    @RequestMapping(value="/searchId", method=RequestMethod.GET)
    publicModelAndViewsearchId(@ModelAttribute("formModel")MyDatamydata,ModelAndViewmav){
        mav.setViewName("searchId");
        return mav;
    }

    @RequestMapping(value="/searchId", method=RequestMethod.POST)
    publicModelAndView serachId(@PathVariableintid,ModelAndViewmav){
        mav.setViewName("searchId");
        return new ModelAndView("'/edit");
    }

[Error Contents]
If you type "1" in the input form and press the Send button,
The URL should be http://localhost:8080/edit and
The error message is as follows:

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

    Wed Oct 09 19:26:41 JST 2019
    There was an unexpected error (type=Not Found, status=404).
    No message available
--


directly to the URL in your browser http://localhost:8080/edit/1
If you enter , you have verified that the screen transition is correct.

Please let me know how to fix it.

java mysql spring-boot thymeleaf

2022-09-30 19:24

1 Answers

<form method="post" action="/edit">
    <h1>Please input the number</h1>
    <input type="text" name="id" th:value="${id}"/>

Therefore, when you click the button, the request parameter id is sent at POST for the path /edit.Therefore, I think the following methods should be used with the @RequestMapping value "/edit" and the method is POST and the id argument with @RequestParam.

@RequestMapping(value="/edit", method=RequestMethod.POST)
    publicModelAndView update(@ModelAttributeMyDatamydata,@RequestParamintid,...)

動作 I'm not checking the operation, so I might be wrong.


2022-09-30 19:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.