Can't use JSTL on a simple example

允我心安 提交于 2020-01-03 18:03:28

问题


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?


回答1:


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)



来源:https://stackoverflow.com/questions/24648545/cant-use-jstl-on-a-simple-example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!