简体   繁体   中英

How to process a post request in the Vaadin application?

My task is that I need to process a post request in the Vaadin application This is an example of a request that needs to be processed

I don't understand how in Vaadin I can get POST or GET requests I also found documentation on Creating a servlet 3.0 application - not found for Vaadin v23

@Route("/")
@CssImport("./styles/shared-styles.css")
@CssImport(value = "./styles/vaadin-text-field-styles.css", themeFor = "vaadin-text-field")
@Log4j2
public class MainView extends VerticalLayout {

...

  public MainView() {
   
//    VaadinSession.getCurrent().addRequestHandler(
//        new RequestHandler() {
//          @Override
//          public boolean handleRequest(VaadinSession session,
//              VaadinRequest request,
//              VaadinResponse response)
//              throws IOException {
//            if ("/notifications".equals(request.getPathInfo())) {
//              // Generate a plain text document
//              response.setContentType("text/plain");
//              response.getWriter().append(
//                  "Here's some dynamically generated content.\n");
//              response.getWriter().format(Locale.ENGLISH,
//                  "Time: %Tc\n", new Date());
//
//              // Use shared session data
//              response.getWriter().format("Session data: %s\n",
//                  session.getAttribute("mydata"));
//
//              return true; // We wrote a response
//            } else
//              return false; // No response was written
//          }
//        });
  }


  @WebServlet(value = "/notifications", asyncSupported = true)
  public static class Servlet extends VaadinServlet {
    @Override
    protected void servletInitialized() throws ServletException {
      super.servletInitialized();
      VaadinServlet servlet = getService().getServlet();
    }
  }

When calling http://localhost:8080/notifications I get

Could not navigate to 'notifications'

With the help of the commented code, I manage to get into the request handler, but I doubt that this is the right way

//    VaadinSession.getCurrent().addRequestHandler(
    //        new RequestHandler() {
    //          @Override
    //          public boolean handleRequest(VaadinSession session,
    //              VaadinRequest request,
    //              VaadinResponse response)
    //              throws IOException {
    //            if ("/notifications".equals(request.getPathInfo())) {
    //              // Generate a plain text document
    //              response.setContentType("text/plain");
    //              response.getWriter().append(
    //                  "Here's some dynamically generated content.\n");
    //              response.getWriter().format(Locale.ENGLISH,
    //                  "Time: %Tc\n", new Date());
    //
    //              // Use shared session data
    //              response.getWriter().format("Session data: %s\n",
    //                  session.getAttribute("mydata"));
    //
    //              return true; // We wrote a response
    //            } else
    //              return false; // No response was written
    //          }
    //        });

I also found documentation on Creating a servlet 3.0 application - not found for Vaadin v23

This should still work, so you would map the application to eg http://localhost:8080/app/* so Vaadin would not interfere with http://localhost:8080/notifications

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM