在JSTL中,c:set操作用于初始化或设置指定作用域内的变量。
c:set
<c:set>的用法如下:
<c:set>
<c:set var =
"variable"
value =
"value"
scope =
"scope"
>
"userid"
"10"
"session"
/>
<c:out value=
"${userid}"
在这里我们把session范围内的变量userid的值设置为10。然后使用c:out操作将其打印出来。
session
userid
10
c:out
我们还可以使用 c:set设置对象的属性:
<c:set target=
"object"
property =
"property name"
"value of property"
首先定义一个Person类。
Person
package
com.jsptutorial;
public
class
Person {
private
String name;
Person() {
this
.name =
""
;
}
/**
* @return 返回名字
*/
String getName() {
return
name;
* @param name 需要设置的名字
void
setName(String name) {
.name = name;
在JSP页面中,把这个类当做JavaBean进行使用。
<%
@page
contentType=
"text/html"
pageEncoding=
"UTF-8"
import
=
"com.jsptutorial.*"
%>
@taglib
uri=
"http://java.sun.com/jsp/jstl/core"
prefix=
"c"
<html>
<head>
<title>JSTL 示例</title>
</head>
<body>
<jsp:useBean id=
"person"
"com.jsptutorial.Person"
"${person}"
property=
"name"
value=
"theITroad"
"${person.name}"
</body>
</html>
我们使用useBean操作命令初始化了一个Person对象person。然后通过c:set给对象的属性进行赋值。最后通过c:out就可以在浏览器中显示出该值。
useBean
person
c:remove
除了c:set操作之外,JSTL还提供了c:remove操作,它用于从指定的作用域中删除变量。
c:remove的语法如下:
<c:remove var =
"variable name"
原文链接:https://codingdict.com/