Cannot use JSTL in a simple example

Here is my current situation: I created a Maven project from my shell using the following command:

mvn archetype:generate -DgroupId=it.my.current.package.example -DartifactId=Example -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
mvn package

Then I opened Eclipse, imported the project as Maven. I added these dependencies to my pom.xml

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
    <scope>provided</scope>
</dependency>

Then I created a JSP and a servlet.

My servlet just sets some variables, and my JSP uses them with some JSTLs.

I added this tag to my JSP:

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

and my JSP code is very simple:

<c:forEach items="${requestScope.empList}" var="emp">
    <tr>
        <td><c:out value="${emp.id}"></c:out></td>
        <td><c:out value="${emp.name}"></c:out></td>
        <td><c:out value="${emp.role}"></c:out></td>
    </tr>
</c:forEach>

My servlet does this:

List<Employee> empList = new ArrayList<Employee>();
Employee emp1 = new Employee();
emp1.setId(1); emp1.setName("Sam");emp1.setRole("Developer");
Employee emp2 = new Employee();
emp2.setId(2); emp2.setName("John");emp2.setRole("Manager");
empList.add(emp1);empList.add(emp2);
request.setAttribute("empList", empList);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/home.jsp");
rd.forward(request, response);

An employee is a simple bean.

When I try to run this application from my servlet, it actually shows me this on my JSP:

${emp.id}      ${emp.name}      ${emp.role}

And it does not show the value that I set on my Servlet.

I am completely new to JSTL, so I entered my problem first. I tried adding jstl-1.2.jarto my directory $TOMCAT_HOME/lib, but that didn't work.

?

EDIT: , JSTL? , ?

+4
1

, JSTL. : ${emp.role} - EL ( ), .

isELIgnored="true", JSP ? :

<%@ page isELIgnored="true" %>

, , web.xml:

<el-ignored>true</el-ignored>

false, 2.4, true, false web.xml:

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <el-ignored>true</el-ignored>
    </jsp-property-group>
</jsp-config>

3.1 , web.xml file 2.3. Servlet 3.1, web.xml :

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    rest of the TAGs
</web-app>

:

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

( 2.3)

+5

Source: https://habr.com/ru/post/1547700/


All Articles