如何在 Spring MVC 中利用 url? [英] How to harness url in Spring MVC?
问题描述
在网络应用程序(在 eclipse 上开发)中,我希望用户利用浏览器中的 url.Web 应用程序基于 java spring mvc,控制器返回 html 页面.所有 html 页面都在 WebContent/WEB-INF/views 文件夹中.所有 css\javacript\images 都在 WebContent/resources/{css\javacript\images} 文件夹中.
以下是此网络应用应访问的网址
- localhost:8080/Project/home - 用于 home.html
- localhost:8080/Project/about - for about.html
- localhost:8080/Project/vendor - 用于 vendor.html(点击将显示所有供应商详细信息列表)
现在我想为供应商实现类别过滤器
- localhost:8080/Project/vendor/med - 用于 vendor.html(使用 js 重用页面以仅显示医疗供应商详细信息列表)
- localhost:8080/Project/vendor/army - 用于 vendor.html(使用 js 重用页面以仅显示军队供应商详细信息列表)
- 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
<link rel="stylesheet" href="resources/css/mystyle.css" type="text/css"/><a href="home">首页</a><a href="vendor">Vendor</a><a href="about">关于</a><a href="vendor/med">医疗</a><a href="vendor/army">Army</a><a href="vendor/other">其他</a>//和其他不相关的东西</html>
供应商.html
<link rel="stylesheet" href="resources/css/mystyle.css" type="text/css"/><a href="home">首页</a><a href="vendor">Vendor</a><a href="about">关于</a><a href="vendor/med">医疗</a><a href="vendor/army">Army</a><a href="vendor/other">其他</a>//用js动态生成3行以下<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>//和其他不相关的东西</html>
我的控制器
@Controller公共类 AppController {@RequestMapping(value = "home", method = RequestMethod.GET)公共字符串家(){回家";}@RequestMapping(value = "vendor", method = RequestMethod.GET)公共字符串供应商(){返回供应商";}@RequestMapping(value = "vendor/med", method = RequestMethod.GET)公共字符串 vendorMed() {返回供应商";}@RequestMapping(value = "vendor/army", method = RequestMethod.GET)公共字符串 vendorArmy() {返回供应商";}@RequestMapping(value = "vendor/med/vendor_xx", method = RequestMethod.GET)公共字符串 vendorMedXX() {返回vendor_xx";}//所有示例网址都已给出}
资源文件夹被添加到项目的构建路径
本地主机: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 导入为 <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) - 如果我点击
Home
从 level_1/level_2 vendor.html,它被定向到 level_1/home.因此在控制器请求映射中没有找到.
问题 2 - 我们如何重定向到 localhost:8080/Project/home ?
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"
我强烈建议几乎总是使用像上面那样的绝对路径,只要你是目录",它就可以工作.
In the web app (developing on eclipse) i want user to harness url in browser. Web app is based on java spring mvc, controller returns html pages. All html pages are in WebContent/WEB-INF/views folder. All css\javacript\images are in WebContent/resources/{css\javacript\images} folder.
Following are the urls this web app should access
- localhost:8080/Project/home - for home.html
- localhost:8080/Project/about - for about.html
- localhost:8080/Project/vendor - for vendor.html (On click all vendor details list will be displayed)
Now i want to implement category filter for vendor
- localhost:8080/Project/vendor/med - for vendor.html (reuse page with js to dispaly only medical vendor detail list)
- localhost:8080/Project/vendor/army - for vendor.html (reuse page with js to dispaly only army vendor detail list)
- localhost:8080/Project/vendor/other - for vendor.html (reuse page with js to dispaly only other vendor detail list)
further on vendor.html (may it be {all, med, army, other} vendor) click on name link and have url as
localhost:8080/Project/vendor/med/vendor_XX to diplay complete info of selected vendor_XX -(coded in vendor_XX.html)
All the submit are GET type
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>
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
Issue
1) - css is not found for all url except level_1.
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" />
Question 1 - why dont spring load css from resources. Am i missing something ?
2) - in case i click on
<a href="home">Home</a>
from level_1/level_2 vendor.html, it is directed to level_1/home. Thus is not found in controller request mappping.
Question 2 - how can we redirect to localhost:8080/Project/home ?
URLs work pretty much like paths in the command line.
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.
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.
When you're on a page whose URL (i.e. 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/
.
To load it from the right URL, i.e. 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
.
To avoid hard-coding the context path of the project (i.e. /Project
), you can use the JSTL's c:url tag, or simply
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.
这篇关于如何在 Spring MVC 中利用 url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!