Announcement Announcement Module
No announcement yet.
Add parameters to the current request Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Add parameters to the current request

    Is there any way of adding parameters to a request in an interceptor prehandle method? I need to check if a paramter exist and if not add it. I am trying to avoid the browser knowing about it with a re-direct. Thank you.

    BTW: I am referring to the info after the '?' mark.

  • #2
    If you want to add parameters you need to generate request wrapper. Look at HttServletRequestWrapper.


    • #3
      Originally posted by tv
      If you want to add parameters you need to generate request wrapper. Look at HttServletRequestWrapper.
      Not sure how that will help since there is no setter method in the HttpServletRequestWrapper. To use this you would have to override certain methods such as getQueryString() which seems a bit too much to just add a name=value string parameter.


      • #4
        I would like to do the same. Is it possible?

        (to add parameters to a request in the preHandle method of an interceptor)


        • #5
          You can add all request parameters to request Attributes, and then you can work only with Attributes.



          • #6
            I think what tv was suggesting is to override the getParameter method on HttpServletRequestWrapper.

            For example:

            public String getParameter ( String paramName ) {
               // We care about the param1 parameter.
               // If it doesn't exist then return 'someDefaultValue'
               if ( "param1".equals ( paramName ) ) {
                  String paramValue = super.getParameter ( paramName );
                  if ( null == paramValue )
                     return "someDefaultValue";
                     return paramValue;
                  return super.getParameter ( paramName );
            There isn't a setRequestParameter because parameters, AFAIK, are immutable in the servlet API and because different containers implements it in different ways.



            • #7
              Yes, but what about if have different default parameters values, base on request ?
              You don't need to override HttpServletRequestWrapper.getParameter() method.
              The more natural way is to create HandlerInterceptor, and in the preHandle() method to move all parameters to attributes.


              • #8
                I agree with other posts in using preHandle in an interceptor where request.setAttribute can be called to add parameters. Example:

                public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

                String rule = request.getParameter("rule");
                Document doc = (Document)WebUtils.getSessionAttribute(request, "navigation");

                if (rule == null) {
                rule = request.getRequestURI();

                if (doc != null) {
                Iterator it = doc.selectNodes( "//navigation//nav[contains(@rule,'"+rule+"')]" ).iterator();

                // process nav display
                while (it.hasNext()) {
                Element e = (Element);
                request.setAttribute("clicked", e.attributeValue("navid"));


                return true;