JSPタグとEL式


JSPタグ
jsp tagタグの外観はhtmlやxmlなどのタグ言語に似ているが、内部は実はパッケージされたJavaコードである.jspタグを使用すると、jspに記述するJavaコードを簡略化できます.タグとはいえ、jspはサーバ上でのみ実行され、ブラウザ上で実行されません.結局、jspは結局サーブレットにすぎません.以下、一般的なjspタグをいくつか紹介します.1.リダイレクトタグ:

このラベルは、以下のコードと等価であり、内部転送です.

index.jsp :






Insert title here


    

Hello I'm index.jsp

Java :






Insert title here


    

http://localhost:8080/jsp-tag_Test/test.jsp, :

Hello I'm index.jsp

jsp :






Insert title here


    

http://localhost:8080/jsp-tag_Test/test.jsp, :

Hello I'm index.jsp

2. :

jsp , head.jsp , jsp head.jsp 。 。 :






Insert title here


    

http://localhost:8080/jsp-tag_Test/test.jsp, :

Hello I'm index.jsp

3. :

, , , jsp:include、jsp:forward、jsp:plugin , :

  : Servlet.service() for servlet [jsp] in context with path [/jsp-tag_Test] threw exception [/test.jsp (line: [9], column: [6]) The jsp:param action must not be used outside the jsp:include, jsp:forward, or jsp:params elements] with root cause
org.apache.jasper.JasperException: /test.jsp (line: [9], column: [6]) The jsp:param action must not be used outside the jsp:include, jsp:forward, or jsp:params elements

1.jsp:param jsp:include :
test.jsp :






Insert title here


    
        
    

index.jsp :






Insert title here


    

http://localhost:8080/jsp-tag_Test/test.jsp, :

lisi

2.jsp:param jsp:forward :
test.jsp :






Insert title here


    
        
    

index.jsp 。

http://localhost:8080/jsp-tag_Test/test.jsp, :

lisi

4. jsp Bean :

 

Bean request、session 、application(servletcontext)、page jsp 。

Java Bean :
Java 、 、 。 , Java , 。Java Bean 。 getXxx、setXxx、isXxx、addXxxListener、XxxEvent 。 。

jsp:useBean Bean , :
1. Student , :

public class Student {

    private String sname;

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    private String email;
    private int age;

}

2.test.jsp :






Insert title here


    
                      

http://localhost:8080/jsp-tag_Test/test.jsp, :

5. Bean :
property ,name jsp:useBean id ,param html name

6. Bean , :
property ,name jsp:useBean id


index.jsp :






Insert title here


    



test.jsp :






Insert title here


    
                          
    


http://localhost:8080/jsp-tag_Test/index.jsp, :

lisibr/>[email protected]
15

test.jsp :

br/>lisi
[email protected]
15

name Bean , , :
index.jsp :




test.jsp :






Insert title here


    
                          
    


http://localhost:8080/jsp-tag_Test/index.jsp, :

zerobr/>[email protected]
16

test.jsp :

br/>zero
[email protected]
16

Bean session :

1. session stu
2. ,
3. , , session

EL


EL(Expression Language) JSP 。 ECMAScript XPath , JSP , Jsp 。EL jsp , java , :

EL :

out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${param.sname}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));

jsp :

