Search code examples
javahtmljspstruts

Passing a value from MenuItemTag to .jsp


Situation:

I have a .jsp with a Menu and MenuItems, which is being displayed on every other .jsp in the app. Three of those MenuItems, at this point in time, each redirect the user to a different .jsp page. But that is silly because those .jsp's AND the Action classes are almost identical. So what I want to do is:

  • make these 3 different menuitems/links redirect to the same page
  • send a parameter with when clicking on a link/MenuItem
  • depending on the value of 'mode':
    • change the .jsp's layout
    • change the values of variables in the onPostPrintx-actions in the Action class

Code:

JSP-page with code for the Menu Widget:

...
<ui:submenu name="Chapter A">
    <ui:menuitem link="/ThisPage.do">Printoption A</ui:menuitem>
    <ui:menuitem link="/ThisPage.do">Printoption B</ui:menuitem>
    <ui:menuitem link="/ThisPage.do">Printoption C</ui:menuitem>
    ...
</ui:submenu> 
<ui:submenu name="Chapter B">
    ...
</ui:submenu>
...

JSP-page with the print-buttons ('ThisPage'):

<html>
<div>
...
<c:set var="mode" value="${PageData.fields.mode}" />
<c:set var="title" value="${val:iif(mode=='1','Option1',val:iif(mode=='2','Option2','Option3'))}" />
...
<body>
    <html-el:form action="/Print" method="post" >
    <h1>Print ${title}</h1>
    <html-el:hidden property="field(mode)"/>
...
(assorted fields to filter output data)
...
        <html-el:submit property="event(printPdf)" styleClass="button" style="width=125">Print to .PDF</html-el:submit>
    <c:if test="${(mode=='3')}">
        <html-el:submit property="event(printWord)" styleClass="button" style="width=125">Print to .DOC</html-el:submit>
    </c:if>
...
</div>
</body>
</html>

Action class ('Print'):

...
public ActionForward onPostPrintPdf(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) 
            throws Exception 
{
    ...
    request.setCharacterEncoding("UTF-8");
    Connection connection = null;
    PageData inForm = ActionUtils.getInputForm(mapping, request, form);
    String mode = (String)inForm.getField("mode");
    ...
    (assorted variables, field/variable validation, export to Jasper Report, etc.)
    ...
    return ActionUtils.getInputRedirect(mapping, request, inForm);
}
...
(more actions for more printing options)
...

What I am struggling with:

I am pretty new to Struts (and java) and I have set up everything (within my knowledge) to 'receive' the parameter value and put it in the 'mode' variable. But I'm struggling with 'sending' the value because I can't use things like 'onclick' in the MenuItems.

At this point I really don't know how to pass a value to 'mode' when clicking on one of the MenuItems.


Solution

  • Thanks to Jimmy for leading me in the right direction! Here is the solution that works with my code:

    ...
    <ui:submenu name="Chapter A">
        <ui:menuitem link="/ThisPage.do?field(mode)=optionA">Printoption A</ui:menuitem>
        <ui:menuitem link="/ThisPage.do?field(mode)=optionB">Printoption B</ui:menuitem>
        <ui:menuitem link="/ThisPage.do?field(mode)=optionC">Printoption C</ui:menuitem>
        ...
    </ui:submenu> 
    <ui:submenu name="Chapter B">
        ...
    </ui:submenu>
    ...