Java Web Learning Note-expression language

Source: Internet
Author: User
Tags java web

1. An Introduction to expression language

The main purpose of this paper is to simplify the code of JSP in MVC, and to facilitate the output of attributes. You can also avoid the judgment that the property is empty, and the expression defaults to null set to "".

2. Built-in object for expression language

A. About the property's Get ${property name}, for the four attribute ranges of page->request->session->application, if you set a property with the same name at the same time, only the property value with the smallest attribute range will be displayed.

In general, property names should be different for each attribute scope, so they can be called directly with the ${property name}.

B. About parameter acquisition ${param. Parameter name} or ${param if a multi-box parameter is required. property name [0]}

C. Get other built-in objects by PageContext built-in objects

${PAGECONTEXT.REQUEST.REMOTEADDR}, ${pagecontext.session.id}, ${pagecontext.session.new}

3. Expression language-operation of the collection

A. For a collection object of list type, you can call directly ${the object name [subscript number]}

B. For a collection object of the map type, it can be called with the ${object name [key Name]}

Example

============================================

A.

<%
List addr = new ArrayList ();
Addr.add ("Germany");
Addr.add ("The United Kingdom");
Addr.add ("France");
Request.setattribute ("info", addr);
%>

B.

<%
Map map = new HashMap ();
Map.put ("Notebook", "The Light of the night 17 inches");
Map.put ("iphone", "Iphonex");
Map.put ("Kindle", "Kindle");
Request.setattribute ("info", map);
%>

4. Applying an expression language in MVC

A. Define VO, servlet, generate Vo object in servlet set Vo property of Object

The most important point is to set the Vo object to an attribute info through the request parameter of the doget.

This way, in the subsequent JSP, you can access the ${info. Member variable}.

B. For cases where the servlet generates a Vo object collection, or if the Vo object collection all is set to property info in doget with the request parameter

The JSP needs to get to the list collection object all by Request.getattribute ("info") first.

The list collection object is then iterated through iterator. Iterator ITR = All.iterator ();

In the while (Itr.hasnext ()) {} loop, you need to pass Pagecontext.setattribure ("dept", Itr.next ()) first;

It is then accessed through the ${dept. Member variable}.

=====================================================================================================a

Servlet

public class Servletpeople extends httpservlet{

private people pl = null;
List<people> all = null;
protected void doget (HttpServletRequest req,httpservletresponse resp) throws servletexception,ioexception{


all = new arraylist<people> ();
PL = new people ();
Pl.setname ("Li Yongxheng");
Pl.setsex ("male");
Pl.setsalary (60000);
All.add (PL);

PL = new people ();
Pl.setname ("Li Yuyan");
Pl.setsex ("male");
Pl.setsalary (80000);
All.add (PL);

Req.setattribute ("info", all);

Req.getrequestdispatcher ("/people/people.jsp"). Forward (REQ,RESP);

}

Jsp

。。。

<%
List all = (list) request.getattribute ("info");
Iterator ITR = All.iterator ();
while (Itr.hasnext ()) {
Pagecontext.setattribute ("dept", Itr.next ());
%>


<%}%>

。。。

Java Web Learning Note-expression language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.