kartikag01
kartikag01

Reputation: 1579

Returning multiple view from spring controller

I want to return multiple views(jsp) from one controller. i.e. one view below another

@RequestMapping(method = RequestMethod.GET, value = "register")
public String addUser(Model model) {
    // on some condition
    if(){
     //add "user/login" above or below "user/edit"
    }
    model.addAttribute(new User());
    return "user/edit";
}

i want to do this on controller not on jsp

it can be possible or i have to use tiles for it

Upvotes: 0

Views: 2114

Answers (1)

Alan Hay
Alan Hay

Reputation: 23226

You can only return one view. If you do not want to use a templating library then you need to set some model attribute and then use that to conditionally render some additional HTML.

CONTROLLER

@RequestMapping(method = RequestMethod.GET, value = "register")
public String addUser(Model model) {

    if(x){
        model.addAttribute("showAdditionalFields", true);
    }

    model.addAttribute(new User());

    return "user/edit";
}

JSP

<c:if test="${showAdditionalFields}">
    <!-- include here -->
</c:if>

Upvotes: 1

Related Questions