0

I am trying insert a text in a input and submit it so I can get it in backend. This is how it looks in the frontend:

<form th:action="@{/student/mypage/}">
        <label><h3><b>Friend Token:</b></h3>
        </label><br>
           <input type="text" th:name = "friendToken"/>
           <input type="submit" />
        </form>

And this is how it looks in backend:

@Controller
@RequestMapping(path = "/student/mypage")
public class MyPageController {

    //Fields
    private final StudentService studentService;
    private final StudentAccountService studentAccountService;

    //Constructor
    @Autowired
    public MyPageController(StudentService studentService, StudentAccountService studentAccountService) {
        this.studentService = studentService;
        this.studentAccountService = studentAccountService;
    }

    /* ~~~~~~~~~~~ Get MyPage View ~~~~~~~~~~~ */
    @GetMapping
    @PreAuthorize("hasRole('ROLE_STUDENT')")
    public String getMyPage(Model model)
    {

        LoggedAccount loggedAccount = new LoggedAccount();

        if(loggedAccount.checkIfStandardAccLogged())
        {
            model.addAttribute("devUsernameAccount", loggedAccount.getLoggedUsername());
        }
        else
        {
            StudentAccount loggedStudentAccount = studentAccountService.getLoggedStudentAccount();
            //query call in db to get info of logged student
            Student infoStudent = studentService.findStudentByNameAndSurname(loggedStudentAccount);

            //getting info about logged acc (credentials) && student info
            model.addAttribute("loggedStudentAccount", loggedStudentAccount);
            model.addAttribute("infoStudent", infoStudent);
            //checkup in case we log in with a dev account
            model.addAttribute("isDevAcc", loggedAccount.checkIfStandardAccLogged().toString());
        }

        return "pages/layer 4/info pages/mypage";
    }

    @RequestMapping(value = "/{friendToken}", method = RequestMethod.POST)
    public void updateFriendToken(
            @PathVariable(required = false, name = "friendToken") String friendToken,
            Model model
    )
    {
        System.out.println("Friend Tokenul este " + friendToken);
    }
}

As you can see I want when i press submit to call that request method called updateFriendToken and get the path variable from url and print it in console. I know there may be many things I got wrong, so feel free to change the html code and java as you please. My project is built with SpringBoot and Java for backend, and HTML, CSS, JS scripts and Thymeleaf for frontend. I am adding a picture of the site, I know my url is not ok, and I can't get that way any path variable, but I really don't know how to get that text I insert in the FriendToken text box.

enter image description here

Thanks lots for helping!

elskersan
  • 1
  • 3

1 Answers1

1

Try modifying

@PathVariable(required = false, name = "friendToken") String friendToken

to

@RequestParam("friendToken") String friendToken
Sidharth
  • 152
  • 4