【问题标题】:How to harness url in Spring MVC?如何在 Spring MVC 中利用 url?
【发布时间】:2016-12-30 20:38:44
【问题描述】:

在网络应用程序(在 Eclipse 上开发)中,我希望用户在浏览器中利用 url。 Web app基于java spring mvc,控制器返回html页面。 所有 html 页面都在 WebContent/WEB-INF/views 文件夹中。所有 css\javacript\images 都在 WebContent/resources/{css\javacript\images} 文件夹中。

以下是此网络应用应访问的网址

  1. localhost:8080/Project/home - 用于 home.html
  2. localhost:8080/Project/about - for about.html
  3. localhost:8080/Project/vendor - 用于vendor.html(点击后将显示所有供应商详细信息列表)

现在我想为供应商实现类别过滤器

  1. localhost:8080/Project/vendor/med - 用于 vendor.html(使用 js 重用页面以仅显示医疗供应商详细信息列表)
  2. localhost:8080/Project/vendor/army - 用于 vendor.html(使用 js 重用页面以仅显示军队供应商详细信息列表)
  3. localhost:8080/Project/vendor/other - 用于 vendor.html(使用 js 重用页面以仅显示其他供应商详细信息列表)

在 vendor.html 上进一步(可能是 {all, med, Army, other} 供应商)点击名称链接并将网址设为

localhost:8080/Project/vendor/med/vendor_XX 显示所选 vendor_XX 的完整信息 -(在 vendor_XX.html 中编码)

所有提交都是GET类型

home/about/vendor_XX.html

<html>
<link rel="stylesheet" href="resources/css/mystyle.css" type="text/css" />

<a href="home">Home</a>
<a href="vendor">Vendor</a>
<a href="about">About</a>

<a href="vendor/med">Medical</a>
<a href="vendor/army">Army</a>
<a href="vendor/other">Other</a>

// and other non relevant stuff 
</html>

vendor.html

 <html>

<link rel="stylesheet" href="resources/css/mystyle.css" type="text/css" />

<a href="home">Home</a>
<a href="vendor">Vendor</a>
<a href="about">About</a>

<a href="vendor/med">Medical</a>
<a href="vendor/army">Army</a>
<a href="vendor/other">Other</a>

// generating below 3 line dynamically with js
<a href="vendor/med/vendor_xx">Vendor_XX</a>
<a href="vendor/med/vendor_yy">Vendor_YY</a>
<a href="vendor/other/vendor_zz">Vendor_ZZ</a>

// and other non relevant stuff 
</html>

我的控制器

@Controller
public class AppController {

@RequestMapping(value = "home", method = RequestMethod.GET)
public String home() {
return "home";
}

@RequestMapping(value = "vendor", method = RequestMethod.GET)
 public String vendor() {
return "vendor";
}

@RequestMapping(value = "vendor/med", method = RequestMethod.GET)
public String vendorMed() {
return "vendor";
}
@RequestMapping(value = "vendor/army", method = RequestMethod.GET)
public String vendorArmy() {
return "vendor";
}
@RequestMapping(value = "vendor/med/vendor_xx", method = RequestMethod.GET)
public String vendorMedXX() {
return "vendor_xx";
}
//all sample urls are given
}

Resources 文件夹被添加到项目的构建路径中

localhost:8080/Project/vendor/med/vendor_XX 将上面的 url 视为 localhost:8080/Project/level_1/level_2/level_3

问题 1) - 除了 level_1 之外的所有 url 都没有找到 css。 level_2 url 需要 css 导入为&lt;link rel="stylesheet" href="../resources/css/mystyle.css" type="text/css" /&gt; level_3 url需要css导入为&lt;link rel="stylesheet" href="../../resources/css/mystyle.css" type="text/css" /&gt;

问题 1 - 为什么不从资源中加载 css。我错过了什么吗?

2) - 如果我点击

 <a href="home">Home</a> 

从 level_1/level_2 vendor.html,它被定向到 level_1/home。因此在控制器请求映射中找不到。

问题 2 - 我们如何重定向到 localhost:8080/Project/home ?

【问题讨论】:

    标签: java spring-mvc url


    【解决方案1】:

    URL 的工作方式与命令行中的路径非常相似。

    如果您在目录/foo/bar/ 中并执行命令less file.txt,您将打开文件/foo/bar/file.txt,因为您使用的是相对路径。

    如果要打开文件/file.txt,则需要less ../../file.txt 向上两个目录,或者只需less /file.txt 从根目录开始。

    当您在一个 URL (即您在浏览器的地址栏中看到的)为http://localhost/Project/vendor/med/vendor_xx 的页面上,并且您在resources/css/mystyle.css 加载文件时,浏览器将从http://localhost/Project/vendor/med/resources/css/mystyle.css 加载它,因为你使用的是相对文件,而当前的“目录”是http://localhost/Project/vendor/med/

    要从正确的 URL 加载它,即 http://localhost/Project/resources/css/mystyle.css,您需要使用 ../../resources/css/mystyle.css 向上两个目录,或者您需要从根目录开始的绝对路径:/Project/resources/css/mystyle.css

    为避免硬编码项目的上下文路径(即/Project),您可以使用 JSTL 的 c:url 标签,或者干脆

    href="${pageContext.request.contextPath}/resources/css/mystyle.css"
    

    我强烈建议几乎总是使用像上面这样的绝对路径,只要你在“目录”,它就可以工作。

    【讨论】:

    • 感谢 Mate,为您的回答提供了很好的学习体验,而且效果很好。两个问题都解决了。我正在使用 Thymeleaf 来评估绝对路径。
    【解决方案2】:

    要访问你需要的资源,你需要在配置类中添加一个 ResourceHandler ...

    public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/resources/**")
            .addResourceLocations("/resources/");
        }
    

    对于 url,你可以使用 spring url 标签:

    <%@ taglib uri="http://www.springframework.org/tags" prefix="spring"  %>
    <html>
    <link rel="stylesheet" href="<spring:url value="/resources/css/mystyle.css"  htmlEscape="true"/>" type="text/css" />
    
    <a href="<spring:url value="/home"htmlEscape="true"/>" >Home</a>
    // ....
    </html>
    

    用于重定向您可以使用“redirect:”前缀:

    return "redirect:/redirectPath";
    

    网上有很多例子,你可以通过一点点搜索来使用它们......

    【讨论】:

    • 对于您的回答,我已经在添加资源,接下来我使用的是 html(不是 jsp)。 不会在那里工作。对于重定向,我不想陷入处理重定向的循环。
    猜你喜欢
    • 2015-08-11
    • 2011-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-22
    • 2011-08-01
    • 2018-09-26
    • 2014-06-21
    相关资源
    最近更新 更多