如何在 Spring + JSP 中创建超链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/421891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create hyperlink in Spring + JSP
提问by Steve Kuo
What's the proper way to create a hyperlink in Spring+JSP? There must be a better way than just coding in the <a href="...">tag. Take for example a page that displays people. The URL is people.htm. The corresponding controller gets people from the database and performs optional column sorting. The JSP might look like:
在 Spring+JSP 中创建超链接的正确方法是什么?肯定有比在<a href="...">标签中编码更好的方法。以显示人物的页面为例。网址是people.htm。相应的控制器从数据库中获取人员并执行可选的列排序。JSP 可能如下所示:
<table>
<tr>
<td><a href="people.htm?sort=name">Name</a></td>
<td><a href="people.htm?sort=age">Age</a></td>
<td><a href="people.htm?sort=address">Address</a></td>
</tr>
...
This seems bad as the URL people.htmis hardcoded in the JSP. There should be a way to have Spring automatically build the <a>tag using the URL defined in servlet.xml.
这看起来很糟糕,因为 URLpeople.htm是在 JSP 中硬编码的。应该有一种方法可以让 Spring<a>使用 中定义的 URL自动构建标记servlet.xml。
Edit: Maybe I should be using a Spring form.
编辑:也许我应该使用 Spring 表单。
回答by cletus
The only thing that comes to mind is the JSTL standard tag <c:url>. For example:
唯一想到的是 JSTL 标准标签<c:url>。 例如:
<c:url var="thisURL" value="homer.jsp">
<c:param name="iq" value="${homer.iq}"/>
<c:param name="checkAgainst" value="marge simpson"/>
</c:url>
<a href="<c:out value="${thisURL}"/>">Next</a>
Now this won't get you servlet mapping or the like but nothing will. It's not something you could really do programmatically (after all, a servlet can and usually does map to a range of URLs). But this will take care of escaping for you.
现在这不会让您获得 servlet 映射等,但不会。这不是您可以真正以编程方式完成的事情(毕竟,servlet 可以并且通常确实映射到一系列 URL)。但这会为你逃脱。
回答by Ole
I haven't seen this kind of functionality in pure spring (although grails offers things like that).
我还没有在纯 spring 中看到这种功能(尽管 grails 提供了类似的功能)。
For your specific case you might consider removing the file part and only using the query string as the href attribute:
对于您的特定情况,您可能会考虑删除文件部分并仅使用查询字符串作为 href 属性:
<td><a href="?sort=name">Name</a></td>
<td><a href="?sort=age">Age</a></td>
<td><a href="?sort=address">Address</a></td>
These links append the query string to the path component of the current url.
这些链接将查询字符串附加到当前 url 的路径组件。
回答by Patrik Bego
Better way to create link is:
创建链接的更好方法是:
<a href="<%=request.getContextPath() %>/people">Name</a>
<%=request.getContextPath() %> makes sure that correct URI will be taken into account.
<%=request.getContextPath() %> 确保将考虑正确的 URI。
"sort" parameter you can get over with hidden field and change a value with a little bit of javascript:
“排序”参数,您可以使用隐藏字段克服并使用一点javascript更改值:
<input type="hidden" name="sort" id="sort" value="name">
And controller method should look like this:
控制器方法应如下所示:
@RequestMapping("/people")
public String createUser(String sort) {
...
}
回答by Sarat Chandra
In Spring MVC in jsp:
在 jsp 中的 Spring MVC 中:
You can use:
您可以使用:
General Hyperlink:
一般超链接:
<a href="/Portfolio/login">Click Here</a>
If passing from controller:
如果从控制器传递:
<a href="/Portfolio/${page}">Click Here</a>
Jsp tags
Jsp 标签
<c:url var="URL" value="login">
<c:param name="param" value="${parameter}"/>
</c:url>
<a href="<c:out value="${URL}"/>">Click Here</a>
Hope it Helps.. :)
希望能帮助到你.. :)
回答by Ramkailash
Import this package in your jsp file
在你的jsp文件中导入这个包
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
when you want to redirect new page or url then use for eg.
当您想重定向新页面或 url 时,请使用例如。
<a href='<c:url value="url of next page" />'>Home</a>

