簡體   English   中英

如何使用JSTL在JSP中獲取類的值?

[英]How do I get a value of a class in a JSP using JSTL?

希望這個標題有意義。 我正在嘗試在People類中獲取整數age的值(這是私有的,具有getter和setters)。 $ {People.age}沒有帶回任何東西。 有什么我想念的嗎? 謝謝。

帶有JSTL的JSP:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
    <c:set var="ages" value="${People.age}"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>

<body>
<p style="color:black;font-size:100px">this persin is ${findThatGuy.People.age}</p>
<c:out value="${findThatGuy.People.age}"/>
</body>
</html>

您需要在findThatGuy bean類中具有People類的getter setter方法。 還要確保在請求屬性中將此設置為setAttribute。

public Class findThatGuy{

    private People people;

    public People getPeople(){

        return people;
    }

    public void setPeople(People people){

        this.people = people;
    }
}

httpRequest.setAttribute("findThatGuy", findThatGuy);

In Jsp use
<c:out value="${findThatGuy.people.age}"/>

您有一個名為People的POJO類,因此可以使用<jsp:useBean>設置bean對象people的值。

這里是一個設置bean對象屬性的教程

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM