简体   繁体   中英

Spring MVC, Thymleaf on ajax call return new HTML page

I am new to thymeleaf. I am planning to implement simple web application with html pages. My landing page controller url is below which returns index.html page

@RequestMapping("/index")
public String index() {
    return "index";
}

I have a button in index.html page which triggers below controller call and want to return a different onclick.html HTML page

@RequestMapping("/web/onclick/{onclickvar}")
public String onclick(Model model, @PathVariable("onclickvar") String onclickvar) {
//do something with onclickvar
    return "onclick";
}

Can anybody provide sample code please.

You can use an anchor link instead of a button to do the same.

<a href="onclick.html" th:href="@{/web/onclick/${someValueForClickvar}">Click here to go to onclick.html</a>

The above link will hit the controller and @PathVariable("onclickvar") String onclickvar should have the value "someValueForClickvar" .

Note : note that here /web/onclick/${someValueForClickvar} is the value which you want to pass as a path variable so change the variable name accordigly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM