|
| 1 | +/* |
| 2 | + * Copyright 2000-2024 Vaadin Ltd. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * 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, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.vaadin.flow; |
| 18 | + |
| 19 | +import com.vaadin.flow.component.UI; |
| 20 | +import com.vaadin.flow.component.html.Div; |
| 21 | +import com.vaadin.flow.component.html.NativeButton; |
| 22 | +import com.vaadin.flow.component.page.Page; |
| 23 | +import com.vaadin.flow.router.BeforeEnterEvent; |
| 24 | +import com.vaadin.flow.router.BeforeEnterObserver; |
| 25 | +import com.vaadin.flow.router.Route; |
| 26 | + |
| 27 | +@Route("com.vaadin.flow.AddQueryParamView") |
| 28 | +public class AddQueryParamView extends Div { |
| 29 | + |
| 30 | + public static final String PARAM_BUTTON_ID = "setParameter"; |
| 31 | + public static final String QUERY_ID = "query"; |
| 32 | + |
| 33 | + public AddQueryParamView() { |
| 34 | + NativeButton button = new NativeButton("Add URL Parameter", e -> { |
| 35 | + updateUrlRequestParameter("test", "HELLO!"); |
| 36 | + }); |
| 37 | + button.setId(PARAM_BUTTON_ID); |
| 38 | + add(button); |
| 39 | + } |
| 40 | + |
| 41 | + public void updateUrlRequestParameter(String key, String value) { |
| 42 | + Page page = UI.getCurrent().getPage(); |
| 43 | + page.fetchCurrentURL(url -> { |
| 44 | + String newLocation = url + "?" + key + "=" + value; |
| 45 | + page.getHistory().replaceState(null, newLocation); |
| 46 | + Div div = new Div(newLocation); |
| 47 | + div.setId(QUERY_ID); |
| 48 | + add(div); |
| 49 | + }); |
| 50 | + } |
| 51 | +} |
0 commit comments