Skip to content

Commit

Permalink
More tags, example with listing and correction in the required VRapto…
Browse files Browse the repository at this point in the history
…r version
  • Loading branch information
luiz committed Dec 18, 2010
1 parent 344fe05 commit 6788bf3
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<dependency>
<groupId>br.com.caelum</groupId>
<artifactId>vraptor</artifactId>
<version>3.2</version>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/br/com/caelum/vraptor/html/PageTagFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import br.com.caelum.vraptor.html.tags.Body;
import br.com.caelum.vraptor.html.tags.Head;
import br.com.caelum.vraptor.html.tags.Html;
import br.com.caelum.vraptor.html.tags.Li;
import br.com.caelum.vraptor.html.tags.Ol;
import br.com.caelum.vraptor.html.tags.P;
import br.com.caelum.vraptor.html.tags.Span;
import br.com.caelum.vraptor.html.tags.Tag;
Expand Down Expand Up @@ -128,4 +130,34 @@ public static Span span(String content) {
public static Span span(Attributes attributes, String content) {
return new Span(attributes, new Text(content));
}
public static Li li(Tag... children) {
return new Li(new Attributes(), children);
}

public static Li li(Attributes attributes, Tag... children) {
return new Li(attributes, children);
}

public static Li li(String content) {
return new Li(new Attributes(), new Text(content));
}

public static Li li(Attributes attributes, String content) {
return new Li(attributes, new Text(content));
}
public static Ol ol(Tag... children) {
return new Ol(new Attributes(), children);
}

public static Ol ol(Attributes attributes, Tag... children) {
return new Ol(attributes, children);
}

public static Ol ol(String content) {
return new Ol(new Attributes(), new Text(content));
}

public static Ol ol(Attributes attributes, String content) {
return new Ol(attributes, new Text(content));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@
*/
package br.com.caelum.vraptor.html.example;

import java.util.Arrays;
import java.util.List;

import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.html.Page;
import br.com.caelum.vraptor.html.PageProcessor;
import br.com.caelum.vraptor.html.example.page.IndexPage;
import br.com.caelum.vraptor.html.example.page.ListPage;
import br.com.caelum.vraptor.view.Results;

@Resource
Expand All @@ -42,6 +46,12 @@ public Page index() {
return null;
}

@Path("/listing")
public void listing() {
List<String> cars = Arrays.asList("GM", "Ford", "VW");
result.use(Results.http()).body(pageProcessor.process(new ListPage(cars)));
}

@Path("/complex/route/to/page2")
public void page2() {
result.use(Results.http()).body("Voila!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class IndexPage implements Page {

public Html render() {
Link page2 = link(); to(ExampleController.class).page2();
Link table = link(); to(ExampleController.class).listing();
Link google = link("http://google.com");

return html(
Expand All @@ -33,6 +34,10 @@ public Html render() {
a(attrs(href(page2)), "complex link"),
span(" to another page")
),
p(
text("To "),
a(attrs(href(table)), "a page with a table")
),
p(
text("And for "),
a(attrs(href(google)), "Google")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package br.com.caelum.vraptor.html.example.page;
import static br.com.caelum.vraptor.html.PageTagFactory.body;
import static br.com.caelum.vraptor.html.PageTagFactory.html;
import static br.com.caelum.vraptor.html.PageTagFactory.li;
import static br.com.caelum.vraptor.html.PageTagFactory.ol;

import java.util.List;

import br.com.caelum.vraptor.html.Page;
import br.com.caelum.vraptor.html.tags.Html;
import br.com.caelum.vraptor.html.tags.Tags;

public class ListPage implements Page {

private final List<String> cars;

public ListPage(List<String> cars) {
this.cars = cars;
}

@Override
public Html render() {
return html(
body(
ol(
cars()
)
)
);
}

// private Tag toTag(String car) {
// ...;
// }

private Tags cars() {
//Tags.foreach(cars).use(TablePage.class).toTag(null);


Tags tags = new Tags();
for (String car : cars) {
tags.append(li(car));
}
return tags;
}

}
25 changes: 25 additions & 0 deletions src/main/java/br/com/caelum/vraptor/html/tags/Li.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package br.com.caelum.vraptor.html.tags;

import br.com.caelum.vraptor.html.attributes.Attributes;

public class Li implements Tag {
private final Attributes attributes;
private final Tag[] children;

public Li(Attributes attributes, Tag... children) {
this.attributes = attributes;
this.children = children;

}

@Override
public Tag[] getChildren() {
return children;
}

@Override
public Attributes getAttributes() {
return attributes;
}

}
21 changes: 21 additions & 0 deletions src/main/java/br/com/caelum/vraptor/html/tags/Ol.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package br.com.caelum.vraptor.html.tags;

import br.com.caelum.vraptor.html.attributes.Attributes;

public class Ol implements Tag {
private final Tag[] children;
private final Attributes attributes;

public Ol(Attributes attributes, Tag... children) {
this.attributes = attributes;
this.children = children;
}

public Attributes getAttributes() {
return this.attributes;
}

public Tag[] getChildren() {
return this.children;
}
}
26 changes: 26 additions & 0 deletions src/main/java/br/com/caelum/vraptor/html/tags/Tags.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package br.com.caelum.vraptor.html.tags;

import java.util.LinkedList;
import java.util.List;

import br.com.caelum.vraptor.html.attributes.Attributes;

public class Tags implements Tag {

private final List<Tag> children = new LinkedList<Tag>();

@Override
public Tag[] getChildren() {
return children.toArray(new Tag[0]);
}

@Override
public Attributes getAttributes() {
return new Attributes();
}

public void append(Tag child) {
children.add(child);
}

}

0 comments on commit 6788bf3

Please sign in to comment.