tags:

views:

240

answers:

1

Hello all, I need to create a drop down menu in struts2 from List. How can I create an action class so that when a .jsp page is loaded, it populates the list first?

i found this link http://www.mkyong.com/struts2/struts-2-sselect-drop-down-box-example/ , it works too but for it work user must go to http://localhost:8080/Struts2Example/selectAction.action to make it work but what if someone were to directly go to select.jsp.

+1  A: 

Since you're using a .jsp, you could load the dropdown with a scriptlet before you render the <s:select> tag.

However, it's better practice to allow the action to perform the loading and hide the .jsp files under /WEB-INF so they're not directly accessible. A common approach to perform this is the Prepare interceptor.

If you've got it in your interceptor stack, it will automatically invoke any method with the following name in your action before invoking the requested method:

  • prepare{MethodName}()
  • prepareDo{MethodName}()
  • prepare()

That means you can do something like the following in your Action:

public class YourAction extends ActionSupport {

    public String prepare(){
         // populate your drop down object
    }   

    public String view(){
         // forward to your jsp
         return SUCCESS;
    }

}

Then all you have to do is call your action's view() method and prepare will be called first by Struts.

Pat
So, let me see if I get this right: I implement prepare() method in the class that forwards it to the page that uses that list?Say, if that action class has 3 diff forwards, wouldn't it be called for each forward and not all of them will be using that list. Am I right?
Nishant
Not so. You can use the 'excludeMethods' parameter to prevent it from running on certain method names (see section in Workflow interceptor on excluding: http://struts.apache.org/2.0.11/docs/workflow-interceptor.html). There is also the 'alwaysInvokePrepare' parameter that you can set to false so that only prepare methods that are suffixed by your method name are invoked (i.e. if you had a view() method in your Action, and alwaysInvokePrepare = false, only prepareView() would be invoked).
Pat
Alright, that makes sense. Would you also know how can I implement dependent drop downs?
Nishant
Sure, I believe the accepted answer on this question is what you're trying to do: http://stackoverflow.com/questions/2174334/populate-a-dropdown-select-based-on-the-value-chosen-on-another-dropdown. On that note, you should consider accepting my answer to help your accept rate.
Pat
Thanks Pat, I am trying to follow that but I am stuck on how to call a method of an action when first drop value is selected! Would jquery be able to handle list as a result or does it have to JSON?
Nishant
You would have to create an onchange event handler on the dropdown that issued an ajax request to your action. The action would then have to create the response in a format that would be easiest for you to work with. I would recommend converting your list into a JSON object and then using that object to create your new, dependent dropdown. I'm not sure what version of Struts2 you're using, but if it's not already bundled, this is a good plugin: https://cwiki.apache.org/confluence/display/WW/JSON%20Plugin
Pat
Thank you once again, Pat. I am able to create one now that accpets list. But, that causes some hibernate issues. I have created another question for that. If that can't be fixed, I will have to use JSON to fix it.http://stackoverflow.com/questions/3376716/struts2-hibernate-returning-a-list-causing-hibernate-to-fetch-data-from-all-rela
Nishant