Skip to content

Commit bb3e090

Browse files
author
Igor Polevoy
committed
#280 layout() method in controller does not override getLayout() in controllers
1 parent 0cdc1c4 commit bb3e090

File tree

7 files changed

+77
-2
lines changed

7 files changed

+77
-2
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package app.controllers;
2+
3+
import org.javalite.activeweb.AppController;
4+
5+
/**
6+
* @author Igor Polevoy on 5/13/16.
7+
*/
8+
public class OverrideLayoutController extends AppController {
9+
10+
public void index(){}
11+
12+
public void action(){
13+
render().layout("/layouts/default_layout");
14+
}
15+
16+
17+
@Override
18+
protected String getLayout() {
19+
return "/layouts/override_layout";
20+
}
21+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Copyright 2009-2014 Igor Polevoy
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package app.controllers;
18+
19+
import org.javalite.activeweb.ControllerSpec;
20+
import org.junit.Before;
21+
import org.junit.Test;
22+
23+
/**
24+
* @author Igor Polevoy
25+
*/
26+
public class OverrideLayoutControllerSpec extends ControllerSpec {
27+
28+
@Before
29+
public void before(){
30+
setTemplateLocation("src/test/views");
31+
}
32+
33+
@Test
34+
public void shouldUseControllerLayout(){
35+
request().get("index");
36+
a(responseContent()).shouldBeEqual("Override layout top Index Override layout bottom");
37+
a(layout()).shouldBeEqual("/layouts/override_layout");
38+
}
39+
40+
@Test
41+
public void shouldUseDefaultLayoutFromAction(){
42+
request().get("action");
43+
a(responseContent()).shouldBeEqual("override");
44+
a(layout()).shouldBeEqual("/layouts/default_layout");
45+
}
46+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Override layout top ${page_content} Override layout bottom
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
override
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Index

activeweb/src/main/java/org/javalite/activeweb/ControllerRunner.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,7 @@ private void renderResponse(Route route, boolean integrateViews) throws Instant
151151
//this is configuration of explicit response. If render() method was called in controller, we already have instance of
152152
// response on current thread.
153153
private void configureExplicitResponse(Route route, String controllerLayout, RenderTemplateResponse resp) throws InstantiationException, IllegalAccessException {
154-
String responseLayout = resp.getLayout();
155-
if(!Configuration.getDefaultLayout().equals(controllerLayout) && Configuration.getDefaultLayout().equals(responseLayout)){
154+
if(!Configuration.getDefaultLayout().equals(controllerLayout) && resp.hasDefaultLayout()){
156155
resp.setLayout(controllerLayout);
157156
}
158157
if(resp.getContentType() == null){

activeweb/src/main/java/org/javalite/activeweb/RenderTemplateResponse.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class RenderTemplateResponse extends ControllerResponse{
2626
private String template, format;
2727
private String layout = Configuration.getDefaultLayout();
2828
private TemplateManager templateManager;
29+
private boolean defaultLayout = true;
2930

3031
/**
3132
* Constructs a response object for rendering pages. This can be used for regular responses.
@@ -70,6 +71,11 @@ public Map values(){
7071

7172
public void setLayout(String layout) {
7273
this.layout = layout;
74+
this.defaultLayout = false; // in some bizarre cases, when you need default_layout set manually inside action!
75+
}
76+
77+
public boolean hasDefaultLayout(){
78+
return defaultLayout;
7379
}
7480

7581
protected void setTemplateManager(TemplateManager templateManager){

0 commit comments

Comments
 (0)