Here's my current situation: I've created a Maven project from my shell, using this 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 a Maven one. I added those 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 use them with some JSTL.
I've added on my JSP this tag:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
and the code on my JSP is really 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 it's doing 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);
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's not showing the value that I set on my Servlet.
I'm totally new to JSTL, so I googled first for my issue. I tried adding jstl-1.2.jar
on my $TOMCAT_HOME/lib
directory, but it didn't work.
What's the problem then?
EDIT: what are the configuration I need to do on my container and project to run JSTL? Isn't enough what I've made?
I think that it is not a problem with JSTL. This notation: ${emp.role}
is EL (Expression Language) and it is not working.
Don't you have isELIgnored="true"
set somewere in the JSP file? Like this:
<%@ page isELIgnored="true" %>
Or maybe in web.xml
:
<el-ignored>true</el-ignored>
It should be false
by default, but if you use servlet version older than 2.4 then the default is true
, so in that case you would need to set it to false
in web.xml
:
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<el-ignored>true</el-ignored>
</jsp-property-group>
</jsp-config>
You have version 3.1
in dependencies, but in web.xml
file 2.3
version is used. To use Servlet 3.1
try to change your web.xml
into:
<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>
Also remove:
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
(it's for 2.3
version)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With