简体   繁体   English

Liferay 6.2上的Spring MVC表单中的Portlet命名空间

[英]Portlet namespace in Spring MVC forms on Liferay 6.2

is there a way to get Spring MVC forms namespaced for portlets? 有没有办法让Spring MVC表单为portlet命名? I don't want to set 我不想设置

<requires-namespaced-parameters>false</requires-namespaced-parameters>

in order to get spring mvc forms working under Liferay 6.2. 为了让Spring mvc表格在Liferay 6.2下运行。

I was thinking of overriding the Spring form-taglib so that it puts the portlet-namespace prefix in front of the form field names/ids without actually trying to bind them to bean-properties with a namespace (which will obviously not work) but that seems very time-consuming. 我正在考虑重写Spring form-taglib,以便它将portlet-namespace前缀放在表单字段名称/ ids之前,而不实际尝试将它们绑定到带有命名空间的bean属性(这显然不起作用)但是看起来非常耗时。

Does anybody know of another way to solve this problem? 有没有人知道解决这个问题的另一种方法?

Here is an example of a form to show the exact problem: 以下是显示确切问题的表单示例:

<portlet:actionURL var="actionURL">
    <portlet:param name="action" value="search"/>
</portlet:actionURL>

<form:form action="${actionURL}" commandName="searchSettings">
    <form:input path="textField"/>
    <form:input path="anotherTextField"/>
    <input type="submit" value="Search"/>
</form:form>

And its corresponding bean would be: 它的相应bean将是:

public class SearchSettings {

    private String textField;
    private String anotherTextField;

    // .. getters & setters

}

This will not work under Liferay 6.2 as the form inputs are not namespaced. 这在Liferay 6.2下不起作用,因为表单输入不是命名空间。 They should be namespaced like so: 它们应该像这样命名空间:

<c:set var="ns"><portlet:namespace/></c:set>

<form:input path="${ns}textField"/>

However this will not work since now Spring will try to bind the formfield to a property 但是这不起作用,因为现在Spring将尝试将formfield绑定到属性

SearchSettings._namespace_portlet_textField

which of course does not exist. 这当然不存在。

Has anybody come across this problem and found a solution other than override Spring MVCs Form-Taglib? 有没有人遇到这个问题,并找到一个解决方案,而不是覆盖Spring MVC Form-Taglib? I saw that it's already documented on Spring's JIRA ( https://jira.springsource.org/browse/SPR-11176 ) but I couldn't find much else on it. 我看到它已经记录在Spring的JIRA( https://jira.springsource.org/browse/SPR-11176 )上,但我找不到其他的东西。

Thanks in advance. 提前致谢。

I have now managed to override the Spring form taglib (3.0.7.RELEASE) in order to support the portlet namespaces. 我现在设法覆盖Spring表单taglib(3.0.7.RELEASE)以支持portlet名称空间。 If you want to do this you'll have to look for this method in the Tag-classes (for example InputTag): 如果你想这样做,你将不得不在Tag-classes中寻找这个方法(例如InputTag):

protected void writeDefaultAttributes(TagWriter tagWriter) throws JspException

You need to override this so it works in the namespace like so: 您需要覆盖它,以便它在命名空间中工作,如下所示:

@Override
protected void writeDefaultAttributes(TagWriter tagWriter) throws JspException {
    writeOptionalAttribute(tagWriter, "name", getNamespace() + getName());
    writeOptionalAttribute(tagWriter, "id", getNamespace() + resolveId());
    super.writeDefaultAttributes(tagWriter);
}

Of course you'll need your own tagdescriptor in order to get the namespace in. Also you'll need to override the 当然,你需要自己的tagdescriptor来获取命名空间。你还需要覆盖

protected int writeTagContent(TagWriter tagWriter) throws JspException;

method, because that's the one calling your writeDefaultAttributes-method. 方法,因为那是调用writeDefaultAttributes方法的方法。

This works for now, but I am still looking for a better way to go about this. 这暂时有效,但我仍在寻找更好的方法来解决这个问题。

EDIT: 编辑:

You can do it quicker with jQuery: 你可以用jQuery更快地完成它:

$(document).ready(function() {
    // Alle inputs
    $('input').each(function() {
        var pnamespace = '<portlet:namespace/>';
        $(this).attr('id', pnamespace + this.id);
        $(this).attr('name', pnamespace + this.name);
    });
    // alle selects
    $('select').each(function() {
        var pnamespace = '<portlet:namespace/>';
        $(this).attr('id', pnamespace + this.id);
        $(this).attr('name', pnamespace + this.name);
    });
});

This will put the namespace prefix on every input and select object. 这将在每个输入和选择对象上放置名称空间前缀。 Now your portlet forms are namespaced correctly. 现在,您的portlet表单已正确命名。

As an alternative, you could use the aui tag lib. 作为替代方案,您可以使用aui标签lib。 Note that the generated id will be the portlet name space followed by the name of the element. 请注意,生成的id将是portlet名称空间,后跟元素的名称。

<%@ taglib uri="http://alloy.liferay.com/tld/aui" prefix="aui" %>
<portlet:actionURL var="saveChartURL" name="saveChart" />
<aui:form action='${saveChartURL}' method='post' name='form'>
<aui:button type='button' name='save' value="Save" />
<aui:input id='json'
            type='textarea'
            name='jsonData'
            label=''
            spellcheck='false'  
            cssClass='json'
            style='disply:none;' />
</aui:form>
<script>
(function(run){run(window.jQuery)}(
    function($){
        $(document).ready(function(){
            var pNS = '<portlet:namespace/>';   
            $('#'+pNS+'save').click(function(){      
                var dataserialized = $('#'+pNS+'form').serialize();
                $.post( "${saveChartURL}", dataserialized );
            });
        }); 
    })
);
</script>

And in the Controller.... 在控制器....

@ActionMapping(value="saveChart")
public void saveChart( 
    @RequestParam(defaultValue="{}") String jsonData, 
    @RequestParam(defaultValue="doughnut2d") String chartType,
    PortletPreferences pp) {

    /* TODO do Action */
}

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

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