eclipse - EL 标签不能与 tomcat 7 中的 maven 一起使用

标签 eclipse jsp maven tomcat jstl

调用 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 映射。

我的项目配置是 Tomcat 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;
}
}

用户DTO.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版本不同。我不知道正确的版本。

最佳答案

首先,不要使用Scriptlets(<%= %>)要获取循环索引,您可以从 <c:forEach 获取索引通过定义 varStatus 标记.

考虑到您已经添加了 uploadedImages ( Map ) 反对对您的 servlet 的请求。

req.setAttribute("uploadedImages", uploadedImages);

用于迭代 uploadedImage来自 uploadedImages 的对象jsp 中的 map 确实像:

在您的 jsp 顶部导入 jSTL 标签库:

<%@ 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(<c: 标签)中的 DTO 获取属性值,请执行以下操作:

在 EL 中:

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

在 JSTL 中:

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

关于eclipse - EL 标签不能与 tomcat 7 中的 maven 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22342099/

相关文章:

java - Eclipse 内容辅助中间字母匹配

java - org.openqa.selenium.NoSuchElementException

java - 如何在 JSTL 中舍入正负小数?

maven - 如何设置从 Maven 中的依赖项引用的属性

java - Eclipse 创建您自己的 system.out.println 快捷方式

eclipse - 当我放在单独的文件夹中时 Jsp 没有编译

java - Thymeleaf 模板 :data=http://www. somesite.com || org.thymeleaf.exceptions.TemplateProcessingException:无法解析为分配序列

java - 将 WAR 文件部署到远程服务器后出现错误(在本地机器上没有错误)

java - 公共(public)存储库(如 maven.org)的 Artifact 的详细(下载)统计信息

java - 在eclipse IDE中运行java maven项目时出现编译错误