0

Get @PathVariable from Spring Boot URL

asked 2023-11-08 00:24:22 +0800

BenLimanto gravatar image BenLimanto
1 1

How to get @PathVariable from Spring Boot to the MVC Page Controller?

Is it possible?

delete flag offensive retag edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2023-11-10 12:16:39 +0800

BenLimanto gravatar image BenLimanto
1 1

updated 2023-11-10 12:18:09 +0800

Seems there are no way other than using Session and Use HttpServletRequest then?

Ugh. it's hacky isn't it? It's the controller method

import jakarta.servlet.http.HttpServletRequest;
@GetMapping("/mvvm/{id}")
public String MvvmPageWithId(@PathVariable Integer id, HttpServletRequest request)
{
    // Hacky? 
    request.getSession().setAttribute("id", id);
    return "mvvm";
}

On the Mvvm Method (if MVVM?), but same I think if it's MVC, on the doAfterInit()?

import org.springframework.web.bind.annotation.PathVariable;
import jakarta.servlet.http.HttpServletRequest;

private Integer id;

@Init
public void init() {
    // MVVM Init
    // @see books[dot]zkoss[dot]org/zk-mvvm-book/8.0/viewmodel/initialization.html

    Execution exec = Executions.getCurrent();
    HttpServletRequest request = (HttpServletRequest) exec.getNativeRequest();

    Object o = request.getSession().getAttribute("id");

    if (o == null) return;

    String idSession = String.valueOf(o);
    request.getSession().removeAttribute("id");
    String idUrl = idSession;

    try {
        this.setId(Integer.parseInt(idUrl));
    }
    catch(Exception e)
    {
        Messagebox.show("Its not int : " + idUrl, new Button[] { Button.OK }, null);
    }
}
link publish delete flag offensive edit
0

answered 2023-11-14 17:47:50 +0800

hawk gravatar image hawk
3205 1 5
http://hawkphoenix.blogsp... ZK Team

updated 2023-11-14 17:49:09 +0800

I think it's not hacky, since spring handles a request differently from ZK. You can pass it through a session or a request attribute. But I think storing the id into the request scope is better than the session scope. Since this will not affect other browser tabs. For example:

@GetMapping("/mvvm/{id}")
public String mvvmExample(@PathVariable Integer id, HttpServletRequest request) {
    request.setAttribute("id", id);
    return "mvvm";
}

In a ViewModel:

@Init
public void init() {
    Executions.getCurrent().getAttribute("id");
}

ZK MVC pattern means you control UI by calling ZK component's API. What you use is Spring MVC which is different from ZK MVC.

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2023-11-08 00:24:22 +0800

Seen: 6 times

Last updated: Nov 14

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More