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

时间:2014-04-02 12:54:08

标签: spring spring-mvc

我正在开发一个应该将URL参数传递到页面的spring web应用程序。 喜欢

xyz.com/listing?type='Hotels'
xyz.com/listing-details?id=53

但是当我看到其他网站所遵循的方法时。它看起来像是

xyz.com/listing/Hotels
xyz.com/listing-details/335

如何调整弹簧控制器以从URL中获取这些参数。

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

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

当请求参数格式不正确且我没有找到与之相关的任何结果时,如何显示404页面。

2 个答案:

答案 0 :(得分:2)

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

从上面的教程中你可以看到你可以编写如下代码(你可以看到Spring MVC的灵活性)

@Controller
public class TestController {
     @RequestMapping(value="/user/{userId}/roles/{roleId}",method = RequestMethod.GET)
     public String getLogin(@PathVariable("userId") String userId,
         @PathVariable("roleId") String roleId){
         System.out.println("User Id : " + userId);
         System.out.println("Role Id : " + roleId);
         return "hello";
     }
     @RequestMapping(value="/product/{productId}",method = RequestMethod.GET)
     public String getProduct(@PathVariable("productId") String productId){
           System.out.println("Product Id : " + productId);
           return "hello";
     }
     @RequestMapping(value="/javabeat/{regexp1:[a-z-]+}",
           method = RequestMethod.GET)
     public String getRegExp(@PathVariable("regexp1") String regexp1){
           System.out.println("URI Part 1 : " + regexp1);
           return "hello";
     }
}

Controller不仅限于使用@PathVariable。它可以使用符合特定需求的@PathVariable和@RequestVariable的组合。

答案 1 :(得分:2)

使用@PathVariable代替@RequestParam

假设您有酒店1 - 500,并且如果该数字小于500,您希望返回结果。如果酒店号码超过500,那么您想要返回Resource Not Found 404

有效网址:

xyz.com/hotels
xyz.com/hotels/335

网址无效:

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;
    }
   }

注意@RequestMapping/hotels的形式提供,这样当URL为

时会调用getHotels()方法
xyz.com/hotels

,请求方法为GET

如果网址包含xyz.com/hotels/2等ID信息,则会调用getHotels()@PathVariable

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

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

    private static final long serialVersionUID = 1L;

    public ResourceNotFoundException(){
        super();
    }
}