out.write("
\r
"); out.write("\t\r
"); out.write("\t\r
"); out.write("\t"); org.apache.jasper.runtime.JspRuntimeLibrary.introspect(_jspx_page_context.findAttribute("stu"), request); out.write("\r
"); out.write("\t\r
"); out.write("\t"); out.write(org.apache.jasper.runtime.JspRuntimeLibrary.toString((((org.zero01.test.Student)_jspx_page_context.findAttribute("stu")).getSname()))); out.write("
\r
"); out.write("\t"); out.write(org.apache.jasper.runtime.JspRuntimeLibrary.toString((((org.zero01.test.Student)_jspx_page_context.findAttribute("stu")).getEmail()))); out.write("
\r
"); out.write("\t"); out.write(org.apache.jasper.runtime.JspRuntimeLibrary.toString((((org.zero01.test.Student)_jspx_page_context.findAttribute("stu")).getAge()))); out.write("\r
");

EL :
${expression}

${name}

, :
1. pageContext name
2. request
3.request session
4.session application(servletContext)
5. , null, 。


test.jsp :






Insert title here


    

${name};

http://localhost:8080/EL-Test/test.jsp, , 。

pageContext name , , :






Insert title here



    

${name}

http://localhost:8080/EL-Test/test.jsp, :

lisi

request name , :






Insert title here



    

${name}

http://localhost:8080/EL-Test/test.jsp, :

request

, name , 。 pageContext , pageContext , :







Insert title here


    

    ${name}

http://localhost:8080/EL-Test/test.jsp, :

pageContext

, , :

${pageScope.name}
${requestScope.name}
${sessionScope.name}
${applicationScope.name}

, Student , request , el Student , :







Insert title here


    

    ${student.sname}
${student.email}
${student.age}

http://localhost:8080/EL-Test/test.jsp, :

lisibr/>[email protected]
16

, Student get , 。

Student , el :







Insert title here


    

    ${requestScope.stu.student.sname}
    
${requestScope.stu.student.email}
${requestScope.stu.student.age}

http://localhost:8080/EL-Test/test.jsp, :

lisibr/>[email protected]
16

, :
1.param:
2.paramValues:
3.header: http
4.headerValues: http
5.cookie: cookie
6.initParam: , servletContxt


index.jsp :






Insert title here


    





test.jsp :







Insert title here


        :${param.sname}
:${param.email}
:${param.age}
type:${paramValues.sname[2]}
host:${header.host}
Accept:${headerValues.Accept[0]}
cookie:${cookie.JSESSIONID.value}

http://localhost:8080/EL-Test/index.jsp, :

lisi1br/>lisi2
lisi3
[email protected]
16

test.jsp :

    :lisi1
    :[email protected]
  :16
type:lisi3
host:localhost:8080
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
cookie:E936637B285D60CA96B7CC3D258EBD50

initParam :
web.xml :




    
        name
            
    

 

test.jsp :







Insert title here


    ${initParam.name}

Tomcat , http://localhost:8080/EL-Test/test.jsp, :

el 、 、 , , :



${num / 100}
${num * 100}
${num + 100}
${num - 100}
${num % 100}
${num == 1000.05}
${num > 1000.05}
${num < 1000.05}
${num >= 1000.05}
${num <= 1000.05}
${num != 1000.05}
${num == 1000.05 && true}
${num == 1000.05 || false}
${num != 1000.05 ? 8:9}

http://localhost:8080/EL-Test/test.jsp, :

10.005
100050.0
1100.5
900.5
0.5
false
true
false
true
false
true
false
false
8

, ,( , , ) :



${num div 100}
${num * 100}
${num + 100}
${num - 100}
${num mod 100}
${num eq 1000.05}
${num gt 1000.05}
${num lt 1000.05}
${num ge 1000.05}
${num le 1000.05}
${num ne 1000.05}
${num eq 1000.05 and true}
${num eq 1000.05 or false}
${num ne 1000.05 ? 8:9}

http://localhost:8080/EL-Test/test.jsp, :

10.005
100050.0
1100.5
900.5
0.5
false
true
false
true
false
true
false
false
8

OGNL :
OGNL Object-Graph Navigation Language , , , , , , 。 。 OGNL EL , ,OGNL , , OGNL。

OGNL,maven :


    ognl
    ognl
    3.0.4

package org.zero01.test;

import java.util.Hashtable;

import ognl.Ognl;
import ognl.OgnlException;

public class TestOGUL{

    public static void main(String[] args) throws OgnlException {

        Student stu=new Student();

        //  hashtable      root,      
        Hashtable hashtable=new Hashtable();
        hashtable.put("stu", stu);

        //       
        Ognl.setValue("stu.sname", hashtable, "  ");
        //       
        System.out.println(Ognl.getValue("stu.sname", hashtable));

        //       
        System.out.println(Ognl.getValue("100*(500+90)*(500+90)", hashtable));
    }
}


34810000

, , EL 。