使用'a href'构建动态网址 [英] Building dynamic URL using 'a href'

查看:155
本文介绍了使用'a href'构建动态网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

请求映射用于映射URL,在这种情况下,当使用

 < a href =company = 1 / branch = 1 / employee = 1 / info>员工信息< / a> 

该方法在具有精确的@RequestMapping注解的控制器中调用,现在我想创建 href标签,并且想动态创建companyId,branchId或employeeId。

解决方案

你当然可以创建字符串指针动态地分配给相应的URL。

第一个选项是使用javascript函数。但是,即使这个函数必须从某个地方获取ID。在我的例子中,我想有一些javascript变量已经包含正确的ID。

 函数createDynamicURL()
{
//要返回的变量
var URL;

//包含相应ID的变量
var companyID = ...
var branchID = ...
var employeeID = ...

//形成变量返回
URL + =company =;
URL + =公司ID;
URL + =/ branch =;
URL + = branchID;
URL + =/ employee =;
URL + = employeeID;
URL + =/ info;

返回URL;
}

然后你的html会是这样的:

 < a href =javascript:window.location = createDynamicURL(); >员工信息< / a> 

另一个更优雅的解决方案是使用onClick事件:

 < a href =#onclick =RedirectURL(); return false; >员工信息< / a> 

与函数

 函数RedirectURL()
{
window.location = createDynamicURL();
}

希望我能帮上忙!


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.

解决方案

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!

这篇关于使用'a href'构建动态网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