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 commandless 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 eitherless ../../file.txt
to go up two directories, or simplyless /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 atresources/css/mystyle.css
, then the browser will load it fromhttp://localhost/Project/vendor/med/resources/css/mystyle.css
, because you use a relative file, and the current "directory" ishttp://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 simplyI 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 ...
for url you can use spring url tag :
for redirect You can use the "redirect:" prefix :
There are many examples on the web that you can use them by a little bit searching ...