I have a controller that handle clicking on links. In handler method i have to do something (on db) and open clicked url in new window (something like _blank
only a simple change is required in html. Add attribute target="_blank" in link
Visit W3Schools
Visit W3Schools
I solved this using JavaScript and AJAX - as @Patrick suggest. Maybe it will be helpful for someone.
<a href="#" onclick="openLink(${link.id},'${link.address}');">Open</a>
openLink
function:
function openLink(id, url) {
jQuery.get('open.html?id='+id, function(data) {
if(data == 'OK') {
window.open(url);
}
}, 'text');
}
Handler method:
@ResponseBody
@RequestMapping(value = "/open.html")
public String open(@RequestParam(value="id") Integer id) {
Link link = linkDAO.get(id);
linkDAO.click(id);
return "OK";
}