在 Web 应用程序中处理上下文的任何巧妙方法? [英] Any clever ways of handling the context in a web app?

查看:20
本文介绍了在 Web 应用程序中处理上下文的任何巧妙方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Java 中,Web 应用程序捆绑在 WAR 中.默认情况下,许多 servlet 容器将使用 WAR 名称作为应用程序的上下文名称.

因此 myapp.war 被部署到 http://example.com/myapp.

问题是 web 应用程序认为它的根"是根",或者只是/",而 HTML 将您的应用程序的根视为/myapp".

Servlet API 和 JSP 具有帮助管理此功能的工具.例如,如果在 servlet 中,您执行:response.sendRedirect("/mypage.jsp"),则容器将添加上下文并创建 url:http://example.com/myapp/mypage.jsp".

但是,您不能使用 HTML 中的 IMG 标签来实现.如果你执行 <img src="/myimage.gif"/> 你可能会得到 404,因为你真正想要的是/myapp/myimage.gif".

许多框架都具有上下文感知的 JSP 标记,并且在 JSP 中有不同的方法来制作正确的 URL(没有一种特别优雅).

对于编码人员来说,在何时使用应用程序相对"网址而不是绝对网址是一个小问题.

最后,还有一个 Javascript 代码问题,需要动态创建 URL,并在 CSS 中嵌入 URL(用于背景图像等).

我很好奇其他人使用什么技术来缓解和解决这个问题.许多人只是简单地将其硬编码到服务器根目录或他们碰巧使用的任何上下文中.我已经知道那个答案了,这不是我要找的.

你在做什么?

解决方案

您可以使用 JSTL 来创建 url.

例如,<c:url value="/images/header.jpg"/> 将作为上下文根的前缀.

使用 CSS,这对我来说通常不是问题.

我有一个像这样的网络根结构:

/css
/图像

在 CSS 文件中,您只需要使用相对 URL (../images/header.jpg) 并且不需要知道上下文根.

至于 JavaScript,对我有用的是在页眉中包含一些常见的 JavaScript,如下所示:

然后您可以在所有脚本中使用上下文根(或者,您可以定义一个函数来构建路径 - 可能更灵活一些).

显然,这完全取决于您使用 JSP 和 JSTL,但我将 JSF 与 Facelets 一起使用,所涉及的技术是相似的 - 唯一真正的区别是以不同的方式获取上下文根.

In Java, web apps are bundled in to WARs. By default, many servlet containers will use the WAR name as the context name for the application.

Thus myapp.war gets deployed to http://example.com/myapp.

The problem is that the webapp considers its "root" to be, well, "root", or simply "/", whereas HTML would consider the root of your application to be "/myapp".

The Servlet API and JSP have facilities to help manage this. For example, if, in a servlet, you do: response.sendRedirect("/mypage.jsp"), the container will prepend the context and create the url: http://example.com/myapp/mypage.jsp".

However, you can't do that with, say, the IMG tag in HTML. If you do <img src="/myimage.gif"/> you will likely get a 404, because what you really wanted was "/myapp/myimage.gif".

Many frameworks have JSP tags that are context aware as well, and there are different ways of making correct URLs within JSP (none particularly elegantly).

It's a nitty problem for coders to jump in an out of when to use an "App Relative" url, vs an absolute url.

Finally, there's the issue of Javascript code that needs to create URLs on the fly, and embedded URLs within CSS (for background images and the like).

I'm curious what techniques others use to mitigate and work around this issue. Many simply punt and hard code it, either to server root or to whatever context they happen to be using. I already know that answer, that's not what I'm looking for.

What do you do?

解决方案

You can use JSTL for creating urls.

For example, <c:url value="/images/header.jpg" /> will prefix the context root.

With CSS, this usually isn't an issue for me.

I have a web root structure like this:

/css
/images

In the CSS file, you then just need to use relative URLs (../images/header.jpg) and it doesn't need to be aware of the context root.

As for JavaScript, what works for me is including some common JavaScript in the page header like this:

<script type="text/javascript">
var CONTEXT_ROOT = '<%= request.getContextPath() %>';
</script>

Then you can use the context root in all your scripts (or, you can define a function to build paths - may be a bit more flexible).

Obviously this all depends on your using JSPs and JSTL, but I use JSF with Facelets and the techniques involved are similar - the only real difference is getting the context root in a different way.

这篇关于在 Web 应用程序中处理上下文的任何巧妙方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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