2010-02-07 57 views
2

嗨我所面臨的問題,同時使用forEach迭代我給完整的代碼,請告訴我這是什麼錯誤。forEach標籤有問題

的index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!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> 
    <form method="get" action="go"> 
    <center><input type="SUBMIT"></center> 
</body> 
</html> 

的web.xml
<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 
    <display-name>FirstJsp</display-name> 
    <context-param> 
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name> 
    <param-value>client</param-value> 
    <!-- 
     <description> 
     State saving method: "client" or "server" (= default) 
     See JSF Specification 2.5.2 
     </description> 
    --> 
    </context-param> 
    <listener> 
    <listener-class>com.sun.faces.config.ConfigureListener</listener-class> 
    </listener> 
    <servlet> 
    <servlet-name>faces</servlet-name> 
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>faces</servlet-name> 
    <url-pattern>*.jsf</url-pattern> 
    </servlet-mapping> 
    <servlet-mapping> 
    <servlet-name>faces</servlet-name> 
    <url-pattern>*.faces</url-pattern> 
    </servlet-mapping> 

    <servlet> 
    <description></description> 
    <display-name>foreachservlet</display-name> 
    <servlet-name>foreachservlet</servlet-name> 
    <servlet-class>com.foreach.foreachservlet</servlet-class> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>foreachservlet</servlet-name> 
    <url-pattern>/go</url-pattern> 
    </servlet-mapping> 

    <welcome-file-list> 
    <welcome-file>index.html</welcome-file> 
    <welcome-file>index.htm</welcome-file> 
    <welcome-file>index.jsp</welcome-file> 
    <welcome-file>default.html</welcome-file> 
    <welcome-file>default.htm</welcome-file> 
    <welcome-file>default.jsp</welcome-file> 
    </welcome-file-list> 
    <context-param> 
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name> 
    <param-value>resources.application</param-value> 
    </context-param> 
</web-app> 

Servlet的代碼:

package com.foreach; 

import java.io.IOException; 

import javax.servlet.RequestDispatcher; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

/** 
* Servlet implementation class foreachservlet 
*/ 
public class foreachservlet extends HttpServlet { 
    private static final long serialVersionUID = 1L; 

    /** 
    * @see HttpServlet#HttpServlet() 
    */ 
    public foreachservlet() { 
     super(); 
     // TODO Auto-generated constructor stub 
    } 

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

     try{ 
      System.out.println("Getting inside get method--"); 
      String[] movieList={"Isquia","3 idiots","Kurbaan","Jab we met"}; 
      request.setAttribute("movieList",movieList); 

      System.out.println("After setting attribute"); 
      RequestDispatcher dispatcher=request.getRequestDispatcher("/pages/welcome.jsp"); 
      dispatcher.forward(request, response); 

       System.out.println("Last line--"); 

     }catch(Exception e){ 
      e.printStackTrace(); 
     } 


    } 

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

} 

jsp代碼:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> 
<HTML> 
<HEAD><TITLE>Movie Collections</TITLE> 
</HEAD> 
<BODY> 
    <strong>Movie list</strong><br></br> 
    <TABLE BORDER=5 ALIGN="CENTER"> 
    <c:forEach var="movie" items="${movieList}"> 
     <tr> 
     <td>${movie}</td> 
     </tr> 
    </c:forEach> 
    </TABLE> 
</BODY></HTML> 

我收到以下錯誤,我無法理解。

welcome.jsp:9:24: Static attribute must be a String literal, its illegal to specify an expression. 
<c:forEach var="movie" items="${movieList}"> 
         ^---^ 
welcome.jsp:9:24: Static attribute must be a String literal, its illegal to specify an expression. 
<c:forEach var="movie" items="${movieList}"> 
         ^---^ 

請幫忙。謝謝

回答

14

我認爲這是因爲你的taglib聲明中的URI不正確。它應該是

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 

,而不是

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> 

您正在使用的一個是舊的,預JSP-2.0庫,它不支持運行時表達式(表達中傳遞作爲原始字符串並由標籤本身評估,而不是JSP引擎)。

編輯:看起來它是你與Expression Language in JSP not working

+1

在另一個問題中,他最初在模板文本中遇到了EL問題。 – BalusC 2010-02-07 13:24:18

+0

非常感謝先生。問題僅在於標籤庫。 – user262577 2010-02-07 14:08:25

+0

很高興幫助你。請不要忘記標記他的答案**接受**通過點擊左邊的大白色複選標記:)這就是它應該如何在這裏工作在Stackoverflow:http://stackoverflow.com/faq – BalusC 2010-02-08 02:33:31

0

同樣的問題,你可以試試:

<c:forEach items="${movieList}" varStatus="i"> 
    <td>${movieList[i.index]}</td> 
</c:forEach> 
1

請加入這一行:

<%@ taglib uri='http://java.sun.com/**jsp**/jstl/core' prefix='c'%> 

相反的:

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> 

其實JSTL - 只是一套自定義標籤,不能單獨使用。 如果你不使用任何MVC框架,那麼你需要自己做一些修改。 如果您正在使用Spring MVC的則包括JSTL爲:

<%@taglib prefix="c" uri="http://java.sun.com/jstl/**core_rt**"%> 

如果不使用core_rt那麼它會拋出錯誤。