jsp:param 与 Java 类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13496889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jsp:param with Java class
提问by John Hendrik
I have a JSP file that includes another JSP file. The first JSP should pass an instance of a Java class (widget) to the second JSP file.
我有一个包含另一个 JSP 文件的 JSP 文件。第一个 JSP 应该将 Java 类(小部件)的实例传递给第二个 JSP 文件。
This is what I have:
这就是我所拥有的:
The first JSP:
第一个JSP:
<jsp:include page="/container/SpecialWidget.jsp">
<jsp:param name="widget" value="${widget}"/> // widget is a .Java POJO
</jsp:include>
The second JSP:
第二个JSP:
${param.widget.id}
The problem is that this code gives an error (it says it doesn't know ID). If I omit the ".id" part, the page prints the Java code for the Java class, which means the class has been transferred correctly. If I change the ${widget} rule of the first page in, for example, ${widget.id} and I try to print ${param.widget}, everything works fine.
问题是这段代码给出了一个错误(它说它不知道 ID)。如果我省略“.id”部分,页面会打印 Java 类的 Java 代码,这意味着该类已正确传输。如果我更改第一页的 ${widget} 规则,例如,${widget.id} 并尝试打印 ${param.widget},一切正常。
My question: Why can't I pass a Java class and directly call upon its attributes? What am I doing wrong?
我的问题:为什么我不能传递一个 Java 类并直接调用它的属性?我究竟做错了什么?
Edit: error message: Caused by: javax.el.PropertyNotFoundException: Property 'id' not found on type java.lang.String
编辑:错误消息:由:javax.el.PropertyNotFoundException:在类型 java.lang.String 上找不到属性“id”
采纳答案by Rafael Borja
When you pass the variable ${widget}
it is translated at request time to a string (widget.toString()
). This value is then passed to the second JSP as a String, not as the original java object.
当您传递变量时,${widget}
它会在请求时转换为字符串 ( widget.toString()
)。然后将该值作为字符串传递给第二个 JSP,而不是作为原始 java 对象。
One approach to access the object's values is setting the parameter's value with the attribute's value:
访问对象值的一种方法是使用属性值设置参数值:
<jsp:param name="widgetId" value="${widget.id}"/>
Then use the code bellow on the second JSP:
然后在第二个 JSP 上使用下面的代码:
${param.widgetId}
You can also set widget as an request attributeand use it on the second page as ${widget.id}
or ${request.widget.id}. I suggest you use the second approach.
您还可以将小部件设置为请求属性,并在第二页上使用它作为${widget.id}
或 ${request.widget.id}。我建议你使用第二种方法。
回答by John Hendrik
I managed to fix my problem with the following code:
我设法用以下代码解决了我的问题:
<c:set var="widget" value="${widget}" scope="request" />
<jsp:include page="/SOMEWHERE/SpecialWidget.jsp"/>
Thank you both for your help:) It saved my day
谢谢你们的帮助:)它拯救了我的一天
回答by JB Nizet
<jsp:param>
passes the parameter as an HTTP request parameter, which can only be a String. So toString()
is called on your widget, and the result of this method is passed as parameter.
<jsp:param>
参数作为HTTP请求参数传递,只能是String。SotoString()
在您的小部件上调用,并且此方法的结果作为参数传递。
You should use a JSP tag, implemented as a tag file, instead of using a JSP include. See http://docs.oracle.com/javaee/1.4/tutorial/doc/JSPTags5.htmlfor how to define an use them.
您应该使用作为标记文件实现的 JSP 标记,而不是使用 JSP 包含。有关如何定义使用它们的信息,请参阅http://docs.oracle.com/javaee/1.4/tutorial/doc/JSPTags5.html。
For example:
例如:
Tag definintion, in /WEB-INF/tags/specialWidget.tag:
标签定义,在 /WEB-INF/tags/specialWidget.tag 中:
<%@ tag %>
<%@ attribute name="widget" required="true" type="the.fully.qualified.name.of.WidgetClass" %>
TODO: add the HTML markup that must be displayed, using ${widget} to access the passed in widget attribute
Tag usage, in any JSP:
标签用法,在任何 JSP 中:
<%@ taglib prefix="myTags" tagdir="/WEB-INF/tags" %>
...
Tada! I will use the specialWidget tag here, with widget as an attribute:
<myTags:specialWidget widget="${widget}"/>