无法在jsp模板之间传递变量

时间:2015-01-28 22:26:58

标签: spring jsp spring-mvc

我无法将变量从a.jsp传递到b.jsp,以下是我尝试过的和输出。有人可以帮忙吗?感谢。

a.jsp

start including
<jsp:include page="b.jsp">
<jsp:param name="somevar" value="zzzz" />
</jsp:include>
 stop including

b.jsp

${somevar}
${param.somevar}
<%=request.getParameter("somevar")%>

输出

start including


null
stop including

2 个答案:

答案 0 :(得分:0)

你必须在包含之前设置你的参数。

start including
<jsp:params>
    <jsp:param name="somevar" value="zzzz" />
</jsp:params>

<jsp:include page="b.jsp">
</jsp:include>
stop including

你可以在b.jsp中尝试这个:

<%=request.getParameter("somevar");%>

什么是输出?

答案 1 :(得分:0)

请尝试以下代码:

<强>针对home.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page session="false"%>
<html>
<head>
<title>Home</title>
</head>
<body>
    <h1>Hello world!</h1>

    <jsp:include page="b.jsp">
        <jsp:param name="something" value="something" />
    </jsp:include>

    <h1>Bye World</h1>
</body>
</html>

在b.jsp页面中使用以下代码。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Param example</title>
</head>
<body>
    <h1>
        <c:out value="${param.something}" />
    </h1>
</body>
</html>

输出必须是

Hello world!
something 
Bye World

尽量避免在JSP页面中使用java代码,因为这是不可取的。有关详细信息,请查看此答案How to avoid Java Code in JSP-Files?