jstl标签库之

2018-05-07  本文已影响0人  bfx1000

在 web 创建 c_set.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<!-- c:set单个赋值 -->
<c:set var="people" value="张三" scope="page"></c:set>
<h2><c:out value="${people}" default="无"/></h2>
<hr/>

<!-- c:set 属性赋值 -->
<jsp:useBean id="people2" class="com.java1234.model.People" scope="page"/>
<c:set property="id" target="${people2}" value="007"></c:set>
<c:set property="name" target="${people2}" value="王二小"></c:set>
<c:set property="age" target="${people2}" value="16"></c:set>
<h2>编号:${people2.id}</h2>
<h2>姓名:${people2.name}</h2>
<h2>年龄:${people2.age}</h2>
<hr/>

<!-- java 代码赋值 -->
<%
    pageContext.setAttribute("people3","李四");
%>
<h2><c:out value="${people3}" default="王五"/></h2>
</body>
</html>

在 src下创建 com.java1234.model.People.java

package com.java1234.model;

public class People {
    private int id;
    private String name;
    private int age;

    public People() {
        super();
    }

    public People(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

结果


结论:
<c:set> 标签赋值:
1)单个赋值
2)jsp:useBean 赋值多个属性

上一篇 下一篇

猜你喜欢

热点阅读