• No results found

SOLUTIONS TO EXERCISES Chapter Basics

In document Wicket Application Development (Page 39-45)

SIX

SOLUTIONS TO EXERCISES

Chapter Basics

Exercise Add a link from the movie list page to the home page. MovieListPage.html

1 <!DOCTYPE html>

2 <html xmlns:wicket="http://wicket.apache.org"> 3 <head>

4 <meta charset="utf-8" />

5 <title>MovieDB - Movie List</title>

6 <link rel="stylesheet" href="style.css" /> 7 </head>

8 <body>

9 <h2>Movie List</h2> 10

11 <ul>

12 <li><a href="#" wicket:id="home">Home</a></li> 13 </ul>

14 </body> 15 </html>

MovieListPage.java

1 package wicket.quickstart; 2

3 import org.apache.wicket.markup.html.WebPage; 4 import org.apache.wicket.markup.html.link.Link; 5

6 public class MovieListPage extends WebPage { 7 public MovieListPage() {

8 Link homePageLink = new Link("home") {

9 @Override

10 public void onClick() {

11 this.setResponsePage(new HomePage());

12 }

13 };

14 this.add(homePageLink);

15 }

16 }

Note: You can see the changes needed to solve this exercise at the address: https://pikacode.com/uyar/wicket- application-development/commit/eda07d059353. You can get a copy of this version by clicking on the download link on that page.

Chapter Application Structure

Exercise Arrange the movie list page so that it will use the same navigation panel and footer. MovieListPage.html

1 <!DOCTYPE html>

2 <html xmlns:wicket="http://wicket.apache.org"> 3 <head>

4 <meta charset="utf-8" />

5 <title>MovieDB - Movie List</title>

6 <link rel="stylesheet" href="style.css" /> 7 </head> 8 <body> 9 <header> 10 <nav wicket:id="mainNavigation"> 11 navigation links 12 </nav> 13 </header> 14 15 <h2>Movie List</h2> 16 17 <footer>

18 <div id="datetime" wicket:id="datetime">

19 Wed Sep 4 15:32:40 EEST 2013

20 </div> 21 </footer> 22 </body> 23 </html>

MovieListPage.java

1 package wicket.quickstart; 2

3 public class MovieListPage extends BasePage { 4 public MovieListPage() {

5 }

6 }

Note: You can see the changes needed to solve this exercise at the address: https://pikacode.com/uyar/wicket- application-development/commit/bc187a53483d. You can get a copy of this version by clicking on the download link on that page.

Chapter Data Model

Exercise Add a page that will display a movie. Then, organize the movie list page so that the entries are links to pages that will display the selected movie.

MovieDisplayPage.html

1 <!DOCTYPE html>

2 <html xmlns:wicket="http://wicket.apache.org"> 3 <head>

4 <meta charset="utf-8" /> 5 <title>MovieDB - Movie</title>

6 <link rel="stylesheet" href="style.css" /> 7 </head>

8 <body>

Wicket Application Development, Release 1.1 9 <header> 10 <nav wicket:id="mainNavigation"> 11 navigation links 12 </nav> 13 </header> 14

15 <h2 wicket:id="title">The Matrix</h2> 16 <table> 17 <tr> 18 <th>Year:</th> 19 <td wicket:id="year">1999</td> 20 </tr> 21 </table> 22 23 <footer>

24 <div id="datetime" wicket:id="datetime">

25 Wed Sep 4 15:32:40 EEST 2013

26 </div> 27 </footer> 28 </body> 29 </html>

MovieDisplayPage.java

1 package wicket.quickstart; 2

3 import org.apache.wicket.markup.html.basic.Label; 4

5 public class MovieDisplayPage extends BasePage { 6 public MovieDisplayPage(Movie movie) {

7 this.add(new Label("title", movie.getTitle()));

8 this.add(new Label("year", movie.getYear().toString()));

9 }

10 }

MovieDisplayPageLink.java

1 package wicket.quickstart; 2

3 import org.apache.wicket.markup.html.link.Link; 4

5 public class MovieDisplayPageLink extends Link { 6 private Movie _movie;

7

8 public MovieDisplayPageLink(String id, Movie movie) { 9 super(id);

10 this._movie = movie;

11 }

12

13 @Override

14 public void onClick() {

15 this.setResponsePage(new MovieDisplayPage(this._movie));

16 } 17 } MovieListPage.html 1 <!DOCTYPE html> 2 <html xmlns:wicket="http://wicket.apache.org"> 37

3 <head>

4 <meta charset="utf-8" />

5 <title>MovieDB - Movie List</title>

6 <link rel="stylesheet" href="style.css" /> 7 </head> 8 <body> 9 <header> 10 <nav wicket:id="mainNavigation"> 11 navigation links 12 </nav> 13 </header> 14 15 <h2>Movie List</h2> 16 17 <table> 18 <tr wicket:id="movie_list"> 19 <td>

20 <a href="#" wicket:id="movie_link">

21 <span wicket:id="title">The Matrix</span> 22 (<span wicket:id="year">1999</span>) 23 </a> 24 </td> 25 </tr> 26 </table> 27 28 <footer>

29 <div id="datetime" wicket:id="datetime">

30 Wed Sep 4 15:32:40 EEST 2013

31 </div> 32 </footer> 33 </body> 34 </html>

MovieListPage.java

