Gate. Matching a type safe page constructor with a readable URL.

I am using Wicket 6.1 and I am trying to figure out how to make my page streams more attractive, more classified URLs.

I use safe type constructors a lot, so my code will look something like this:

class SearchResultsPage extends WebPage { public SearchResultsPage(SearchResultsModel model) { // display the data in the model } } 

To go to the page, I have this code:

 AjaxButton ajaxButton = new IndicatingAjaxButton("ajaxbutton") { @Override protected void onSubmit(AjaxRequestTarget target, Form<?> form) { SearchResultsModel results = search(kriteriamodel) getRequestCycle().setResponsePage(new SearchResultsPage(results)); } @Override protected void onError(AjaxRequestTarget target, Form<?> form) { target.add(feedback); } }; 

Now, when I get to the results page, will my URL look something like searchresults? 5

I would like to be able to encode the search parameters into a URL that the user could add for a subsequent referral.

I know that I can use the PageParameters map and manually map my objects to and from this structure. But I'm looking for a more elegant solution.

  • I'm looking for pointers to some kind of smart interface that my page or model needs to implement in order to make the page more bookmarked.
  • An example would be nice.
  • Ideally, only the code in Page / Model should be changed.

Thanks.

+4
source share
1 answer

What I would do is something like this:

 class SearchResultsPage extends WebPage { public static PageParameters convertModelToParam(SearchResultsModel model) { ... } public static SearchResultModel convertParamToModel( PageParameters param ) { //this is for convenience only ... } public SearchResultsPage( PageParameters param ) { this( convertParamToModel( param ) ); } private SearchResultsPage(SearchResultsModel model) { // display the data in the model } } 

Thus, your page will become bookmarked, but you will be able to keep your type designers safe. The obvious drawback is that when you have a constructor that accepts PageParameters , there is no way to make sure that it is derived from the appropriate conversion method. But when you think about it, this is the exact model of the real world: users can freely edit the query string in your URL, and your application should be able to handle it.

Remember that after the page can be bookmarked, the parameters become user-defined, therefore they must be completely cleared before converting them into your business objects. No automatic system will do this for you.

+2
source

Source: https://habr.com/ru/post/1441839/


All Articles