2012-09-12 6 views
0

나는 서블릿에 넘어졌고, 논리와 시각을 완벽하게 구분하기 때문에 스크립틀릿에 비해 서블릿을 좋아합니다. 하지만 JSP 페이지에서 인스턴스 메소드를 호출하는 데 문제가 있습니다.JSTL의 JSP 페이지에서 배열 메서드를 호출하려면 어떻게해야합니까?

나는 다음과 같은 JSP 페이지가 있습니다

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 


<!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> 
<c:forEach items="${stringarray}"> 
${stringarray} 
<br/> 
</c:forEach> 
</body> 
</html> 

그리고 다음 서블릿 :

package controller; 

import java.io.IOException; 
import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.Cookie; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

/** 
* Servlet implementation class Servlet 
*/ 
@WebServlet("/Servlet") 
public class Servlet extends HttpServlet 
{ 

    private static final long serialVersionUID = 1L; 

    /** 
    * Default constructor. 
    */ 
    public Servlet() 
    { 
     // TODO Auto-generated constructor stub 
    } 

    /** 
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 
    */ 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    { 
     String[] strarray = new String[5]; 

     strarray[0] = "zero"; 
     strarray[1] = "one"; 
     strarray[2] = "two"; 
     strarray[3] = "three"; 
     strarray[4] = "four"; 

     request.setAttribute("stringarray", strarray); 
     request.getRequestDispatcher("index.jsp").forward(request, response); 
    } 

    /** 
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 
    */ 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    { 
     // TODO Auto-generated method stub 
    } 
} 

왜 내 JSP 페이지의 도트 분리와 배열 방법을 호출 할 수 있습니다!

<c:forEach var="stringElement" items="${stringarray}"> 
    ${stringElement} 
    <br/> 
</c:forEach> 

c:forEach 태그는 ${stringarray}의 각 요소를 통해 루프하지만, 각 항목에 액세스 할 수, 당신은 변수를 정의 할 필요가 :

답변

1

나는 당신이 찾고있는 것은 다음과 같은 생각합니다. 또한 TLD docs

+0

완벽하게 작동합니다. 고맙습니다. 배열 메서드를 호출하면 어떨까요? – Birdman

+0

어떤'arrays' 메소드를 사용하고 있습니까? – beny23

+0

내 "문자열 배열"? – Birdman

관련 문제