0

SpringBoot controller, pass variables from the method @GetMapping to ZK page composer

asked 2021-08-29 05:30:45 +0800

franferri gravatar image franferri
48 2

Sorry for my ignorance, this must be quite straight forward but after googling a bit I didn't find a nice way of doing it.

So, I do have a class in springboot as @Controller with the following method

@GetMapping("/plainzul")
public String plainZULExample(Model model) {
    model.addAttribute("attribute1", "ZkRocks!");
    model.addAttribute("attribute2", randomObjectForThisExample);

    anotherRandomObject = new AnotherRandomObject()

    return "plainzul";
}

I do have a page called plainzul.zul with a SelectorComposer which basically is a plain:

@Override
public void doAfterCompose(Div comp) throws Exception {
    super.doAfterCompose(comp);

    System.out.println("Hello cor3000 !");
}

How can I access to the attribute1? Or basically, the real question is how can I have an object in the spring controller from the Composer? maybe using the model.setAttribute is subobtimal.

Any help or examples?

How do I get the anotherRandomObject for example?

Many thanks

delete flag offensive retag edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-08-30 11:18:31 +0800

cor3000 gravatar image cor3000
6280 2 7

I my local tests Spring just transforms the Model attributes into native request attributes.

This matches the somewhat vague Spring MVC 4.0.x documentation (didn't find the equivalent for Spring 5, just assuming it's the same):

The model Map is simply transformed into an appropriate format, such as JSP request attributes, a Velocity template model.

JSP/ZUL page are roughly/technically equivalent, both are handled by Servlets more or less, based on the same HttpServletRequest.

While it's possible to access the native request directly -(HttpServletRequest)Executions.getCurrent().getNativeRequest()) - the built in ZK abstraction is an Execution (wrapping around the request/response objects).

In your composer you can access the execution attributes as follows:

@Override
public void doAfterCompose(Div comp) throws Exception {
    super.doAfterCompose(comp);

    Execution exec = Executions.getCurrent();

    String attr1 = (String) exec.getAttribute("attribute1");
    RandomClassForThisExample randomObjectForThisExample = 
            (RandomClassForThisExample) exec.getAttribute("attribute2");

    System.out.println("Hello cor3000 !");
}
link publish delete flag offensive edit

Comments

Brilliant, many thanks cor3000!

franferri ( 2021-08-30 17:48:44 +0800 )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
1 follower

RSS

Stats

Asked: 2021-08-29 05:30:45 +0800

Seen: 8 times

Last updated: Aug 30 '21

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