简体   繁体   English

OpenCMS:如何在JSP / Javascript / Java中使用变量?

[英]OpenCMS: How to use variables in JSP/Javascript/Java?

I have a form like this: 我有这样的形式:

<form id="form" method="post">
    Name: <input name="name" />
    E-Mail: <input name="email"/>
    Comment: <textarea name="comment" cols="5" rows="10"></textarea>
    <input type="submit" name="submit" value="submit"/>
</form>

for a commentary function for some developer blog. 一些开发者博客的评论功能。 Therefore I need to insert the input into some database for some select queries later. 因此,我需要稍后将输入插入某些数据库以进行某些选择查询。

Now how do I tell the script which comments belong to which blog post? 现在如何告诉脚本哪些评论属于哪个博客帖子?

My idea would be to get the name of the file or the title of the entry or something and put it into the query I'm creating. 我的想法是获取文件名或条目标题或其他内容,并将其放入我正在创建的查询中。 And I might manage to find out how to get the filename in OpenCMS as well - that's about five minutes of searching, if any - but what I don't know is how I get the OpenCMS variable into my Java part of the file, where I set up the actual query/queries. 而且我可能还设法找出如何在OpenCMS中获取文件名-大约需要五分钟的搜索时间-但我不知道如何将OpenCMS变量放入文件的Java部分中,在哪里我设置了实际的查询/查询。

I searched for that earlier already, some weeks ago, but I managed to find a way to avoid this then. 几周前,我已经在较早时进行搜索,但是后来设法找到了避免这种情况的方法。 Now though I can't think of a way of avoidance either, so I'm hoping that someone knows how to get this done properly. 现在,尽管我也无法想到一种避免的方法,所以我希望有人知道如何正确完成此任务。

In case you need this, here's the code for the actual blog entries so far, I'm going to append the commentary function (being an element) underneath the author information then. 如果需要,这里是到目前为止实际博客条目的代码,然后我将注释功能(作为元素)添加到作者信息的下面。

                <cms:contentcheck ifexistsone="Header"><h2><cms:contentshow element="Header" /></h2></cms:contentcheck>
                <p class="BlogEntry">
                <cms:contentcheck ifexistsone="Text"><cms:contentshow element="Text" /></cms:contentcheck>  
                <cms:contentcheck ifexistsone="IntLink"><%@ include file="/system/modules/de.medienkonzepte.uform.templates/elements/internerlink.txt" %></cms:contentcheck>
                </p>
                <cms:contentcheck ifexistsone="Image">
                    <cms:contentloop element="Image">
                        <img style="padding:10px;" src="<cms:link><cms:contentshow element="ImageSrc" /></cms:link>" alt="<cms:contentshow element="Alt_Tag" />" />
                    </cms:contentloop>
                </cms:contentcheck>

            </cms:contentloop>
        </cms:contentcheck>
                 <cms:contentcheck ifexistsone="Content/Text">

        <c:set var="dateString"><cms:contentshow element="Date"/></c:set>
        <% 
         java.util.Date date = new java.util.Date(); 

         date.setTime(Long.parseLong(pageContext.getAttribute("dateString").toString())); 
         pageContext.setAttribute("date", date); 
        %>                  

                <% // Author %>
                <cms:contentcheck ifexistsone="Author"><p class="blogentry_author">Written by: <cms:contentshow element="Author"/> on <fmt:formatDate value="${date}" type="date" pattern="dd.MM.yyyy"/></p></cms:contentcheck>

        </cms:contentcheck> 
    </div> <% // ende innercontent %>

a commentary function for some developer blog 一些开发人员博客的评论功能

Is the user on the detail page of the blog entry (only this one entry for the entire page), with a comment form at the bottom, as very common, ie in Wordpress blogs? 用户是否在博客条目的详细信息页面上(整个页面只有一个条目),并在底部带有评论表格(在Wordpress博客中很常见)? Or is it a page with multiple blog entries on it? 还是页面上有多个博客条目?

If you're on the detail page, you can get the current filename right away with java code: 如果您在详细信息页面上,则可以使用Java代码立即获取当前文件名:

CmsJspActionElement cms = new CmsJspActionElement(pageContext, request, response);
String filename = cms.getRequestContext().getUri();

but what I don't know is how I get the OpenCMS variable into my Java part of the file, where I set up the actual query/queries. 但是我不知道如何将OpenCMS变量放入文件的Java部分中,在其中设置实际的查询/查询。

<c:set var="filename" scope="request">output your OpenCms variable in here</c:set>

Then you can access the filename variable via 然后您可以通过访问文件名变量

 request.getAttribute("filename")

Where are you doing the query? 您在哪里查询? Right in jsp or in a java class? 是在jsp还是在Java类中? But doesn't matter much, you can just pass the request as parameter to it. 但是没关系,您可以将请求作为参数传递给它。 This is a way to do it: 这是一种方法:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM