具有空请求参数的路径的RequestMapping

时间:2018-09-06 18:53:07

标签: java spring rest

我需要为/ jsp / login和/ jsp / login?error建立一个映射

请注意,我无法使用/ jsp / login?error = someval,然后检查param是否具有该值或默认值。 =在这种情况下,不允许使用参数。

这在春天可行吗?

为澄清上述问题,我已经将代码设为:

@RequestMapping("/jsp/login")
public String login(@RequestParam(value = "error", required = false) String error, Map<String, Object> model) {
        if(error != null && !error.isEmpty()) {
            model.put("message", "wrong user id or password");
        }
        return "login";
}

因此,请求/ jsp / login和/ jsp / login?error都被映射为error = null,而required = false。如果URL是/ jsp / login之类的错误,则为error!= null?error = yes

要求:错误!=即使对于/ jsp / login也为null?错误,而没有任何参数值

3 个答案:

答案 0 :(得分:2)

是可以的

@RequestMapping (value = "/jsp/login", method = RequestMethod.GET)
public String showLoginWindow(@RequestParam(value = "error", required = false) String errorStr) throws LoginException {...}

重要的部分是required = false,因此,如果调用/jsp/login,则errorStr的值将为null。

答案 1 :(得分:0)

是-您可以有可选的请求参数。

    @RequestMapping(value = "/jsp/login", method = RequestMethod.GET)
    public @ResponseBody String handleLogin(
            HttpServletRequest req,
            @RequestParam(value="error",required = false) String error) {
        //TODO: write your code here
        return "success";
    }

答案 2 :(得分:0)

我不好!完美地将if(error!= null &&!error.isEmpty())更改为if(error!= null)。 错过了基本的。