I'm currently building a web system based on Java 7. There are various reasons why I don't use Java 8 In exchange for that reason, the problem was that Optional could not be used.
Optional is "I don't know if the argument will have a value, but I'll try to receive it!" However, in Spring MVC, it is used to get the URL parameter expressed in REST format.
For example, suppose you define the controller as follows:
SampleController.java
@Controller
@RequestMapping("page")
public class SampleController {
@RequestMapping({"", "{num}"})
public ModelAndView getMessages(@PathVariable("num") Optional<String> num) {
if (num.ifPresent()) {
//If the parameter is present
} else {
//If the parameter does not exist
}
return new ModelAndView("index");
}
}
It is an implementation that wants to process the same action method with or without parameter values.
This time, we assume the URLs http: // localhost / page / 1
and http: // localhost / page
.
When the former is requested, the character string after the second slash (** 1 **) is judged as a parameter, so ** 1 ** is entered in the argument num, and the result of ```ifPresent ()` `` to determine if there was an input is ** true **. Here, since the type variable is * \ <String > *, it is possible to get the value with num.get () as a character string type.
If the latter URL is requested, it is determined that the parameter value does not exist and no ** value is entered in the argument num **. Therefore, the judgment result of ```ifPresent () `` `is ** false **.
In Java8 or later, the above implementation is sufficient. Up to this point, it is a common method that is also introduced on external sites and within Qiita.
However, since Optional is a class newly implemented in Java8, the above program cannot be used as it is until Java7. I also thought "Is it possible to set the argument type to * String * and get it with * null *?", But I throw a ** NullPointerException **.
You can implement it by splitting the action method, but I don't really want to do that because I feel like I've lost. (I didn't want to have at least two of the same source in the same class, or implement a private method to avoid the former. If you do, I want to use the same source. )
Then, what to do is to create a class that can be used like Optional (hereinafter, ** parameter class **) and use it as an alternative.
SampleParameter.java
public class SampleParameter {
private String num;
//If you do not use lombok you need:
// pubric String getNum() { return num; }
// pubric String setNum(String num) { this.num = num; }
boolean ifPresent() {
return num != null;
}
}
SampleController.java
@Controller
@RequestMapping("page")
public class SampleController {
@RequestMapping({"", "{num}"})
public ModelAndView getMessages(@PathVariable("num") SampleParameter num) {
if (num.ifPresent()) {
//If the parameter is present
} else {
//If the parameter does not exist
}
return new ModelAndView("index");
}
}
Yes, it looks like this. As with `` `@ ModelAttribute, it looks like you're making something like a form class. In fact, if the parameter class does not have the parameter name specified by
@ RequestMapping```,
Be careful as it throws a ** NullPointerException **.
At first glance, it looks like we are reinventing the wheel, but it is also a fairly convenient class.
On the contrary, the following points should be noted when considering the implementation.
`setNum (Object) ``` method for
@ RequestMapping ({"", "{num}"}) ``
. Recommended Posts