问题 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 accesslocalhost:8080/Project/home - for home.htmllocalhost:8080/Project/about - for about.htmllocalhost:8080/Project/vendor - for vendor.html (On click all vendor details list will be displayed)Now i want to implement category filter for vendorlocalhost: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 aslocalhost:8080/Project/vendor/med/vendor_XX to diplay complete info of selected vendor_XX -(coded in vendor_XX.html)All the submit are GET typehome/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@Controllerpublic 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 projectlocalhost:8080/Project/vendor/med/vendor_XXConsider above url as localhost:8080/Project/level_1/level_2/level_3Issue1) - 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.cssto 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 simplyhref="${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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-11 04:50