2017-02-08 6 views
0

Java 서블릿을 사용하는 방법을 배우고 장난감 예제를 설정했습니다. c : forEach가 요청 범위에서 목록을 찾지 못하는 것 같습니다.

나는 doGet 방법에 다음과 같은 논리와 servlet이 : 그럼

 

@Override 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 

     //HttpSession session = request.getSession(); 
     List theList = new ArrayList(); 
     theList.add(1); 
     theList.add(2); 
     theList.add(3); 
     request.setAttribute("intList", theList); 

     RequestDispatcher dispatcher = request.getRequestDispatcher("hello.jsp");   
     dispatcher.forward(request, response); 
    } 
 

I가 hello.jsp에 다음 코드 :

:

<%@page contentType="text/html" pageEncoding="UTF-8"%> 

<!DOCTYPE html> 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>JSP Page</title> 
</head> 
<body> 

<h1>Redirect Worked</h1> 
<c:forEach items="${intList}" var="item"> 
    ${item}<br> 
</c:forEach> 
</body> 
</html> 

내가 내 브라우저 보여 기대

 
Redirect Worked 
1 
2 
3 

하지만 내가 보는 것은 :

 
Redirect Worked 

내가 뭘 잘못하고 있니?

+2

그것은 내가 JSP로의 사용 때문에 긴 시간을, 그러나 당신은 JSP에서 JSTL 라이브러리를 포함 할 필요가 없습니다? '<% @ taglib prefix = "c"uri = "http://java.sun.com/jstl/core"%>' – DaveH

+1

참조 http://stackoverflow.com/questions/4912690/how-to-access- at-request-attributes-in-jsp – chaim

+0

제안 사항을 모두 조합하여 문제가 해결되었습니다. – James

답변

0

어쩌면 이런 식으로 작동할까요? DaveH, theodosis 및 짜잔에

<c:forEach items="${intList}" var="item"> 
     <c:out value="${item}"/> 
    </c:forEach> 
0

덕분에 나는 내 JSP 파일 고정 :

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>JSP Page</title> 
</head> 
<body> 
    <h1>redirect worked!</h1> 
    <c:forEach var="item" items="${requestScope.intList}"> 
     <c:out value="${item}"/> 
    </c:forEach> 

</body> 
</html>