Building dynamic URL using 'a href'

时光毁灭记忆、已成空白 提交于 2019-12-07 13:23:34

问题


I am using spring-3.2 version.

@RequestMapping("/company={companyId}/branch={branchId}/employee={employeeId}/info")

The requestmapping is used to map a URL, so in this case when ever a URL is called using

<a href="company=1/branch=1/employee=1/info" > employee info </a>

the method is called in the controller with the exact @RequestMapping annotation, now I want to create the "a href" tag dynamically and want to create companyId,branchId,or employeeId dynamically.


回答1:


You could of course build the string pointing to the respective URL dynamically.

A first option would be using a javascript function. However, even this function has to take the IDs from somewhere. In my example, I suppose that there are javascript variables which already contain the right IDs.

function createDynamicURL()
{
    //The variable to be returned
    var URL;

    //The variables containing the respective IDs
    var companyID=...
    var branchID=...
    var employeeID=...

    //Forming the variable to return    
    URL+="company=";
    URL+=companyID;
    URL+="/branch=";
    URL+=branchID;
    URL+="/employee=";
    URL+=employeeID;
    URL+="/info";

    return URL;
}

Then your html would be like:

<a href="javascript:window.location=createDynamicURL();" > employee info </a>

Another, more elegant solution would be to use the onClick event:

<a href="#" onclick="RedirectURL();return false;" > employee info </a>

with the function

function RedirectURL()
{
    window.location= createDynamicURL();
}

Hope I helped!



来源:https://stackoverflow.com/questions/20834002/building-dynamic-url-using-a-href

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!