栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

javaweb探索篇(三)jsp,el,jstl

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

javaweb探索篇(三)jsp,el,jstl

首先是jsp(粗略介绍)

<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %><%--
  Created by IntelliJ IDEA.
  User: wangsheng
  Date: 2022/2/11
  Time: 17:24
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


<%--声明类--%>
<%!
    private  Integer id;
    private  String name;
    private Mapmap;
%>
<%--声明静态代码块--%>
<%!
   static {
      Map map=new HashMap<>() ;
        map.put("key1","value1");
       map.put("key2","value2");
   }
%>
<%--方法--%>
<%!
    public  static int abc(){
        return 10+20;
    }
%>
<%--内部类--%>
<%!
    public class A{
        private  Integer id;
        private  String name;
        private Mapmap;
    }
%>
<%--输出脚本--%>
<%=12%>
<%=map%>
<%=request.getParameter("username")%>
<%--代码块脚本--%> <% int i=12; if(i==12){ System.out.println("小哥哥很帅"); }else{ System.out.println("小哥哥不睡觉吗?"); } %> <% String username=request.getParameter("username"); System.out.println("请求参数是"+username); %>
<%for(int j=1;j<=5;j++){ %> <% } %>
第<%=j%>行
EL表达式 
El表的式主要的作用是输出域中的对象
EL表达式的值是:${key}
<%--
  Created by IntelliJ IDEA.
  User: wangsheng
  Date: 2022/2/12
  Time: 17:52
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


<%
    pageContext.setAttribute("key1","pageCotext1");
    pageContext.setAttribute("key2","pageCotext2");
    request.setAttribute("key","request");
    session.setAttribute("key","session");
    application.setAttribute("key","aplliction");
%>
EL中的四大域对象
     pageScope=======>pageContext域
值是:${pageScope.key1}
requsetScope=======>Requset域
值是:${requestScope.key}
sessionScope=======>session域
值是:${sessionScope.key}
apllicationScope=======>ServletContext域
值是:${applicationScope.key}
JSTL
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="C" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: wangsheng
  Date: 2022/2/12
  Time: 19:57
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    jstl库


<%-- 
   作用:set标签可以在域对象中保存数据
        scope:域对象
        var:设置key
        value:设置值
   --%>
   保存之前:${requestScope.abc}
保存之后:${requestScope.abc}
<%-- 作用:判断 test:EL判断表达式 式子成立时执行类容执行 --%> 12等于12
<%request.setAttribute("height",168);%> <%-- 作用:多路判断 相当于swtich case when:表示满足条件 等同于case ohterwise:表达其他条件 等同于defalut --%> 很高 还可以 有点矮
<%-- 作用:遍历输出条件 begin:设置开始索引 end:设置结束的索 var:设置变量 items:遍历的数据源 --%>
第${i}行

<%--遍历数组--%> <%pageContext.setAttribute("arr",new String[]{"12354444","125444444","5411474444"});%> ${ites}
<%--遍历map集合--%> <% Map map=new HashMap<>(); map.put("key1","value1"); map.put("key2","value2"); map.put("key3","value3"); request.setAttribute("map",map); %> ${values.key}
练习(联合使用jsp,el,jtsl向页面输出学生信息)

student类 

package com.java.Person;

public class Student {
    private Integer id;
    private String name;
    private String password;
    private Integer age;
    private String phone;

    public Student() {
    }

    public Student(Integer id, String name, String password, Integer age, String phone) {
        this.id = id;
        this.name = name;
        this.password = password;
        this.age = age;
        this.phone = phone;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getAge() {
        return age;
    }

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

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + ''' +
                ", password='" + password + ''' +
                ", age=" + age +
                ", phone='" + phone + ''' +
                '}';
    }
}

jsp文件

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="com.java.Person.Student" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%--
  Created by IntelliJ IDEA.
  User: wangsheng
  Date: 2022/2/12
  Time: 20:47
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


遍历list集合
<%
    List student=new ArrayList<>();
    for(int i=1;i<=5;i++){
        student.add(new Student(i,"name"+i,"password"+i,18+i,"123444852"+i));
    }
    request.setAttribute("student",student);
%>

     
id name password age phone 操作
${stu.id} ${stu.name} ${stu.password} ${stu.age} ${stu.phone} 删除

结果

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/736716.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号