Author: nilsga Date: Tue Jan 8 00:18:23 2008 New Revision: 609901 URL: http://svn.apache.org/viewvc?rev=609901&view=rev Log: WW-2370 Using the name of the current executing action in the url when no action is specified.
Modified: struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/components/PortletUrlRenderer.java struts/struts2/trunk/plugins/portlet/src/test/java/org/apache/struts2/views/jsp/PortletUrlTagTest.java Modified: struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/components/PortletUrlRenderer.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/components/PortletUrlRenderer.java?rev=609901&r1=609900&r2=609901&view=diff ============================================================================== --- struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/components/PortletUrlRenderer.java (original) +++ struts/struts2/trunk/plugins/portlet/src/main/java/org/apache/struts2/components/PortletUrlRenderer.java Tue Jan 8 00:18:23 2008 @@ -20,6 +20,8 @@ */ package org.apache.struts2.components; +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.util.TextUtils; import org.apache.struts2.StrutsException; import org.apache.struts2.portlet.util.PortletUrlHelper; @@ -46,11 +48,14 @@ } String result; - if (urlComponent.value == null && urlComponent.action != null) { + if (onlyActionSpecified(urlComponent)) { result = PortletUrlHelper.buildUrl(urlComponent.action, urlComponent.namespace, urlComponent.method, urlComponent.parameters, urlComponent.portletUrlType, urlComponent.portletMode, urlComponent.windowState); - } else { + } else if(onlyValueSpecified(urlComponent)){ result = PortletUrlHelper.buildResourceUrl(urlComponent.value, urlComponent.parameters); } + else { + result = createDefaultUrl(urlComponent); + } if ( urlComponent.anchor != null && urlComponent.anchor.length() > 0 ) { result += '#' + urlComponent.anchor; } @@ -71,14 +76,34 @@ } } + private String createDefaultUrl(URL urlComponent) { + String result; + ActionInvocation ai = (ActionInvocation)urlComponent.getStack().getContext().get( + ActionContext.ACTION_INVOCATION); + String action = ai.getProxy().getActionName(); + result = PortletUrlHelper.buildUrl(action, urlComponent.namespace, urlComponent.method, urlComponent.parameters, urlComponent.portletUrlType, urlComponent.portletMode, urlComponent.windowState); + return result; + } + + private boolean onlyValueSpecified(URL urlComponent) { + return urlComponent.value != null && urlComponent.action == null; + } + + private boolean onlyActionSpecified(URL urlComponent) { + return urlComponent.value == null && urlComponent.action != null; + } + /** * [EMAIL PROTECTED] */ public void renderFormUrl(Form formComponent) { String action = null; if (formComponent.action != null) { - // if it isn't specified, we'll make somethig up action = formComponent.findString(formComponent.action); + } + else { + ActionInvocation ai = (ActionInvocation) formComponent.getStack().getContext().get(ActionContext.ACTION_INVOCATION); + action = ai.getProxy().getActionName(); } String type = "action"; Modified: struts/struts2/trunk/plugins/portlet/src/test/java/org/apache/struts2/views/jsp/PortletUrlTagTest.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/portlet/src/test/java/org/apache/struts2/views/jsp/PortletUrlTagTest.java?rev=609901&r1=609900&r2=609901&view=diff ============================================================================== --- struts/struts2/trunk/plugins/portlet/src/test/java/org/apache/struts2/views/jsp/PortletUrlTagTest.java (original) +++ struts/struts2/trunk/plugins/portlet/src/test/java/org/apache/struts2/views/jsp/PortletUrlTagTest.java Tue Jan 8 00:18:23 2008 @@ -20,7 +20,6 @@ */ package org.apache.struts2.views.jsp; -import java.lang.reflect.Field; import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; @@ -48,11 +47,14 @@ import com.mockobjects.servlet.MockJspWriter; import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.ActionInvocation; +import com.opensymphony.xwork2.ActionProxy; import com.opensymphony.xwork2.util.ValueStack; import com.opensymphony.xwork2.util.ValueStackFactory; /** */ [EMAIL PROTECTED]("unchecked") public class PortletUrlTagTest extends MockObjectTestCase { URLTag tag = new URLTag(); @@ -84,8 +86,6 @@ du.init(); Dispatcher.setInstance(du); - mockPortletApiAvailable(); - stack = du.getContainer().getInstance(ValueStackFactory.class).createValueStack(); stack.getContext().put(ActionContext.CONTAINER, du.getContainer()); ActionContext.setContext(new ActionContext(stack.getContext())); @@ -138,21 +138,6 @@ ActionContext.setContext(ctx); } - /** - * - */ - private void mockPortletApiAvailable() { - try { - Field field = Dispatcher.class.getDeclaredField("portletSupportActive"); - field.setAccessible(true); - field.set(null, Boolean.TRUE); - } - catch(Exception e) { - - } - - } - public void testEnsureParamsAreStringArrays() { Map params = new HashMap(); params.put("param1", "Test1"); @@ -324,6 +309,31 @@ tag.setMethod("input"); tag.doStartTag(); tag.doEndTag(); + } + + public void testUrlWithNoActionOrMethod() throws Exception { + PortletMode mode = PortletMode.VIEW; + mockHttpReq.stubs().method("getQueryString").will(returnValue("")); + mockPortletRes.expects(once()).method("createRenderURL").will( + returnValue((PortletURL) mockPortletUrl.proxy())); + Map paramMap = new HashMap(); + + Mock mockActionProxy = mock(ActionProxy.class); + mockActionProxy.stubs().method("getActionName").will(returnValue("currentExecutingAction")); + final ActionProxy proxy = (ActionProxy)mockActionProxy.proxy(); + + Mock mockActionInvocation = mock(ActionInvocation.class); + mockActionInvocation.stubs().method("getProxy").will(returnValue(proxy)); + ActionInvocation ai = (ActionInvocation)mockActionInvocation.proxy(); + + stack.getContext().put(ActionContext.ACTION_INVOCATION, ai); + paramMap.put(PortletActionConstants.ACTION_PARAM, new String[]{"/view/currentExecutingAction"}); + paramMap.put(PortletActionConstants.MODE_PARAM, new String[]{mode.toString()}); + mockPortletUrl.expects(once()).method("setParameters").with(new ParamMapConstraint(paramMap)); + mockPortletUrl.expects(once()).method("setPortletMode").with(eq(PortletMode.VIEW)); + mockPortletUrl.expects(once()).method("setWindowState").with(eq(WindowState.NORMAL)); + tag.doStartTag(); + tag.doEndTag(); } private static class ParamMapConstraint implements Constraint {