Skip to content

Commit d271a60

Browse files
committed
Added 'inheritable' attribute to URL actions (#75)
1 parent f2446cb commit d271a60

8 files changed

Lines changed: 66 additions & 18 deletions

File tree

‎core/src/main/java/com/ocpsoft/pretty/faces/annotation/URLAction.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,10 @@ public enum PhaseId
9898
* </p>
9999
*/
100100
PhaseId phaseId() default PhaseId.RESTORE_VIEW;
101+
102+
/**
103+
* Whether child mappings inherit this action or not.
104+
*/
105+
boolean inheritable() default false;
101106

102107
}

‎core/src/main/java/com/ocpsoft/pretty/faces/config/annotation/PrettyAnnotationHandler.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ private void processPrettyActionAnnotation(final URLAction actionAnnotation, fin
408408
ActionSpec actionSpec = new ActionSpec();
409409
actionSpec.setMethod(method);
410410
actionSpec.setOnPostback(actionAnnotation.onPostback());
411+
actionSpec.setInheritable(actionAnnotation.inheritable());
411412
actionSpec.setPhaseId(actionAnnotation.phaseId());
412413

413414
// check which mapping the action belongs to
@@ -483,6 +484,7 @@ public void build(final PrettyConfigBuilder builder)
483484
UrlAction urlAction = new UrlAction();
484485
urlAction.setPhaseId(actionSpec.getPhaseId());
485486
urlAction.setOnPostback(actionSpec.isOnPostback());
487+
urlAction.setInheritable(actionSpec.isInheritable());
486488

487489
// try to get bean name
488490
Class clazz = actionSpec.getMethod().getDeclaringClass();
@@ -645,6 +647,7 @@ private static class ActionSpec
645647
private boolean onPostback;
646648
private PhaseId phaseId;
647649
private String[] mappingIds;
650+
private boolean inheritable;
648651

649652
public boolean isOnPostback()
650653
{
@@ -686,6 +689,16 @@ public void setMappingIds(final String[] mappingIds)
686689
this.mappingIds = mappingIds;
687690
}
688691

692+
public boolean isInheritable()
693+
{
694+
return inheritable;
695+
}
696+
697+
public void setInheritable(boolean inheritable)
698+
{
699+
this.inheritable = inheritable;
700+
}
701+
689702
}
690703

