0

I made a Form using Thymeleaf but running into this issue. I read many articles but didn't find any solution.
Any solution you can suggest ?

Project Controller ->

@Controller
public class Controllers {

@GetMapping("/home")
public ModelAndView home(){
    System.out.println("User is in Homepage");
    return new ModelAndView("index");
}

@GetMapping("/service")
public ModelAndView service(){
    System.out.println("User is in Service Page");
    return new ModelAndView("service");
}

@GetMapping("/about")
public ModelAndView about(){
    System.out.println("User is in About page");
    return new ModelAndView("about");
}

Here's Controller Class for submitting form ->

 @Controller

public class SavingUser{

@Autowired
private UserRepository userRepository;


@PostMapping("/registerUser")
public ModelAndView user(@ModelAttribute Customer customer, ModelMap model){
    System.out.println("User in registration page..");
    userRepository.save(customer);
    model.addAttribute("saveUser", customer);
    return new ModelAndView("index");
 }
}

And here's my HTML Form -

    <div id="form">
    <form action="registerUser" th:action="@{/registerUser}"  th:object="${saveUser}" method="POST">
        <br />
        <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
        <label for="name">Your Name:</label><br />
        <input type="text" th:field="*{name}"  placeholder="" /><br />

        <label for="suburb">Your Suburb</label><br />
        <input type="text"  th:field="*{suburb}"  placeholder="" /><br />

        
            <input class="submit" type="submit" value="Submit"  />
            <br /><br />
        </div>
    </form>
    </div>

I tried to remove action="", still it didn't work.

1 Answer 1

1

Well, from what I see in the code you miss the @GetMapping method in your controller to actually display the page. Not very clear in what step you get 405 status. It would be useful if you also add the relevant exception message from the console.

Edit: to answer the original question, you get 405 because in the Post Controller you make a POST request to "/services" which dosen't exists (only the GET exists for services).

    @PostMapping("/registerUser")
    public ModelAndView user(@Valid @ModelAttribute Customer customer, BindingResult result, ModelMap model){
        [...]
        return new ModelAndView("service"); // this makes a POST to "service" endpoint!
    }

To correct that, you must make a redirect to the page like this:

    @PostMapping("/registerUser")
    public ModelAndView user(@Valid @ModelAttribute Customer customer, BindingResult result, ModelMap model){
        [...]
        return new ModelAndView("redirect:/service"); // this makes a GET to "service" endpoint
    }

Leaving that aside, there are many things that can be imporoved. First of all, you are not using Thymeleaf in your project. No Thymeleaf markup will be processed. To use it you must add first the dependency, then configure Thymeleaf as your HTML resolver. The proper way to do all that is detailed here.

Also, I really recommend reading Thymeleaf documentation and follow a few tutorials to understand how things work.

Sign up to request clarification or add additional context in comments.

10 Comments

Can you clarify what a @GetMapping does here and what should that return ? I have another controller class for all the controllers which already display the views well. and this form, when I click submit, the console message is ' method 'POST' not supported'
@WaisShuja I was referring to the controller class that display the page. There are many things that may trigger 405 status, and without additional info is hard to debug. For example it could be triggered by security setup, a wrong method call caused by incompatible objects or mixing up https and http.
@WaisShuja The controller that displays "register user" page is still missing from description. Submitting a form with Thymeleaf has the same level of difficulty as JSP, nothing too fancy. Here the problem lies in something that isn't shown yet. Can you zip the project if it's not something sensible and give the link here? It would be easier to see what's actually happening.
@WaisShuja I'll look into it and return with an answer, no worries.
@WaisShuja Updated the original answer after seeing the project. Cheers!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.