How to multiple Spring Url and annotation: @PathVariable null .
+Multiple Spring Url :
@RequestMapping(value= {"","/{slug}/{slug1}","/{slug}"})
trong giá trị của value thì bạn có thể dùng 1 array.
+Làm sao để set giá trị @PathVariable là null.
Cách 1:
--Trong value nếu như bạn không lấy giá trị của path bạn có thể config trong -servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<mvc:annotation-driven />
Như vậy là bạn có thể dùng : @PathVariable(value="slug1" ,required=false )
Cách 2:
@PathVariable Map<String,String> path
Bạn có thể dùng để lấy các PathVariable
String newPathVariable = "";
String newPathVariable1 = "";
newPathVariable = path.get("slug");
newPathVariable1= path.get("slug1");
// bạn có thể check pathVariable có tồn tại hay không bằng cách name.containsKey(key)
++Method: containsKey(key):
boolean containsKey(Object key)
Returns true if this map contains a mapping for the specified key. More formally, returns true if and only if this map contains a mapping for a key k such that (key==null ? k==null : key.equals(k)). (There can be at most one such mapping.)
Parameters:
key - key whose presence in this map is to be tested
Returns:
true if this map contains a mapping for the specified key
Throws:
ClassCastException - if the key is of an inappropriate type for this map (optional)
NullPointerException - if the specified key is null and this map does not permit null keys (optional)
Cách 3:
@RequestMapping(value = {"/simple", "/simple/{game}"}, method = RequestMethod.GET)
public ModelAndView gameHandler(@PathVariable Optional<String> game,
HttpServletRequest request) {
if (game.isPresent()) {
//game.get()
//corresponds to path "/simple/{game}"
} else {
//corresponds to path "/simple"
}
}