Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

275
Visualizações
Passing javascript variable to spring MVC controller with request param

Is it possible to send a javascript variable to a controller endpoint, and then have the controller return a new view? I've tried using a requestbody and ajax to do it, which passes the variable correctly, but is unable to load a new view.

Maybe there's a way to do it with thymeleaf?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you're using Thymeleaf then just reference the template you want to return to the user by its filename.

@Controller
public class YourController {

    @GetMapping("/someUrl")
    public String getTemplate(@RequestParam String templateName){
        return templateName;
    }
}

This would be the bare minimum that you'd need, assuming your templates are in the correct folder - by default in resources/static. If the frontend sends a GET (/someUrl?templateName=xyz) to the backend, it would return the xyz.html from your templates. If it does not find the template that was requested in the parameters then it will return a 404.

Edit: Reading through the comments I realized there might be a confusion between @RequestParam and @PathVariable. In case you want to use a path variable to define the template name, so that the frontend can call GET (/someUrl/xyz), then you can do

@Controller
public class YourController {

    @GetMapping("/someUrl/{templateName}")
    public String getTemplate(@PathVariable String templateName){
        return templateName;
    }
}

Resources for both below:

https://www.baeldung.com/spring-request-param

https://www.baeldung.com/spring-pathvariable

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda