Nan*_*com 12 spring spring-mvc spring-3
我使用spring 3.0,我有一个非常简单的问题,但在互联网上没有找到任何答案.我想生成一个路径(URI),就像在我的JSP中一样:
<spring:url value="/my/url" />
Run Code Online (Sandbox Code Playgroud)
但在控制器内部.使用的相关服务是什么?谢谢!
编辑:可能与此有关:http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/resources.html#resources-resourceloader?对此没有更好的解决方案吗?
Chr*_*ang 36
罗森的建议是黄金.
还有来自3.1的ServletUriComponentsBuilder类,它以静态方式从当前请求构建URL.例如:
ServletUriComponentsBuilder.fromCurrentContextPath().path("/my/additional/path").build().toUriString();
Run Code Online (Sandbox Code Playgroud)
它是<spring:url>servlet中最接近的东西.
我会说
\n\nrequest.getRequestURL() + "/my/url"\nRun Code Online (Sandbox Code Playgroud)\n\n完成工作。没有这样的内置功能, spring:url 调用 UrlTag.class ,该类具有以下方法来生成 URL,您可以将其用作代码的 ins\xc5\x91iration :
\n\nprivate String createUrl() throws JspException {\n HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();\n HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();\n StringBuilder url = new StringBuilder();\n if (this.type == UrlType.CONTEXT_RELATIVE) {\n // add application context to url\n if (this.context == null) {\n url.append(request.getContextPath());\n }\n else {\n url.append(this.context);\n }\n }\n if (this.type != UrlType.RELATIVE && this.type != UrlType.ABSOLUTE && !this.value.startsWith("/")) {\n url.append("/");\n }\n url.append(replaceUriTemplateParams(this.value, this.params, this.templateParams));\n url.append(createQueryString(this.params, this.templateParams, (url.indexOf("?") == -1)));\n\n String urlStr = url.toString();\n if (this.type != UrlType.ABSOLUTE) {\n // Add the session identifier if needed\n // (Do not embed the session identifier in a remote link!)\n urlStr = response.encodeURL(urlStr);\n }\n\n // HTML and/or JavaScript escape, if demanded.\n urlStr = isHtmlEscape() ? HtmlUtils.htmlEscape(urlStr) : urlStr;\n urlStr = this.javaScriptEscape ? JavaScriptUtils.javaScriptEscape(urlStr) : urlStr;\n\n return urlStr;\n}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
16830 次 |
| 最近记录: |