今天在测试JSP中的<jsp:forward>指令时遇到了以下错误提示:
org.apache.jasper.JasperException: /index.jsp (line: 24, column: 4) Expecting "jsp:param" standard action with "name" and "value" attributes org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:42) org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:408) org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:89) org.apache.jasper.compiler.Parser.parseParam(Parser.java:825) org.apache.jasper.compiler.Parser.parseBody(Parser.java:1672) org.apache.jasper.compiler.Parser.parseOptionalBody(Parser.java:1002) org.apache.jasper.compiler.Parser.parseForward(Parser.java:866) org.apache.jasper.compiler.Parser.parseStandardAction(Parser.java:1118) org.apache.jasper.compiler.Parser.parseElements(Parser.java:1451) org.apache.jasper.compiler.Parser.parse(Parser.java:138) org.apache.jasper.compiler.ParserController.doParse(ParserController.java:242) org.apache.jasper.compiler.ParserController.parse(ParserController.java:102) org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:198) org.apache.jasper.compiler.Compiler.compile(Compiler.java:373) org.apache.jasper.compiler.Compiler.compile(Compiler.java:353) org.apache.jasper.compiler.Compiler.compile(Compiler.java:340) org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
JSP页面代码为:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<%@ include file="top.jsp" %>
<!-- <a href="./errorTest.jsp">错误测试</a> -->
<jsp:forward page="./errorTest.jsp"> (这处的<jsp:forward></jsp:forward>标签对不能跨行)
</jsp:forward>
<br>
</body>
</html>
经查发现,导致出现该错误的原因就是代码中<jsp:forward page="./errorTest.jsp"></jsp:forward>这对标签跨行了;当<jsp:forward></jsp:forward>标签对没进行传参时(即没有<jsp:param>子标签时),不能跨行;反之,进行传参时(有<jsp:param>子标签)可以跨行,例如:
<jsp:forward page="./errorTest.jsp">
<jsp:param value="Hello!" name="param1"/>
<jsp:param value="hi!" name="param2"/>
</jsp:forward>
(备注:本人的测试环境:MyEclipse2014、JDK1.6、Tomcat7.0)