2017-11-08 8 views
0

그 제목이 의미가 있기를 바랍니다. 저는 People 클래스에서 정수 나이 (개인 클래스이고 getter와 setter를 가짐)의 값을 얻으려고합니다. $ {People.age}이 (가) 다시는 아무것도 가져 오지 않습니다. 내가 빠진 게 있니? 고맙습니다. JSTL과JSTL을 사용하여 JSP에서 클래스의 값을 얻으려면 어떻게해야합니까?

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> 

답변

0

당신은 findThatGuy 콩 클래스에있는 사람들 클래스의 게터 세터가 있어야합니다. 또한 요청 속성에 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}"/> 
+0

답장을 보내 주셔서 감사합니다. httpRequest.setAttribute ("findThatGuy", findThatGuy); 가기? 클래스 아래, 서블릿에 있습니까? –

+0

예 스프링 프레임 워크를 사용하는 경우 Servlet 클래스 또는 컨트롤러에 Bean을 채워야합니다. 권장 방법은 웹 개발을위한 스프링 MVC 프레임 워크를 사용하는 것입니다. – Rajesh

0

당신은 People라는 이름의 POJO 클래스를 가지고, 그래서 당신은 빈 객체 people의 값을 설정하는 <jsp:useBean>를 사용할 수있다.

여기서 빈 개체의 속성을 설정하려면 tutorial입니다.