In jsp A, I have this statement:

<c:set var="prefix" value="fe" />

In jsp A, I include jsp B and then in B, I do:
alert(${prefix});

And I get : 'fe' in undefined How can I get the value of the variable prefix in jsp B?

有帮助吗?

解决方案 2

Maybe, when the jsp is rendered, alert(${perfix}) is evaluated to alert(fe). The error you are getting is a javascript error because there is probably nothing defined as fe in your code.

What you might want to do is change it to

<c:set var="prefix" value="'fe'" />

So that it's evaluated as a String in Javascript.

其他提示

You can set the scope of the variable to request:

<c:set var="prefix" value="fe" scope="request"/>

Also, make sure you're using the include directive rather than the include standard action. The directive happens at translation time so variables defined in the including file are available to the included file but the include standard action actually inserts the response from the included file at runtime and therefore won't have access to variables set in the including file. Simply put use this:

<%@include file="myinclude.jsp" %>

instead of this:

<jsp:include page="myinclude.jsp" />
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top