Spring/Java web:我怎么知道一个请求是一个表单帖子?

Spring/Java web: how can I know a request is a form post?

我正在做一个 Spring 网站。该网站有许多访问者可以获得的页面。它还允许访问者填写和提交一些表格。

我想使用 Spring 拦截器(作为中心位置)来检查来自访问者的每个 Web 请求。如何确定网络请求是获取请求还是表单发布?

感谢和问候!

您可以按如下方式使用 getMethod() -

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class MyInterceptor extends HandlerInterceptorAdapter {
    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {
        System.out.println("Request Intercepted for : "
                + request.getRequestURI());
        System.out.println("Request type : "
                + request.getMethod());
        return true;
    }
}

表格documentation

java.lang.String getMethod()

Returns the name of the HTTP method with which this request was made, for example, GET, POST, or PUT. Same as the value of the CGI variable REQUEST_METHOD.

Returns: a String specifying the name of the method with which this request was made