从多个 URL 返回视图时出现问题

发布于 2024-10-23 01:03:54 字数 515 浏览 2 评论 0原文

我正在使用 Spring MVC 构建一个 Web 应用程序,但遇到了问题。

目前,如果我登录并转到网络应用程序的主页,那么它会正确显示用户主页(正常的资料图像、详细信息等)——那就太好了。

但是,我正在尝试构建该功能,以便如果有人使用 url /user/username 访问该网站,那么它将重定向到控制器并尝试查找用户名为“username”的用户 - 如果找到的话它加载所选用户配置文件的视图。

到目前为止一切正常 - 但我想检查一下您是否已登录并导航到 /user/mycurrentusername 那么不要加载正常的配置文件页面,只需加载您第一次登录时看到的页面..问题是当我返回与主页相同的视图时,就像我现在在 url /user/* 一样,找不到任何页面资源(图像/css),所以我得到了页面,但没有格式/图像..

什么这是解决这个问题的最好方法吗?有更好的方法来处理这个问题吗?

(我也有问题,因为它在 /url/images 查找图像/css..它再次向我的用户控制器发出请求:( )

非常感谢!

I am building a web app using Spring MVC and I am having a problem.

Currently, if I login and go to the homepage of the webapp then it displays the user home page correctly (normal stuff profile image, details, etc) - thats all great.

However, I am trying to build in the functionality so that if anyone goes to the site with the url /user/username then it will redirect to a controller and attempt to find a user with the username "username" - if one is found then it loads the view of the selected users profile.

This is all working fine so far - but I wanted to check that if you are logged in and navigate to /user/mycurrentusername then dont load the normal profile page, just load the page you see when you first logon.. the problem is that when I return the same view I do for the homepage, being as I am now at url /user/* none of the page resources can be found (images/css) so i get the page but with no formatting/images..

What would be the best way around this? is there a better way to handle this?

(I also have problems that as it looks for images/css at /url/images.. it fires a request to my user Controller again :( )

Help much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黑凤梨 2024-10-30 01:03:54

理想情况下,您为样式表等创建的 url 应该使用某种 url 重写标记来创建,以确保它们相对于根应用程序上下文而不是当前 url。您使用什么视图技术? JSP?如果是这样,你会想要这样的 标记将创建一个实际上恒定的 URL,不会随当前页面请求而改变。

至于您的控制器受到 css/images 等请求的影响,请查看 这个问题。简而言之,理想情况下您希望使用 Tuckey 的 Url Rewrite 过滤器之类的东西来确保静态内容请求不会调用您的 DispatcherServlet。

编辑:实际上看起来 Spring 3.04 现在有比 Tuckey's Filter 更好的解决方案。 看起来设置起来更简单。

The urls you're creating for your stylesheets etc. should be created ideally with some sort of url rewriting tag, to ensure they're relative to the root app context and not to the current url. What view technology are you using? JSP? If so you'll want something like this <link rel="stylesheet" href="<c:url value='/css/styles.css'/>" type="text/css"/>. The <c:url/> tag will create a url which is effectively constant and won't change with your current page request.

As for your controller being hit on requests for css/images etc, check out this question. In short, ideally you want to use something like Tuckey's Url Rewrite filter to make sure that your DispatcherServlet isn't called for static content requests.

Edit: Actually looks like Spring 3.04 has a nicer solution to this now than Tuckey's Filter. <mvc:resources/> looks much simpler to setup.

初懵 2024-10-30 01:03:54

对于第一个问题,您可能将图像和 CSS 源指定为相对路径。由于视图是从不同的路径渲染的,因此相对路径也不同。有关如何正确指定它们的信息,请参阅 Melv 的答案。

对于第二个问题,请查看 spring mvc 文档 了解如何让静态请求通过 Dispatcher Servlet。

For the first problem you likely are specifying your image and css sources as relative paths. Since the view is rendered from a different path, that relative path is also different. See Melv's answer for how to get them specified correctly.

For the second problem, have a look at section 15.12.4 in the spring mvc docs for how to let static requests pass through the Dispatcher Servlet.

野侃 2024-10-30 01:03:54

您基本上有三个选择:

  1. 不要更改您的网络上下文。将所有 URL 设置为 http://myapp/myrequest?par1=val1。我以前做过这种应用程序,但是当有很多可能的操作时,URL将无法组织。 “REST 风格”URL 更漂亮,并且是世界不久前的发展方向。

  2. 在所有内部网址中使用 。我认为这不是一个坏选择,但有很多样板代码,比乍一看要多。如果您使用 javascript 调用 URL,则必须知道哪个是正确的 URL,这可能会很笨拙。

  3. 在我当前的开发中,我使用 标签。您必须在所有页面的头部包含 。然后,您可以使所有 URL 相对于应用程序的基本路径。虽然有一些缺点,但这是我的首选。

编辑:回答你的问题。您可以通过以下方式获取应用程序的根 URL,而不是参数化您的应用程序:

<base href="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}/" />

You basically have three options:

  1. Don't change your web context. Make all your URLs like http://myapp/myrequest?par1=val1. I did this kind of applications time ago, but when there are a lot of possible operations URLs won't be organized. "REST style" URLs are prettier and is where the world is going to from some time ago.

  2. Use <c:url> in ALL your internal URLs. I think this is not a bad option, but is a lot of boilerplate code, more than it seems at first glance. If you call URLs with javascript must be aware of which is the correct URL, and can be clumsy.

  3. In my current developement, I make use of the <BASE> tag. You must include <base href="http://myhost:8080/myapp/" /> in the head of all your pages. Then you can make all your URLs relative to your application's base path. There are some downsides, but is my preferred option.

EDIT: Answering your question. Instead of parameterise your application you can obtain the application's root URL this way:

<base href="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}/" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文