如果我将URL键入为localhost:8080/MyApp/loginAction.action?username=raj&password=123,我将如何获取拦截器/动作类中的url?我尝试了一些请求对象的方法,但没有得到。用过的request.getRequestURL()request.getQueryString() ....等

最佳答案

您可以使用HttpServletRequest.getRequestURL()获取不包含参数的完整URL("?foo=bar"部分,即QueryString)。

您可以使用?获取QueryString(如果有的话)(并且不带HttpServletRequest.getQueryString()符号)。



在INTERCEPTOR中,从ActionContext获取请求:

public String intercept(ActionInvocation invocation) throws Exception {
    final ActionContext context = invocation.getInvocationContext();
    HttpServletRequest request = (HttpServletRequest)context.get(StrutsStatics.HTTP_REQUEST);

    String url = request.getRequestURL();
    String queryString = request.getQueryString();

    String fullUrl = url + (queryString==null ? "" : ("?" + queryString));

    LOG.debug(fullUrl);

    return invocation.invoke();
}




在ACTION中,从ServletRequestAware获取请求:

public class MyAction implements ServletRequestAware {
    private javax.servlet.http.HttpServletRequest request;

    public void setServletRequest(javax.servlet.http.HttpServletRequest request){
        this.request = request;
    }

    public String execute(){
        String url = request.getRequestURL();
        String queryString = request.getQueryString();

        String fullUrl = url + (queryString==null ? "" : ("?" + queryString));

        LOG.debug(fullUrl);

        return Action.SUCCESS;
    }
}

10-05 22:48