简体   繁体   English

如何在Spring MVC中利用url?

[英]How to harness url in Spring MVC?

In the web app (developing on eclipse) i want user to harness url in browser. 在Web应用程序(在Eclipse中开发)中,我希望用户利用浏览器中的url。 Web app is based on java spring mvc, controller returns html pages. Web应用程序基于Java Spring MVC,控制器返回html页面。 All html pages are in WebContent/WEB-INF/views folder. 所有html页面都位于WebContent / WEB-INF / views文件夹中。 All css\\javacript\\images are in WebContent/resources/{css\\javacript\\images} folder. 所有css \\ javacript \\ images都位于WebContent / resources / {css \\ javacript \\ images}文件夹中。

Following are the urls this web app should access 以下是此网络应用应访问的网址

  1. localhost:8080/Project/home - for home.html 本地主机:8080 / Project / home-用于home.html
  2. localhost:8080/Project/about - for about.html 本地主机:8080 /项目/关于-关于about.html
  3. localhost:8080/Project/vendor - for vendor.html (On click all vendor details list will be displayed) localhost:8080 / Project / vendor-对于vendor.html(单击后将显示所有供应商详细信息列表)

Now i want to implement category filter for vendor 现在我想为供应商实施类别过滤器

  1. localhost:8080/Project/vendor/med - for vendor.html (reuse page with js to dispaly only medical vendor detail list) localhost:8080 / Project / vendor / med-用于vendor.html(带有js的重用页面仅显示医疗供应商详细信息列表)
  2. localhost:8080/Project/vendor/army - for vendor.html (reuse page with js to dispaly only army vendor detail list) localhost:8080 / Project / vendor / army-用于vendor.html(与js一起使用的页面仅显示军队供应商详细信息列表)
  3. localhost:8080/Project/vendor/other - for vendor.html (reuse page with js to dispaly only other vendor detail list) localhost:8080 / Project / vendor / other-用于vendor.html(带有js的重用页面仅显示其他供应商详细信息列表)

further on vendor.html (may it be {all, med, army, other} vendor) click on name link and have url as 进一步在vendor.html(可能是{all,med,军队,其他}供应商)上,单击“名称”链接,并将网址设为

localhost:8080/Project/vendor/med/vendor_XX to diplay complete info of selected vendor_XX -(coded in vendor_XX.html) localhost:8080 / Project / vendor / med / vendor_XX来显示所选vendor_XX的完整信息-(编码在vendor_XX.html中)

All the submit are GET type 所有提交均为GET类型

home/about/vendor_XX.html 主页/关于/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 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>

My Controller 我的控制器

@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 folder is added to build path of project 添加资源文件夹以创建项目的路径

localhost:8080/Project/vendor/med/vendor_XX Consider above url as localhost:8080/Project/level_1/level_2/level_3 localhost:8080 / Project / vendor / med / vendor_XX在上述网址上将其视为localhost:8080 / Project / level_1 / level_2 / level_3

Issue 1) - css is not found for all url except level_1. 问题1)-未找到除level_1以外的所有URL的CSS。 level_2 url need css import as <link rel="stylesheet" href="../resources/css/mystyle.css" type="text/css" /> level_3 url need css import as <link rel="stylesheet" href="../../resources/css/mystyle.css" type="text/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" />

Question 1 - why dont spring load css from resources. 问题1-为什么不从资源中加载CSS。 Am i missing something ? 我想念什么吗?

2) - in case i click on 2)-万一我点击

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

from level_1/level_2 vendor.html, it is directed to level_1/home. 从level_1 / level_2 vendor.html,它被定向到level_1 / home。 Thus is not found in controller request mappping. 因此在控制器请求映射中找不到。

Question 2 - how can we redirect to localhost:8080/Project/home ? 问题2-我们如何重定向到localhost:8080 / Project / home?

URLs work pretty much like paths in the command line. URL的工作方式非常类似于命令行中的路径。

If you're in directory /foo/bar/ and execute command less file.txt , you will open the file /foo/bar/file.txt , because you're using a relative path. 如果您位于目录/foo/bar/并执行命令less file.txt ,则将打开文件/foo/bar/file.txt ,因为您使用的是相对路径。

If you want to open the file /file.txt , you need either less ../../file.txt to go up two directories, or simply less /file.txt to start at the root. 如果要打开文件/file.txt ,则需要less ../../file.txt来进入两个目录,或者只需less /file.txt即可从根目录开始。

When you're on a page whose URL (ie what you see in the location bar of the browser) is http://localhost/Project/vendor/med/vendor_xx , and you load the file at resources/css/mystyle.css , then the browser will load it from http://localhost/Project/vendor/med/resources/css/mystyle.css , because you use a relative file, and the current "directory" is http://localhost/Project/vendor/med/ . 当您访问其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/

To load it from the right URL, ie http://localhost/Project/resources/css/mystyle.css , you need to use either ../../resources/css/mystyle.css to go up two directories, or you need an absolute path to start at the root: /Project/resources/css/mystyle.css . 要从正确的URL(即http://localhost/Project/resources/css/mystyle.css加载它,您需要使用../../resources/css/mystyle.css进入两个目录,或者您需要从根目录开始的绝对路径: /Project/resources/css/mystyle.css

To avoid hard-coding the context path of the project (ie /Project ), you can use the JSTL's c:url tag, or simply 为了避免对项目的上下文路径(即/Project )进行硬编码,可以使用JSTL的c:url标记,或者简单地

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

I stronly advise to almost always use absolute paths like the above, which work from whenever "directory" you are. 我强烈建议几乎总是使用上述绝对路径,无论您在何处,都可以使用绝对路径。

to access the resources you need to add a ResourceHandler in configuration class ... 访问资源,您需要在配置类中添加ResourceHandler ...

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

for url you can use spring url tag : 对于网址,您可以使用spring网址标签:

<%@ 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>

for redirect You can use the "redirect:" prefix : 用于重定向您可以使用“ redirect:”前缀:

return "redirect:/redirectPath";

There are many examples on the web that you can use them by a little bit searching ... 网上有许多示例,您只需稍作搜索即可使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM