How to harness url in Spring MVC?

2019-09-18 21:15发布

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

  1. localhost:8080/Project/home - for home.html
  2. localhost:8080/Project/about - for about.html
  3. 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

  1. localhost:8080/Project/vendor/med - for vendor.html (reuse page with js to dispaly only medical vendor detail list)
  2. localhost:8080/Project/vendor/army - for vendor.html (reuse page with js to dispaly only army vendor detail list)
  3. 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 ?

2条回答
何必那么认真
2楼-- · 2019-09-18 21:47

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 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.

查看更多
太酷不给撩
3楼-- · 2019-09-18 21:57

to access the resources you need to add a ResourceHandler in configuration class ...

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

for url you can use spring url tag :

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

return "redirect:/redirectPath";

There are many examples on the web that you can use them by a little bit searching ...

查看更多
登录 后发表回答