【问题标题】:EL tag not working with maven in tomcat 7EL标签不适用于tomcat 7中的maven
【发布时间】:2014-03-12 04:34:03
【问题描述】:

调用 jsp 页面时不呈现 EL 标记。 <c:out value="${uploadedImage.value.getDocumentName()}"/>

打印 ${uploadedImage.value.getDocumentName()}

我的 pom 文件是

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<url>http://maven.apache.org</url>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.9</version>
</dependency> 

<dependency>
    <groupId>dom4j</groupId>
    <artifactId>dom4j</artifactId>
    <version>1.6.1</version>
</dependency>

<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.1.1</version>
</dependency>

<dependency>
    <groupId>commons-collections</groupId>
    <artifactId>commons-collections</artifactId>
    <version>3.2.1</version>
</dependency>

<dependency>
    <groupId>cglib</groupId>
    <artifactId>cglib</artifactId>
    <version>2.2</version>
</dependency>

<dependency>
           <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.3.4.Final</version>
</dependency>

<dependency>
    <groupId>javax.transaction</groupId>
    <artifactId>jta</artifactId>
    <version>1.1</version>
</dependency>

<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-servlet-api</artifactId>
    <version>7.0.12</version>
</dependency>

<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-el-api</artifactId>
    <version>7.0.12</version>
</dependency>

<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-jsp-api</artifactId>
    <version>7.0.12</version>
</dependency>

<dependency>
    <groupId>taglibs</groupId>
    <artifactId>standard</artifactId>
    <version>1.1.2</version>
</dependency>

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
    <scope>runtime</scope>
</dependency>

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>


<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.2</version>
</dependency>

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
</dependency>                    
</dependencies>
<build>
    <finalName>ImageManagement</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>
</project>

我的 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>

Web 应用程序具有所有 Servlet 映射。

我的项目配置是 雄猫 7.0 Eclipse 开普勒

<c:if test = "${not empty uploadedImages}">
<% int i = 0; %>
    <c:forEach var="uploadedImage" items="${uploadedImages}">
        <tr>
            <% i++; %>
            <td><%= i %></td>
            <td><c:out value="${uploadedImage.value.getDocumentName()}"/></td>
            <td><c:out value="${uploadedImage.value.getDocumentSize()}"/></td>
            <!-- <td><img src="image?id=${uploadedImage.value.getUserRepositryId()}" style = "height : 80px; width : 50px" alt = "image here"/></td>-->

            <td>
                <a href = "delete?id=${uploadedImage.value.getUserRepositryId()}"> Delete </a>
                <a href = "edit?id=${uploadedImage.value.getUserRepositryId()}"> Edit </a>
            </td>
        </tr>
    </c:forEach>
</c:if>

uploadedImages 是一个 HashMap 变量,它被设置为 Session 对象。 它从数据库中获取保存的图像并将其保存为 HashMap。 数据库部分工作正常,它从数据库加载图像并将它们保存在 HashMap 中。

唯一的问题是工作中的 EL 标签。 这个项目在没有 maven 的情况下运行良好。

UserRepositryDTO.java

public class UserRepositryDTO {
private int userRepositryId;
private int userId;
private String documentName;
private long documentSize;

private byte[] document;
private String message;


/**
 * @return the message
 */
public String getMessage() {
    return message;
}
/**
 * @param message the message to set
 */
public void setMessage(String message) {
    this.message = message;
}
/**
 * @return the userRepositryId
 */
public int getUserRepositryId() {
    return userRepositryId;
}
/**
 * @return the userId
 */
public int getUserId() {
    return userId;
}
/**
 * @return the documentName
 */
public String getDocumentName() {
    return documentName;
}
/**
 * @return the documentSize
 */
public long getDocumentSize() {
    return documentSize;
}

/**
 * @return the document
 */
public byte[] getDocument() {
    return document;
}
/**
 * @param userRepositryId the userRepositryId to set
 */
public void setUserRepositryId(int userRepositryId) {
    this.userRepositryId = userRepositryId;
}
/**
 * @param userId the userId to set
 */
public void setUserId(int userId) {
    this.userId = userId;
}
/**
 * @param documentName the documentName to set
 */
public void setDocumentName(String documentName) {
    this.documentName = documentName;
}
/**
 * @param documentSize the documentSize to set
 */
public void setDocumentSize(long documentSize) {
    this.documentSize = documentSize;
}

/**
 * @param document the document to set
 */
public void setDocument(byte[] document) {
    this.document = document;
}
}

