Home > Back-end >  Missing request attribute 'projektId' of type String | Thymleaf Form with just a String
Missing request attribute 'projektId' of type String | Thymleaf Form with just a String

Time:08-01

I'm working on a Projekt where you can add workers to projects with their ids.I am using springboot, thymeleaf and a database means you give a project and a worker Id and the programm adds the worker to the project.workerlist. The Problem ist that I get this error:

Required request parameter 'projektId' for method parameter type String is not present

My HTML Form looks like this

<form action="#" th:action="@{neuenMitarbeiterzuProjektHinzufuegen}" method="post">
        Projekt ID: <input type="text" th:value="*{projektId}" required/><br>
        Mitarbeiter ID: <input type="text" th:value="*{mitarbeiterId}" required/><br>
        <br>
        <input type="submit" value="Mitarbeiter hinzufügen"/>
        <input type="reset" value="Clear"/>
    </form>

My Post Route Handler Method looks like this

    @PostMapping(value="/neuenMitarbeiterzuProjektHinzufuegen")
public String neuenMitarbeiterzuProjektHinzufuegen(@RequestAttribute(value = "projektId") String projektID, @RequestAttribute(value = "mitarbeiterId") String mitarbeiterID,Model m)
{
    Optional<Projekt> projekt = projektRepository.findById(Long.parseLong(projektID));
    projektRepository.findById(Long.parseLong(projektID)).get().mitarbeiterHinzufuegen(mitarbeiterRepository.findById(Long.parseLong(mitarbeiterID)).get());
    return "redirect:Projekte";
}

CodePudding user response:

Looking at your code example I think you should be using @RequestParam not @RequestAttribute. Param is for things posted from the user (web) side and attribute you can set on the server side.

This blog has some explanation on the difference of @RequestAttribute https://www.logicbig.com/tutorials/spring-framework/spring-web-mvc/request-attribute.html

  • Related