如何在使用<c:url>标签构建带参数链接时保留class属性?
<c:url> with HTML Link Attributes (Like 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

