有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从Struts操作的钩子更改表单值(Liferay 6.2)

我只是想改变一个来自表单的值。我挂接了一个Struts Action来更改该值,然后继续使用默认的processAction。但它没有应用这些变化

这是我的钩子代码:

public class EditRecordDisplayPortletAction extends BaseStrutsPortletAction {
    ...

    public void processAction(
            StrutsPortletAction originalStrutsPortletAction,
            PortletConfig portletConfig, ActionRequest actionRequest,
            ActionResponse actionResponse)
        throws Exception {
        ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

        ...
        //Stuff that is working here ...
        ...

        //Here I change the value that is comming from the form that should be used later:
        DynamicActionRequest dynamicActionRequest = null;
        Map<String, String[]> params = new HashMap();

        String observationInputKey = getObservationInputKey();
        params.put(observationInputKey, new String[]{"Here goes some text I have calculated in the hook."});
            dynamicActionRequest = new DynamicActionRequest(actionRequest, params, false);
        //Continues with the normal operation, but it's not using the new values:
        originalStrutsPortletAction.processAction(
            originalStrutsPortletAction, portletConfig, dynamicActionRequest,
            actionResponse);
    }
    ...

我正在使用DynamicActionRequest,因为我无法更改__actionRequest__。想了解更多信息,这里有人解释了一些类似的东西,应该是有效的: Wrap actionRequest with a DynamicActionRequest


共 (1) 个答案

  1. # 1 楼答案

    按照您链接的论坛帖子的示例,您可以更简单地创建DynamicActionRequest。请尝试一下:

    public void processAction(
            StrutsPortletAction originalStrutsPortletAction,
            PortletConfig portletConfig, ActionRequest actionRequest,
            ActionResponse actionResponse)
        throws Exception {
        ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
    
        ...
        //Stuff that is working here ...
        ...
    
        DynamicActionRequest dynamicActionRequest = new DynamicActionRequest(actionRequest);
    
        String observationInputKey = getObservationInputKey();
        dynamicActionRequest.setParameter(observationInputKey, "Here goes some text I have calculated in the hook.");
    
        //Continues with the normal operation, but it's not using the new values:
        originalStrutsPortletAction.processAction(
            originalStrutsPortletAction, portletConfig, dynamicActionRequest,
            actionResponse);
    }
    ...
    

    当然,javadoc for DynamicActionRequest只包含方法名,但是您将布尔参数inherit指定给构造函数作为false。那可能是你唯一的问题