Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 如何处理不在变量名中的URL参数_Spring_Spring Mvc - Fatal编程技术网

Spring 如何处理不在变量名中的URL参数

Spring 如何处理不在变量名中的URL参数,spring,spring-mvc,Spring,Spring Mvc,我正在开发一个SpringWeb应用程序,该应用程序应该具有要传递到页面中的URL参数。 像 但当我看到其他网站所采用的方法时。看起来像是 xyz.com/listing/Hotels xyz.com/listing-details/335 如何调整spring控制器以从URL中提取这些参数 目前,对于xyz.com/listing?type='Hotels',我的控制器方法如下所示 public ModelAndView getContactList(@RequestParam(value

我正在开发一个SpringWeb应用程序,该应用程序应该具有要传递到页面中的URL参数。 像

但当我看到其他网站所采用的方法时。看起来像是

xyz.com/listing/Hotels
xyz.com/listing-details/335
如何调整spring控制器以从URL中提取这些参数

目前,对于xyz.com/listing?type='Hotels',我的控制器方法如下所示

public ModelAndView getContactList(@RequestParam(value = "type", defaultValue =       "Hotels") String type, HttpServletRequest request) {
  Some Code
}

另外,如果请求参数的格式不正确,并且找不到与之相关的任何结果,如何显示404页面。

查看Spring的@PathVariable。一个相关的教程是

从上面的教程中,您可以看到您可以编写如下代码(您可以看到SpringMVC的灵活性)


控制器不仅限于使用@PathVariable。它可以使用@PathVariable和@RequestVariable的组合来满足特定的需要。

查看Spring的@PathVariable。一个相关的教程是

从上面的教程中,您可以看到您可以编写如下代码(您可以看到SpringMVC的灵活性)


控制器不仅限于使用@PathVariable。它可以使用@PathVariable和@RequestVariable的组合来满足特定的需要。

使用
@PathVariable
而不是
@RequestParam

假设您有酒店
1
-
500
,如果该数字小于
500
,您希望返回结果。如果酒店编号超过
500
,则您希望返回
未找到资源404

有效URL:

xyz.com/hotels
xyz.com/hotels/335
无效的URL:

xyz.com/hotels/501
将控制器定义如下:

@Controller
@RequestMapping("/hotels")
public class HotelController {

    @RequestMapping(method=RequestMethod.GET)
    public ModelAndView getHotels(){
        System.out.println("Return List of Hotels");
        ModelAndView model = new ModelAndView("hotel");
        ArrayList<Hotel> hotels = null;
        // Fetch All Hotels
        model.addObject("hotels", hotels);
        return model;
    }

    @RequestMapping(value = "/{hotelId}", method=RequestMethod.GET)
    public ModelAndView getHotels(@PathVariable("hotelId") Integer hotelId){
        if (hotelId > 500){
            throw new ResourceNotFoundException();
        }
        ModelAndView model = new ModelAndView("hotel");
        Hotel hotel = null;
        // get Hotel
        hotel = new Hotel(hotelId, "test Hotel"+hotelId);
        model.addObject("hotel", hotel);

        return model;
    }
   }
请求方法是
GET

如果URL包含id信息,如
xyz.com/hotels/2
,则将调用带有
@PathVariable
getHotels()

现在,如果要在酒店id大于
500
时返回
404
,请抛出自定义
异常。请注意,
ResponseStatus.NOT_FOUND
在下面的自定义异常处理程序方法中进行了注释:

@ResponseStatus(value = HttpStatus.NOT_FOUND)
class ResourceNotFoundException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    public ResourceNotFoundException(){
        super();
    }
}

使用
@PathVariable
代替
@RequestParam

假设您有酒店
1
-
500
,如果该数字小于
500
,您希望返回结果。如果酒店编号超过
500
,则您希望返回
未找到资源404

有效URL:

xyz.com/hotels
xyz.com/hotels/335
无效的URL:

xyz.com/hotels/501
将控制器定义如下:

@Controller
@RequestMapping("/hotels")
public class HotelController {

    @RequestMapping(method=RequestMethod.GET)
    public ModelAndView getHotels(){
        System.out.println("Return List of Hotels");
        ModelAndView model = new ModelAndView("hotel");
        ArrayList<Hotel> hotels = null;
        // Fetch All Hotels
        model.addObject("hotels", hotels);
        return model;
    }

    @RequestMapping(value = "/{hotelId}", method=RequestMethod.GET)
    public ModelAndView getHotels(@PathVariable("hotelId") Integer hotelId){
        if (hotelId > 500){
            throw new ResourceNotFoundException();
        }
        ModelAndView model = new ModelAndView("hotel");
        Hotel hotel = null;
        // get Hotel
        hotel = new Hotel(hotelId, "test Hotel"+hotelId);
        model.addObject("hotel", hotel);

        return model;
    }
   }
请求方法是
GET

如果URL包含id信息,如
xyz.com/hotels/2
,则将调用带有
@PathVariable
getHotels()

现在,如果要在酒店id大于
500
时返回
404
,请抛出自定义
异常。请注意,
ResponseStatus.NOT_FOUND
在下面的自定义异常处理程序方法中进行了注释:

@ResponseStatus(value = HttpStatus.NOT_FOUND)
class ResourceNotFoundException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    public ResourceNotFoundException(){
        super();
    }
}

@PathParam
将对您有所帮助。要仅在用户提供特定GET参数时进行匹配,您可以执行以下操作:
@RequestMapping(params=“type=Hotel”)
。我相信它也可以使用正则表达式,并接受单个字符串参数或它们的数组。
@PathParam
将帮助您。要仅在用户提供特定GET参数时进行匹配,您可以执行以下操作:
@RequestMapping(params=“type=Hotel”)
。我相信它也可以使用正则表达式,并接受单个字符串参数或它们的数组。谢谢您的回答。谢谢您的回答。在上述方法中写入的HttpStatus.NOT_可以导入为org.springframework.http.HttpStatus.NOT_FOUNDHttpStatus.NOT_FOUND在上述方法中写入的可以导入为org.springframework.http.HttpStatus.NOT_FOUND