javastrutshttprequest

request attribute behaviour in struts


I want to understand how request attributes behave. I have right now set some values in session which is giving me a lot of problems. I'm converting them all to request attribute.

I have an action class where in view method I have set up something like this,

public ActionForward view(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception
{
  //...
  request.setAttribute("abc", ABC);
  //...
}

this view method is called when I click a tab and a page is displayed. There is another button within this page called "Export". I have made export call same action class and call a method called exportExcel as follows:

public void exportExcel(ActionMapping mapping, ActionForm actionForm,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception
{
    if (request.getAttribute("abc") != null) {
        String def = (String)request.getAttribute("abc");
    }
}

assuming I would get request parameter. It didn't work. Also, I don't want to set this variable in session. Is there any workaround to get this request attribute in this exportExcel method?


Solution

  • In your jsp you need to create hidden input field of name="abc" and set its value to abc request attribute value. This way it will get submitted back when user clicks on your export button.

    If your export button is an anchor, then you could set its href with abc parameter name/value.