如何在Spring MVC中利用url?

时间:2016-08-23 21:28:19

标签: java spring-mvc url

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

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

  1. localhost:8080 / Project / home - for home.html
  2. localhost:8080 / Project / about - for about.html
  3. localhost:8080 /项目/供应商 - for vendor.html(点击所有供应商详情列表将会显示)
  4. 现在我想为供应商实施类别过滤器

    1. localhost:8080 / Project / vendor / med - for vendor.html(重用页面,js只显示医疗供应商详细信息列表)
    2. localhost:8080 /项目/供应商/军队 - 对于vendor.html(重用页面与js仅显示军队供应商详细信息列表)
    3. localhost:8080 /项目/供应商/其他 - 对于vendor.html(重用页面,其中js仅显示其他供应商详细信息列表)
    4. 进一步在vendor.html(可能是{all,med,army,other}供应商)点击名称链接并将网址设为

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

      所有提交都是GET类型

      家/约/ 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
      }
      

      将资源文件夹添加到项目的构建路径

      本地主机:8080 /项目/供应商/ MED / vendor_XX 将上面的url视为localhost:8080 / Project / level_1 / level_2 / level_3

      问题 1) - 除了level_1之外,找不到所有网址的css。 level_2 url需要css导入为<link rel="stylesheet" href="../resources/css/mystyle.css" type="text/css" /> level_3 url需要将css导入为<link rel="stylesheet" href="../../resources/css/mystyle.css" type="text/css" />

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

      2) - 如果我点击

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

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

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

2 个答案:

答案 0 :(得分:0)

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

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

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

当您在其网址(即您在浏览器的位置栏中看到的内容)为http://localhost/Project/vendor/med/vendor_xx的网页上,并且您在resources/css/mystyle.css加载文件时,浏览器将会从http://localhost/Project/vendor/med/resources/css/mystyle.css加载它,因为你使用了一个相对文件和当前的&#34;目录&#34;是http://localhost/Project/vendor/med/

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

为避免对项目的上下文路径进行硬编码(即/Project/resources/css/mystyle.css),您可以使用JSTL的c:url标记,或者只是

/Project

我强烈建议几乎总是使用如上所述的绝对路径,这些路径可以在任何时候使用&#34;目录&#34;你是。

答案 1 :(得分:0)

访问在配置类中添加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>

用于重定向您可以使用&#34;重定向:&#34;前缀:

return "redirect:/redirectPath";

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