使用JSTL中遇到的EL報錯問題

 今天自己想玩一把JSTL可惜老出狀況。所以自己就去網上找嘍!
網上給了一個答案,還不錯:
解決方案1:
修改web.xml文件
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
把上面那個改爲這個:
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
對。我試了一下,不錯,是可以運行。但是還有一個問題:EL表達示他不運行啊!好像被忽略掉了一樣。
下面給一個例子。自己運行一下看看就明白了。
myjsp.jsp文件
<%@ page contentType="text/html;charset=GB2312"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
      <title>My JSP 'myjsp.jsp' starting page</title>
  </head>
 
  <body>
     
    This is my JSP page. <br>
    <table>
    <tr>
    <td>${true and true}</td>
      <td> <c:set var="user" value="zhangsan" scope="session"/></td>
   <td><c:out value="${true and true}"/></td>
    <td>變量爲</td>
    <td><c:out value="4545"/></td>
    </tr>
    </table>
  </body>
</html>


web.xml文件
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
</web-app>

你運行以後的結果了:它變成什麼樣了哦?
This is my JSP page.
${true and true}   true 變量爲 4545
輸出 的是這個樣子
而我們要的是這個結果:
This is my JSP page.
true   true 變量爲 4545
所以,以上的方法有一些些問題哦~!

解決方案2:
再給我們的web.xml
先把我們從apache的網站上下的文件解壓
然後把jakarta-taglibs-standard-1.1.2/jakarta-taglibs-standard-1.1.2/tld裏面的那幾個標籤copy過來到你的工作區。
web.xml改成這樣:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
  <jsp-config>
    <taglib>
        <taglib-uri>http://java.sun.com/jstl/fmt</taglib-uri>
        <taglib-location>/WEB-INF/fmt.tld</taglib-location>
    </taglib>
   
    <taglib>
        <taglib-uri>http://java.sun.com/jstl/fmt-rt</taglib-uri>
        <taglib-location>/WEB-INF/fmt-rt.tld</taglib-location>
    </taglib>
   
    <taglib>
        <taglib-uri>http://java.sun.com/jstl/core</taglib-uri>
        <taglib-location>/WEB-INF/c.tld</taglib-location>
    </taglib>
   
    <taglib>
        <taglib-uri>http://java.sun.com/jstl/core-rt</taglib-uri>
        <taglib-location>/WEB-INF/c-rt.tld</taglib-location>
    </taglib>
   
    <taglib>
        <taglib-uri>http://java.sun.com/jstl/sql</taglib-uri>
        <taglib-location>/WEB-INF/sql.tld</taglib-location>
    </taglib>
   
    <taglib>
        <taglib-uri>http://java.sun.com/jstl/sql-rt</taglib-uri>
        <taglib-location>/WEB-INF/sql-rt.tld</taglib-location>
    </taglib>
   
    <taglib>
        <taglib-uri>http://java.sun.com/jstl/x</taglib-uri>
        <taglib-location>/WEB-INF/x.tld</taglib-location>
    </taglib>
   
    <taglib>
        <taglib-uri>http://java.sun.com/jstl/x-rt</taglib-uri>
        <taglib-location>/WEB-INF/x-rt.tld</taglib-location>
    </taglib>
  </jsp-config> 
   
</web-app>
好啦!改成這樣就可以在jsp裏面了較好的使用他們了!
大家加油啊!
發佈了34 篇原創文章 · 獲贊 0 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章