20
 <%!  
    String str = "prerna";  
  %>  

 <jsp:include page="index.html">
      <jsp:param name="type1" value=<%=str%> >
      </jsp:param>  
 </jsp:include>

我想在 param 标记中传递一个 java 变量,但我不知道该怎么做。

我也想在index.html.
任何人都可以建议我这样做的方法吗?

4

5 回答 5

24

直接放进去就好了value

<jsp:include page="index.html">
    <jsp:param name="type1" value="prerna" />
</jsp:include>

或者使用 JSTL<c:set>设置它并使用 EL${}获取它。

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
...
<c:set var="type1" value="prerna" />
...
<jsp:include page="index.html">
    <jsp:param name="type1" value="${type1}" />
</jsp:include>

如果您包含的页面是 jsp,那么您可以将其用作${param.type1}

于 2011-03-27T04:22:48.853 回答
5

Request parameters can be passed by using <jsp: param>
One can pass the parameter names and values to the forwarded file by using a <jsp: param> tag

Sample e.g :

HTML :

<html>
<head>
<title></title>
</head>
<body>
<jsp:forward page="ssParameters.jsp">
  <jsp:param name="myParam" value="Amar Patel"/>
  <jsp:param name="Age" value="15"/>
</jsp:forward>
</body>
</html>   

<jsp:param> tag is used to pass the name and values to the targeted file. These parameters will be retrieved by the targeted file by using request.getParameter() method. In this way one can pass and retrieve the parameters.

This page had a parameter forwarded to it:<br>
  <b>Name:</b> <%= request.getParameter("myParam") %><br>
  <b>Age:</b> <%= request.getParameter("Age") %>
于 2011-03-26T18:14:06.147 回答
3

将参数传递给 jsp jstl:

/* JSP PARENT */

<jsp:include page="../../templates/options.jsp">                    
    <jsp:param name="action" value="${myValue}"/>       
</jsp:include>


/* JSP CHILD (options.jsp)*/

<div id="optionButtons left">       
    <span>${param.action}</span>
</div>
于 2014-05-12T16:25:39.433 回答
2

只是,双引号中的 <%=str%> 应该可以,我希望这是您问题的答案。

<%!  
    String str = "prerna";  
%>  

<jsp:include page="index.html">
      <jsp:param name="type1" value="<%=str%>" />  
</jsp:include>
于 2018-06-07T23:15:20.847 回答
0

使用request.setAttribute()您可以将 Java 变量传递给 JSP。

 <%  
    String str = "prerna";

    request.setAttribute("myVar",str);
  %>  

 <jsp:include page="index.html">
      <jsp:param name="type1" value="${myVar}" >
      </jsp:param>  
 </jsp:include>
于 2018-08-31T21:24:07.647 回答