【问题标题】:How do I get a value of a class in a JSP using JSTL?如何使用 JSTL 在 JSP 中获取类的值?
【发布时间】:2017-11-08 14:00:57
【问题描述】:

希望这个标题有意义。我试图在 People 类中获取整数年龄的值(它是私有的,具有 getter 和 setter)。 ${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>

【问题讨论】:

    标签: java jsp jstl


    【解决方案1】:

    您需要在 findThatGuy bean 类中为 People 类设置 getter。还要确保在请求属性中将此设置为 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}"/>
    

    【讨论】:

    • 感谢您的回复。 httpRequest.setAttribute("findThatGuy", findThatGuy);去?它是在类下,在 servlet 中吗?
    • 是的,如果您使用 spring 框架,您需要在 servlet 类或控制器中填充您的 bean。推荐方式是使用spring mvc框架进行web开发。
    【解决方案2】:

    你有一个名为People的POJO类,所以你可以利用&lt;jsp:useBean&gt;来设置bean对象people的值。

    这里,是一个tutorial 来设置一个bean对象的属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-19
      • 2011-10-30
      • 2012-05-20
      • 2012-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多