UserDTO.java

public class UserDTO {
private int userId;
private String username;
private String message;
private long totalSize;
private Map<Integer,UserRepositryDTO> userRepositry;

/**
 * @return the totalSize
 */
public long getTotalSize() {
    return totalSize;
}
/**
 * @param totalSize the totalSize to set
 */
public void setTotalSize(long totalSize) {
    this.totalSize = totalSize;
}

/**
 * @return the userId
 */
public int getUserId() {
    return userId;
}
/**
 * @return the username
 */
public String getUsername() {
    return username;
}
/**
 * @return the message
 */
public String getMessage() {
    return message;
}
/**
 * @param userId the userId to set
 */
public void setUserId(final int userId) {
    this.userId = userId;
}
/**
 * @param username the username to set
 */
public void setUsername(final String username) {
    this.username = username;
}
/**
 * @param message the message to set
 */
public void setMessage(final String message) {
    this.message = message;
}
public Map<Integer,UserRepositryDTO> getUserRepositry() {
    return userRepositry;
}
public void setUserRepositry(Map<Integer,UserRepositryDTO> userRepositry) {
    this.userRepositry = userRepositry;
}
}

这个问题是由于servlet jsp jstl的不同版本造成的。我不知道正确的版本。

【问题讨论】:

  • 你使用EL无效..试试${uploadedImage.value.documentName}
  • 这是有效的,因为 getDocumentName 是返回文档名称的函数
  • 请添加你有getDocumentName()的类。您在 OP 中添加的依赖项很有用..

标签: eclipse jsp maven tomcat jstl


【解决方案1】:

首先,不要使用Scriptlets(&lt;%= %&gt;)获取循环索引,可以通过定义varStatus&lt;c:forEach标签获取索引。

考虑到您已将 uploadedImages(Map) 对象添加到您的 servlet 上的请求中。

req.setAttribute("uploadedImages", uploadedImages);

用于从 uploadedImages 中迭代 uploadedImage 对象在 jsp 中的 Map 喜欢:

在您的 jsp 顶部导入 jstl taglib:

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

那么,

<c:choose>
  <c:when test="${not empty uploadedImages}">
    <c:forEach var="uploadedImage" items="${uploadedImages}" varStatus="status">
        <tr>
            <!--Her, don't use Scriptlets to get index, get index from <c:forEach varStatus -->
            <td>${status.index}</td> 
            <td><c:out value="${uploadedImage.value.documentName}"/></td>
            <td><c:out value="${uploadedImage.value.documentSize}"/></td>
            <!-- <td><img src="image?id=${uploadedImage.value.userRepositryId}" style = "height : 80px; width : 50px" alt = "image here"/></td>-->

            <td>
                <a href = "delete?id=${uploadedImage.value.userRepositryId}"> Delete </a>
                <a href = "edit?id=${uploadedImage.value.userRepositryId}"> Edit </a>
            </td>
        </tr>
    </c:forEach>
</c:when>
<c:otherwise>
   uploadedImages Map is empty..
</c:otherwise>
<c:choose>

用表达式语言(${ })或 jstl(&lt;c: 标签)从 DTO 获取属性值:

在 EL:

${uploadedImage.value.documentName} <!--internally EL will invoke getDocumentName() to get documentName value-->

在 JSTL 中:

<c:out value="${uploadedImage.value.documentName}"/>

【讨论】:

    【解决方案2】:

    您需要修复您的 web.xml。它声明它使用 Servlet 2.3,相当于不支持 EL 的 JSP 1.2。

    尝试使用以下内容:

    <web-app xmlns="http://java.sun.com/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                                 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
             version="3.0"
             metadata-complete="true">
    </web-app>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-31
      • 2012-06-03
      • 1970-01-01
      • 2011-12-09
      相关资源
      最近更新 更多