spring 如何将值从 JSP 传递给tiles 属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8915976/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:52:39  来源:igfitidea点击:

How to pass values from JSP to tiles attribute?

springjsptiles

提问by Srinath Dasu

I am converting an existing Tiles 1 webapp to Tiles 2 architecture. I am having trouble passing values from JSP page to tiles attributes.

我正在将现有的 Tiles 1 webapp 转换为 Tiles 2 架构。我在将值从 JSP 页面传递到磁贴属性时遇到问题。

Here is my tiles definition file (tiles-definition.xml)

这是我的瓷砖定义文件(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:

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>

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.

在 story.jsp 中,我可以 System.out.print() mytitle、myheader 的值,它们显示正确。但是,这些值不会传递给 tile 属性。

Any idea how to fix this?

知道如何解决这个问题吗?

回答by JB Nizet

${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".

${mytitle}是一个 JSP EL 表达式,意思是:在页面范围、请求范围、会话范围或应用程序范围中查找名为“mytitle”的属性。

By defining a scriptlet variable, you haven't defined an attribute in any of these scopes. It would work if you had

通过定义 scriptlet 变量,您还没有在任何这些范围内定义属性。如果你有它会工作

pageContext.setAttribute("mytitle", mytitle);

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:

但是在 JSP 中使用 scriptlet 是不好的做法。我不知道你的故事豆来自哪里,但它可能是一个请求属性。如果是这样,您可以使用 JSTL 以这种方式定义新的页面范围属性:

<c:set var="mytitle" value="${story.title}"/>

This is unnecessary though, since you could use this expression directly in the tiles tag:

不过这是不必要的,因为您可以直接在tiles标签中使用这个表达式:

<tiles:putAttribute name="pageTitle" value="${story.title}"/>

Read more about the JSP EL in this tutorial.

本教程中阅读有关 JSP EL 的更多信息。