The use of Java EL expressions 01

Use of Java EL expressions

Use El expressions to echo data

-Why use el expression, first look at the previous script output

<body>
    <form action="el.jsp">
        username:<input type="text" name="name" value="<%=request.getParameter("name") %>" /><br>
        <input type="submit" value="提交">
    </form>

    <br>
    <%

    out.print(request.getParameter("name"));
    %>
</body>

Effect:

write picture description here

Here we find that when we use a small script to write back data, the form will display null data by default

When we enter some values ​​and submit, it will be displayed normally
write picture description here

From here, we will find that the use of small scripts is a bit inappropriate.


And we can give an example of session value

<br>
    <br>
    <%
    Customer customer = new Customer();
    customer.username="zhangsan";
        session.setAttribute("user", customer);


        out.print(((Customer)session.getAttribute("user")).getUsername());
    %>

Although the effect is good, it is very troublesome to write small scripts

write picture description here

- Use El expressions to complete the above functions

<body>
    <form action="el.jsp">
        username:<input type="text" name="name" value="${param.name }" /><br>
        <input type="submit" value="提交">
    </form>

    <br>
    获取参数中的name值:<br>
    ${param.name}

    <br>
    <br>
    <%
    Customer customer = new Customer();
    customer.username="zhangsan";
        session.setAttribute("user", customer);

    %>
    获取session域当中的值:<br>
    ${sessionScope.user.username}




</body>

Effect:

write picture description here

Method of parenthesis operator in el expression

${sessionScope["com.xinhua.user"].username}

Example:

<br>
    <%
    Customer customer = new Customer();
    customer.username="zhangsan";
        session.setAttribute("com.xinhua.user", customer);

    %>
    获取session域当中的值:<br>
    ${sessionScope["com.xinhua.user"].username}

The effect is the same as above...

  • El can perform automatic type conversion
    <form action="el.jsp">
        username:<input type="text" name="name" value="${param.name }" /><br>
        <input type="submit" value="提交">
    </form>

    <br>
    获取参数中的name值:<br>
    ${param.name+10}

    <br>
    <br>

Effect:
write picture description here

  • El hidden object

When we don't write the scope, the default el expression will look for the value in the smallest scope, and if it is found, it will be displayed. If it is not found, it will continue to look for a larger scope.

E.g:

<br>
    <%
    Customer customer = new Customer();
    customer.username="zhangsan";
        session.setAttribute("user", customer);

    %>
    获取session域当中的值:<br>
    ${user.username}
  1. Here ${user.username} will first go to the page scope to find the user
  2. Then go to the request to find the user attribute
  3. If you can't find it, go to the session to find the user. At this time, if the user is found, its attribute value will be displayed.
  4. If none of the above is found, continue to find whether there is a user in the application

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325337729&siteId=291194637