Spring MVC: Open link in new browser window in handler method

前端 未结 2 1755
没有蜡笔的小新
没有蜡笔的小新 2021-01-03 16:46

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

相关标签:
2条回答
  • 2021-01-03 17:22

    only a simple change is required in html. Add attribute target="_blank" in link

    Visit W3Schools

    Visit W3Schools

    0 讨论(0)
  • 2021-01-03 17:35

    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";
    }
    
    0 讨论(0)
提交回复
热议问题