691704
/**

‎core/src/main/java/com/ocpsoft/pretty/faces/config/mapping/UrlAction.java‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class UrlAction
2828
private PrettyExpression action;
2929
private PhaseId phaseId = PhaseId.RESTORE_VIEW;
3030
private boolean onPostback = true;
31+
private boolean inheritable = false;
3132

3233
/**
3334
* Create a new {@link UrlAction} with empty values
@@ -56,6 +57,7 @@ public UrlAction(ActionElement actionElement)
5657
{
5758
phaseId = PhaseId.valueOf(actionElement.getPhaseId().trim().toUpperCase());
5859
}
60+
inheritable = actionElement.isInheritable();
5961
}
6062

6163
/**
@@ -134,6 +136,16 @@ public void setAction(final String action)
134136
this.action = new ConstantExpression(action);
135137
}
136138

139+
public boolean isInheritable()
140+
{
141+
return inheritable;
142+
}
143+
144+
public void setInheritable(boolean inheritable)
145+
{
146+
this.inheritable = inheritable;
147+
}
148+
137149
@Override
138150
public int hashCode()
139151
{
@@ -188,7 +200,7 @@ else if (!phaseId.equals(other.phaseId))
188200
@Override
189201
public String toString()
190202
{
191-
return "UrlAction [action=" + action + ", onPostback=" + onPostback + ", phaseId=" + phaseId + "]";
203+
return "UrlAction [action=" + action + ", onPostback=" + onPostback + ", phaseId=" + phaseId + ", inheritable=" + inheritable + "]";
192204
}
193205

194206
}

‎core/src/main/java/com/ocpsoft/pretty/faces/config/spi/ParentingPostProcessor.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ private void mergeActions(UrlMapping parent, UrlMapping child)
100100
List<UrlAction> result = new ArrayList<UrlAction>();
101101
for (UrlAction action : parent.getActions())
102102
{
103-
if (!result.contains(action))
103+
if (!result.contains(action) && action.isInheritable())
104104
{
105105
result.add(copy(action));
106106
}
@@ -151,7 +151,8 @@ private UrlAction copy(UrlAction urlAction)
151151
UrlAction result = new UrlAction();
152152
result.setAction(urlAction.getAction());
153153
result.setOnPostback(urlAction.onPostback());
154-
result.setPhaseId(result.getPhaseId());
154+
result.setPhaseId(urlAction.getPhaseId());
155+
result.setInheritable(urlAction.isInheritable());
155156
return result;
156157
}
157158

‎core/src/main/resources/META-INF/ocpsoft-pretty-faces-3.3.1.xsd‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,15 @@
370370
<xs:attribute name="onPostback"
371371
type="xs:boolean">
372372
</xs:attribute>
373+
<xs:attribute name="inheritable"
374+
type="xs:boolean" default="false">
375+
<xs:annotation>
376+
<xs:documentation>
377+
Whether child mappings inherit
378+
this action or not.
379+
</xs:documentation>
380+
</xs:annotation>
381+
</xs:attribute>
373382
</xs:extension>
374383
</xs:simpleContent>
375384
</xs:complexType>

‎core/src/test/java/com/ocpsoft/pretty/faces/config/spi/ParentingPostProcessorTest.java‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,9 @@ public void testParentIdInheritsPatterns() throws SAXException, IOException
107107
assertEquals(1, config.getMappingById("grandchild").getPathValidators().get(1).getIndex());
108108

109109
// actions
110-
assertEquals(3, config.getMappingById("grandchild").getActions().size());
110+
assertEquals(2, config.getMappingById("grandchild").getActions().size());
111111
assertEquals("#{myBean.parentAction}", config.getMappingById("grandchild").getActions().get(0).getAction().getELExpression());
112-
assertEquals("#{myBean.childAction}", config.getMappingById("grandchild").getActions().get(1).getAction().getELExpression());
113-
assertEquals("#{myBean.grandchildAction}", config.getMappingById("grandchild").getActions().get(2).getAction().getELExpression());
112+
assertEquals("#{myBean.grandchildAction}", config.getMappingById("grandchild").getActions().get(1).getAction().getELExpression());
114113
assertEquals(2, config.getMappingById("child").getActions().size());
115114
assertEquals("#{myBean.parentAction}", config.getMappingById("child").getActions().get(0).getAction().getELExpression());
116115
assertEquals("#{myBean.childAction}", config.getMappingById("child").getActions().get(1).getAction().getELExpression());
@@ -121,17 +120,17 @@ public void testParentIdInheritsPatterns() throws SAXException, IOException
121120
assertEquals("#{myBean.parentAction}", config.getMappingById("parent").getActions().get(0).getAction().getELExpression());
122121

123122
// query parameters
124-
assertEquals(3, config.getMappingById("grandchild").getActions().size());
123+
assertEquals(3, config.getMappingById("grandchild").getQueryParams().size());
125124
assertEquals("parent", config.getMappingById("grandchild").getQueryParams().get(0).getName());
126125
assertEquals("child", config.getMappingById("grandchild").getQueryParams().get(1).getName());
127126
assertEquals("grandchild", config.getMappingById("grandchild").getQueryParams().get(2).getName());
128-
assertEquals(2, config.getMappingById("child").getActions().size());
127+
assertEquals(2, config.getMappingById("child").getQueryParams().size());
129128
assertEquals("parent", config.getMappingById("child").getQueryParams().get(0).getName());
130129
assertEquals("child", config.getMappingById("child").getQueryParams().get(1).getName());
131-
assertEquals(2, config.getMappingById("child2").getActions().size());
130+
assertEquals(2, config.getMappingById("child2").getQueryParams().size());
132131
assertEquals("parent", config.getMappingById("child2").getQueryParams().get(0).getName());
133132
assertEquals("child2", config.getMappingById("child2").getQueryParams().get(1).getName());
134-
assertEquals(1, config.getMappingById("parent").getActions().size());
133+
assertEquals(1, config.getMappingById("parent").getQueryParams().size());
135134
assertEquals("parent", config.getMappingById("parent").getQueryParams().get(0).getName());
136135

137136
}

‎core/src/test/resources/parenting-pretty-config.xml‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
2-
<pretty-config xmlns="http://ocpsoft.com/prettyfaces/3.1.1"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://ocpsoft.com/prettyfaces/3.1.1
5-
http://ocpsoft.com/xml/ns/prettyfaces/ocpsoft-pretty-faces-3.1.1.xsd">
2+
<pretty-config xmlns="http://ocpsoft.com/prettyfaces/3.3.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
63

74
<url-mapping id="parent">
85
<pattern value="/parent" />
96
<query-param name="parent">#{myBean.parentParam}</query-param>
107
<view-id value="/parent.jsf" />
11-
<action>#{myBean.parentAction}</action>
8+
<action inheritable="true">#{myBean.parentAction}</action>
129
</url-mapping>
1310

1411
<url-mapping id="child" parentId="parent">

‎docs/src/main/docbook/en-US/config/Parent_mappings.xml‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,21 @@
4040
</url-mapping>
4141
]]></programlisting>
4242

43-
At this time, only the pattern and path-parameter
44-
<link linkend="config.validation">validators</link> will be inherited by child mappings;
45-
however, this may be enhanced in future versions.
43+
Child mappings will inherit all properties from the parent mapping. This includes the pattern,
44+
validators and query parameters. URL actions won't be inherited per default. If you want an action
45+
to be inherited by child mappings, set it's <code>inheritable</code> attribute to <code>true</code>.
4646
</para>
47+
48+
<programlisting><![CDATA[<url-mapping id="store">
49+
<pattern value="/store/" />
50+
<view-id value="/faces/shop/store.jsf" />
51+
<action inheritable="true">#{storeBean.someAction}</action>
52+
</url-mapping>
53+
54+
<url-mapping parentId="store" id="category">
55+
<pattern value="/#{category}" />
56+
<view-id value="/faces/shop/category.jsf" />
57+
</url-mapping>
58+
]]></programlisting>
4759

4860
</section>

0 commit comments

Comments
 (0)