简体   繁体   English

如何使用JSTL在JSP中获取类的值?

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

Hopefully that title made sense. 希望这个标题有意义。 I'm trying to get the value of the integer age (which is private and has getters and setters) in the class People. 我正在尝试在People类中获取整数age的值(这是私有的,具有getter和setters)。 ${People.age} isn't bringing anything back. $ {People.age}没有带回任何东西。 Is there something i'm missing out on? 有什么我想念的吗? Thank you. 谢谢。

JSP with JSTL: 带有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>

You need to have getter setter for People class in findThatGuy bean Class. 您需要在findThatGuy bean类中具有People类的getter setter方法。 Also make sure you set this in request attribute as setAttribute. 还要确保在请求属性中将此设置为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}"/>

You have a POJO class named People , So you can make use of <jsp:useBean> to set the value of bean object people . 您有一个名为People的POJO类,因此可以使用<jsp:useBean>设置bean对象people的值。

Here, is a tutorial to set property of a bean object. 这里是一个设置bean对象属性的教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM