如何将值从 JSP 传递到tiles属性?
我正在将现有的 Tiles 1 Web 应用程序转换为 Tiles 2 架构。我在将值从 JSP 页面传递到图块属性时遇到问题。
这是我的图块定义文件 (tiles-definition.xml)
<tiles-definitions>
<definition name="cda.layout" template="/jsp/layouts/layout.jsp">
<put-attribute name="pageTitle" value="StoryTitle" type="string"/>
<put-attribute name="pageHeader" value="StoryHeader" type="string"/>
<put-attribute name="resources" value="" type="string"/>
</definition>
</tiles-definitions>
。layout.jsp 如下所示:
<html>
<head>
<title><tiles:insertAttribute name="pageTitle" flush="true"/></title>
</head>
<body>
...
...
<div class="content">
<h1><tiles:insertAttribute name="pageHeader" flush="true"/></h1>
</div>
...
...
</body>
</html>
我有一个使用布局的故事页面,需要将值传递给模板属性。
<%
// create a business object and populate
String mytitle= story.getTitle();
String myheader = story.getHeader();
%>
<tiles:insertTemplate template="../layouts/layout.jsp" flush="false" >
<tiles:putAttribute name="pageTitle" value="${mytitle}"/>
<tiles:putAttribute name="pageHeader"value="${myheader}"/>
</tiles:insertTemplate>
在 Story.jsp 中,我可以 System.out.print() mytitle、myheader 的值,它们显示正确。但是,这些值不会传递到图块属性。
知道如何解决这个问题吗?
I am converting an existing Tiles 1 webapp to Tiles 2 architecture. I am having trouble passing values from JSP page to tiles attributes.
Here is my tiles definition file (tiles-definition.xml)
<tiles-definitions>
<definition name="cda.layout" template="/jsp/layouts/layout.jsp">
<put-attribute name="pageTitle" value="StoryTitle" type="string"/>
<put-attribute name="pageHeader" value="StoryHeader" type="string"/>
<put-attribute name="resources" value="" type="string"/>
</definition>
</tiles-definitions>
The layout.jsp looks like:
<html>
<head>
<title><tiles:insertAttribute name="pageTitle" flush="true"/></title>
</head>
<body>
...
...
<div class="content">
<h1><tiles:insertAttribute name="pageHeader" flush="true"/></h1>
</div>
...
...
</body>
</html>
I have a story page which uses the layout and need to pass values to template attributes.
<%
// create a business object and populate
String mytitle= story.getTitle();
String myheader = story.getHeader();
%>
<tiles:insertTemplate template="../layouts/layout.jsp" flush="false" >
<tiles:putAttribute name="pageTitle" value="${mytitle}"/>
<tiles:putAttribute name="pageHeader"value="${myheader}"/>
</tiles:insertTemplate>
In the story.jsp, I can System.out.print() the values for mytitle, myheader and they are showing correct. But, these values are NOT passed on to the tile attributes.
Any idea how to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
${mytitle}
是一个 JSP EL 表达式,其含义是:在页面范围、请求范围、会话范围或应用程序范围中查找名为“mytitle”的属性。通过定义 scriptlet 变量,您还没有在任何这些范围中定义属性。如果你有的话它会起作用
但是在 JSP 中使用 scriptlet 是不好的做法。我不知道你的故事 bean 来自哪里,但它可能是一个请求属性。如果是这样,您可以使用 JSTL 以这种方式定义新的页面范围属性:
但这不是必需的,因为您可以直接在tiles 标记中使用此表达式:
在 本教程。
${mytitle}
is a JSP EL expression which means: find an attribute in page scope, or request scope, or session scope, or application scope, named "mytitle".By defining a scriptlet variable, you haven't defined an attribute in any of these scopes. It would work if you had
But using scriptlets in JSPs is bad practice. I don't know where your story bean comes from, but it's probably a request attribute. If so, you can define a new page-scope attribute this way, using the JSTL:
This is unnecessary though, since you could use this expression directly in the tiles tag:
Read more about the JSP EL in this tutorial.