1 package wicket.quickstart; 2 3 import java.util.List; 4 5 import org.apache.wicket.markup.html.basic.Label; 6 import org.apache.wicket.markup.html.list.ListItem; 7 import org.apache.wicket.markup.html.list.PropertyListView; 8

9 public class MovieListPage extends BasePage { 10 public MovieListPage() {

11 WicketApplication app = (WicketApplication) this.getApplication(); 12 MovieCollection collection = app.getCollection();

13 List<Movie> movies = collection.getMovies(); 14

15 PropertyListView movieListView =

16 new PropertyListView("movie_list", movies) {

17 @Override

18 protected void populateItem(ListItem item) {

19 Movie movie = (Movie) item.getModelObject();

20 MovieDisplayPageLink movieLink =

21 new MovieDisplayPageLink("movie_link", movie); 22 movieLink.add(new Label("title"));

23 movieLink.add(new Label("year"));

24 item.add(movieLink); }

Wicket Application Development, Release 1.1

25 };

26 this.add(movieListView);

27 }

28 }

Note: You can see the changes needed to solve this exercise at the address: https://pikacode.com/uyar/wicket- application-development/commit/37f420ee88e2. You can get a copy of this version by clicking on the download link on that page.

Chapter Forms

Exercise Add a link to the movie display page which, when clicked, will take the user to the movie edit page. After saving, the movie should be updated in the collection (not added a second time). MovieCollection.java

1 package wicket.quickstart; 2

3 import java.util.LinkedList; 4 import java.util.List; 5

6 public class MovieCollection { 7 private List<Movie> _movies; 8

9 public MovieCollection() {

10 this._movies = new LinkedList<Movie>();

11 }

12

13 public List<Movie> getMovies() { 14 return this._movies;

15 }

16

17 public void addMovie(Movie movie) { 18 this._movies.add(movie);

19 }

20

21 public void deleteMovie(Movie movie) { 22 this._movies.remove(movie);

23 }

24

25 public void updateMovie(Movie movie) {

26 }

27 }

MovieEditForm.java

1 package wicket.quickstart; 2

3 import org.apache.wicket.markup.html.form.Form; 4 import org.apache.wicket.markup.html.form.TextField; 5 import org.apache.wicket.model.CompoundPropertyModel; 6

7 public class MovieEditForm extends Form { 8 private boolean _newMovie;

9

10 public MovieEditForm(String id, Movie movie, boolean newMovie) { 11 super(id);

12

13 CompoundPropertyModel model = new CompoundPropertyModel(movie); 14 this.setModel(model);

15

16 this.add(new TextField("title")); 17 this.add(new TextField("year")); 18

19 this._newMovie = newMovie;

20 }

21

22 @Override

23 public void onSubmit() {

24 Movie movie = (Movie) this.getModelObject();

25 WicketApplication app = (WicketApplication) this.getApplication(); 26 MovieCollection collection = app.getCollection();

27 if (this._newMovie)

28 collection.addMovie(movie);

29 else

30 collection.updateMovie(movie);

31 this.setResponsePage(new MovieDisplayPage(movie));

32 }

33 }

MovieEditPage.java

1 package wicket.quickstart; 2

3 public final class MovieEditPage extends BasePage { 4 public MovieEditPage(Movie movie) {

5 this.add(new MovieEditForm("movie_edit", movie, true));

6 }

7

8 public MovieEditPage(Movie movie, boolean newMovie) {

9 this.add(new MovieEditForm("movie_edit", movie, newMovie));

10 } 11 } MovieDisplayPage.html 1 <!DOCTYPE html> 2 <html xmlns:wicket="http://wicket.apache.org"> 3 <head> 4 <meta charset="utf-8" /> 5 <title>MovieDB - Movie</title>

6 <link rel="stylesheet" href="style.css" /> 7 </head> 8 <body> 9 <header> 10 <nav wicket:id="mainNavigation"> 11 navigation links 12 </nav> 13 </header> 14

15 <h2 wicket:id="title">The Matrix</h2> 16 <table> 17 <tr> 18 <th>Year:</th> 19 <td wicket:id="year">1999</td> 20 </tr> 21 </table>

Wicket Application Development, Release 1.1

22

23 <p><a href="#" wicket:id="edit_link">Edit</a></p> 24

25 <footer>

26 <div id="datetime" wicket:id="datetime">

27 Wed Sep 4 15:32:40 EEST 2013

28 </div> 29 </footer> 30 </body> 31 </html>

MovieDisplayPage.java

1 package wicket.quickstart; 2

3 import org.apache.wicket.markup.html.basic.Label; 4 import org.apache.wicket.markup.html.link.Link; 5

6 public class MovieDisplayPage extends BasePage { 7 private Movie _movie;

8

9 public MovieDisplayPage(Movie movie) { 10 this._movie = movie;

11

12 this.add(new Label("title", movie.getTitle()));

13 this.add(new Label("year", movie.getYear().toString())); 14

15 Link editLink = new Link("edit_link") {

16 @Override

17 public void onClick() {

18 MovieDisplayPage parent = (MovieDisplayPage) this.getParent(); 19 this.setResponsePage(new MovieEditPage(parent.getMovie(), false));

20 }

21 };

22 this.add(editLink);

23 }

24

25 public Movie getMovie() { 26 return this._movie;

27 }

28 }

Note: You can see the changes needed to solve this exercise at the address: https://pikacode.com/uyar/wicket- application-development/commit/1f8b53f98f79. You can get a copy of this version by clicking on the download link on that page.

In document Wicket Application Development (Page 39-45)

Related documents