You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在使用<c:url>标签构建带参数链接时保留class属性?

Got it, let's sort this out for you! The key thing to remember here is that <c:url> is just a JSTL tag that generates a properly encoded URL string — it doesn't replace the HTML <a> tag, so you can't add CSS classes directly to it. Instead, you need to use <c:url> to build your URL (with parameters) and then plug that URL into the href attribute of your existing <a> tag, keeping all your styling intact.

Option 1: Store the URL in a variable (cleanest approach)

First, use <c:url> with the var attribute to save your generated URL to a variable. Then reference that variable in your <a> tag's href:

<!-- Generate the URL with parameters and store it in a variable -->
<c:url var="menuLinkUrl" value="/menu">
    <c:param name="Id" value="736"/>
    <c:param name="user" value="example"/>
</c:url>

<!-- Use the variable in your styled link -->
<a class="btn btn-outline-light mt-3 btn-lg" href="${menuLinkUrl}">Name</a>

Option 2: Embed <c:url> directly in the href attribute

If you prefer a more compact (though slightly less readable) approach, you can nest the <c:url> tag directly inside the href attribute:

<a class="btn btn-outline-light mt-3 btn-lg" href="<c:url value='/menu'>
    <c:param name='Id' value='736'/>
    <c:param name='user' value='example'/>
</c:url>">Name</a>

Why your original attempt didn't work

Your initial code only generates the URL string, but doesn't wrap it in an HTML <a> tag. Since <c:url> isn't an HTML element, adding class attributes to it has no effect — those styles belong to the <a> tag, which handles the visual presentation and user click behavior.

内容的提问来源于stack exchange,提问作者user16722881

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 12:52:37