Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC: How to test whether param exists when there's no value?

I want to display an error message with my custom login.jsp form. When there's an error, the url is ../loginForm?error without any value assigned to error. (This seems to be the behavior of Spring Security.) If there's no error, the url is simply ../loginForm (without the parameter). In the controller I can capture the parameter with @RequestParam, but how do I check whether or not error is passed? In other words, how can I test a parameter alone without a value?

Here's the controller code I have now:

@RequestMapping("/loginForm")
public String showLoginForm(@RequestParam(value="error", defaultValue="false") 
                                    boolean error,
                                Model model)
{
    if (error == true) 
    {
        model.addAttribute("loginError", "Invalid username and password.");
    }

    return "/user/loginForm";
}

...and here's the JSP snippet:

 <c:if test="${not empty loginError}">
  <tr>
   <td><c:out value="${loginError}" /></td>
  </tr>
 </c:if>

At this point I'm not including the Security configuration I have set up, since everything else seems to be working and I want to keep this focused on the issue at hand.

Thanks in advance for any suggestions!

like image 293
Craig Davis Avatar asked Oct 30 '15 20:10

Craig Davis


People also ask

What is required false in RequestParam?

Optional or Required Request Parameters In this case, if the request parameter is not included in the URL then its value will default to the value provided by the defaultValue. Or you can use the required=false with the @RequestParam annotation. Note that required=false works well only with String datatypes.

Can request param be empty?

The @RequestParam annotation supports the following parameters: defaultValue: Default value as a fallback mechanism when the request doesn't have a value or is empty.

How do you specify the default value at the rate request param annotation?

The default value of the @RequestParam is used to provide a default value when the request param is not provided or is empty. In this code, if the person request param is empty in a request, the getName() handler method will receive the default value John as its parameter.


Video Answer


2 Answers

Ok, I figured it out (while taking a break). The @RequestParam only works when there's actually a parameter available for mapping. If no such parameter is passed in, it's useless. So instead, I checked the Map provided by ServletRequest:

@RequestMapping("/loginForm")
public String showLoginForm(ServletRequest request, Model model)
{
    Map<String, String[]> paramMap = request.getParameterMap();

    if (paramMap.containsKey("error")) 
    { 
        model.addAttribute("loginError", "Invalid username and password.");
    }

    return "/user/loginForm";
}

It works fine now.

like image 75
Craig Davis Avatar answered Nov 30 '22 13:11

Craig Davis


There is another way to do that. Just create one more method where @RequestMapping will check presence of "error" parameter, add required attribute and return view. Both methods could exist together.

@RequestMapping(value = "/loginForm", params = {"error"})
public String loginError(Model model)
{
    model.addAttribute("loginError", "Invalid username and password.");
    return "/user/loginForm";
}

@RequestMapping(value = "/loginForm")
public String login()
{
    return "/user/loginForm";
}
like image 42
Maksym Iasynetskyi Avatar answered Nov 30 '22 11:11

Maksym Iasynetskyi