不能在一个简单的示例上使用JSTL [英] Can't use JSTL on a simple example
问题描述
这是我目前的情况: 我已经使用以下命令从Shell创建了一个Maven项目:
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
然后我打开Eclipse,将项目作为Maven导入.我将那些依赖项添加到我的
pom.xml
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>
然后我创建了一个JSP和一个Servlet.
Then I created a JSP and a Servlet.
我的servlet仅设置了一些变量,而我的JSP将它们与一些JSTL一起使用.
My servlet just sets some variables and my JSP use them with some JSTL.
我在JSP上添加了以下标签:
I've added on my JSP this tag:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
JSP上的代码非常简单:
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>
我的Servlet正在这样做:
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是一个简单的Bean.
Employee is a simple Bean.
当我尝试从Servlet运行此应用程序时,它实际上在我的JSP上向我显示了此信息:
When I try to run this application, from my Servlet, it actually shows me this, on my JSP:
${emp.id} ${emp.name} ${emp.role}
它并没有显示我在Servlet上设置的值.
And it's not showing the value that I set on my Servlet.
我是JSTL的新手,所以我首先用Google搜索了我的问题.我尝试在$TOMCAT_HOME/lib
目录中添加jstl-1.2.jar
,但是没有用.
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.
那是什么问题?
要运行JSTL,我需要在容器和项目上进行哪些配置?我做的还不够吗?
what are the configuration I need to do on my container and project to run JSTL? Isn't enough what I've made?
推荐答案
我认为JSTL没问题.这种表示法:${emp.role}
是EL(表达语言),它不起作用.
I think that it is not a problem with JSTL. This notation: ${emp.role}
is EL (Expression Language) and it is not working.
您没有在JSP文件中设置isELIgnored="true"
吗?像这样:
Don't you have isELIgnored="true"
set somewere in the JSP file? Like this:
<%@ page isELIgnored="true" %>
或者也许在web.xml
中:
<el-ignored>true</el-ignored>
默认情况下应该为false
,但是如果您使用的Servlet版本早于2.4,则默认值为true
,因此在这种情况下,您需要在web.xml
中将其设置为false
:>
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>
您在依赖项中具有版本3.1
,但是在web.xml
文件中使用了2.3
版本.要使用Servlet 3.1
,请尝试将web.xml
更改为:
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>
也删除:
<!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
版本)
这篇关于不能在一个简单的示例上使用JSTL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!