以下是我的jsp代码的一部分:
${cForm.cId} <!-- I can see this value displayed correctly --%>
<%! String i = ${cForm.cId}; %>
<td class="value">
<img src="/supportcenter/cServlet?sampleImg=<%=i %>" width="300px" />
</td>
cForm
是我的 java 对象文件,我可以在浏览器中看到 ${cForm.cId}
的值正确显示。
但是,当我想将值分配给 String i
变量时,我一直遇到 fail to compile :
jsp 错误。
请多多指教。
最佳答案
您不能在 scriptlet 中混合表达语言。您应该仅使用 scriptlet 或仅使用表达式语言。
要修复当前错误,请从请求(或存储该属性的任何位置)检索该属性:
<% String i = ((CForm)request.getAttribute("cForm")).getId(); %>
要真正解决您的问题:stop using scriplets at all ,将其全部保留为表达语言:
<img src="/supportcenter/cServlet?sampleImg=${cForm.cId}" width="300px" />
关于java - 将 java 对象文件值分配给 jsp 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23601046/