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

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

问题描述

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

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.

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

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

问题是webapp认为它 rootto,well,root或简称为/,而HTML会认为你的应用程序的根目录是/ 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".

Servlet API和JSP具有帮助管理它的工具。例如,如果在servlet中执行:response.sendRedirect(/ mypage.jsp),容器将在上下文之前创建url: http://example.com/myapp/mypage.jsp

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".

然而,你不能例如,使用HTML中的IMG标记。如果你执行< img src =/ myimage.gif/>,你可能会获得404,因为你真正想要的是/myapp/myimage.gif。

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".

许多框架都有具有上下文感知功能的JSP标记,并且有不同的方法可以在JSP中创建正确的URL(没有特别优雅)。

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).

对于编码员而言,使用App Relative网址与绝对网址之间的关系是一个很小的问题。

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

最后,存在需要动态创建URL的Javascript代码以及CSS中的嵌入式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.

你做什么?

推荐答案

您可以使用JSTL创建网址。

You can use JSTL for creating urls.

例如,< c:url value = /images/header.jpg/> 将为上下文根添加前缀。

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

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

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

我有这样的网络根结构:

I have a web root structure like this:

/ css

/图像

/css
/images

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

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.

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

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).

显然这一切都取决于你使用的JSP和JSTL,但是我使用了FaceF的JSF并且所涉及的技术是相似的 - 唯一真正的区别在于以不同的方式获取上下文根。

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天全站免登陆