Search code examples
spring-bootvaadinvaadin7

RequestMapping in Sprint-boot VAADIN application


I have a Spring-boot VAADIN application with main classes as follows

Application Class

@SpringBootApplication
public class MySpringBootApplication {

    public static void main(String[] args) {
        SpringApplication.run(MySpringBootApplication.class, args);
    }
}

VAADIN-UI Class

@Theme("valo")
@SpringUI
public class MyAppUI extends UI {

    @Autowired
    private SpringViewProvider viewProvider;

    @Override
    protected void init(VaadinRequest vaadinRequest) {

        final VerticalLayout mainLayout = new VerticalLayout();
        setContent(mainLayout);

        Navigator navigator = new Navigator(this, mainLayout);
        navigator.addProvider(viewProvider);
    }
}

VAADIN-View Class

@SpringView(name = "")
public class MyAppView extends VerticalLayout implements View {

    @PostConstruct
    void init() {
        // Some logic here
    }

    @Override
    public void enter(ViewChangeListener.ViewChangeEvent event) {
        // Some logic here
    }
}

Currently, the application handles the request in root URL i.e. say http://localhost:8080/. But I want the application to handle requests when a parameter is supplied by http://localhost:8080/<parameter_value>. How can I achieve this?

The logic I have to execute is the same in both cases i.e. I want MyAppView to process both root URL request and the one with a parameter value.


Solution

  • VAADIN's getUI().getPage().getLocation().getPath() method can be used to get the parameter value from the URL. This will give everything from '/' in the URL. Sample code is given below:

    VAADIN-View Class

    @SpringView(name = "")
    public class MyAppView extends VerticalLayout implements View {
    
        @PostConstruct
        void init() {
            // Some logic here
        }
    
        @Override
        public void enter(ViewChangeListener.ViewChangeEvent event) {
           // Remove spaces and also initial '/' in the path
           String uriParamValue = getUI().getPage().getLocation().getPath().trim().substring(1);
           // Do processing with uriParamValue
        }
    }