Spring Expression Language

When using the ${} syntax, you are in fact using Spring Expression Language (SpEL). There are several variants of EL available in the wild; SpEl is one of the most powerful variants.

Here is an overview of its main features:

Feature

Syntax

Explanation

Accessing a list element

list[0]

 

Accessing a map entry

map[key]

 

Ternary operator

condition ? 'yes' : 'no'

 

Elvis operator

person ?: default

Returns default if person's value is null

Safe navigation

person?.name

Returns null if person or her name is null

Templating

'Your name is #{person.name}'

Injects values into a string

Projections

${persons.![name]}

Extracts the names of all the persons and puts them into a list

Selection

persons.?[name == 'Bob']'

Retrieves the person whose name is Bob inside a list

Function call

person.sayHello()

 

The SpEl usage is not limited to views. You can also use it in various places inside the Spring framework, for instance, when injecting properties inside beans with the @Value annotation.

Getting data with a request parameter

We are able to display data coming from the server inside the view. However, what if we wanted to get input from the user? With the HTTP protocol, there are multiple ways to do this. The simplest way is to pass a query parameter to our URL.

Note

Query parameters

You certainly know query parameters. They are found after the ? character in a URL. They consist of a list of name and values separated by the & symbol (Ampersand), for example, page?var1=value1&var2=value2.

We can leverage this technique to ask our user for their name. Let's modify our HelloController class again:

@Controller
public class HelloController {

    @RequestMapping("/")
    public String hello(@RequestParam("name") String userName, Model model) {
        model.addAttribute("message", "Hello, " + userName);
        return "resultPage";
    }
}

If we navigate to localhost:8080/?name=Geoffroy, we can see the following:

Getting data with a request parameter

By default, the request parameter is mandatory. This means that if we were to navigate to localhost:8080, we would see an error message.

Looking at the @RequestParam code, we can see that in addition to the value parameter, there are two other attributes possible: required and defaultValue.

Therefore, we can change our code and specify a default value for our parameter or indicate that it is not required:

@Controller
public class HelloController {

    @RequestMapping("/")
    public String hello(@RequestParam(defaultValue = "world") String name, Model model) {
        model.addAttribute("message", "Hello, " + name);
        return "resultPage";
    }
}

Tip

In Java 8, it is possible not to specify the value parameter. In that case, the name of the annotated method parameter will be used.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.2.240