This is about jsp, beans with struts. The problem is, I am using one jsp which is containing some select boxes. I ll be populating the select boxes from DB thru beans and action class. It’s working properly. The problem is with the data insertion from JSP to DB. The case is, I have to add a new record (record is containing the select boxes data as fields). Now if I select any data from the ‘select’box, I have to capture that data thru ‘action class’ and then pass that to ‘form Bean’ so that I can update my DB with the new record. But what is happening is, I am unable to get the values of the ‘select box’ data from the JSP to action class thru form Bean. Can any one help me out with this? One more thing, I can’t use sessions in JSP. If any one can provide some examples in this regard, that will be of great use.

Dani AI

Generated

Short answer for : the select control must post a parameter whose name exactly matches a property on the ActionForm, and the property type must match the HTML control (single select -> String; multi-select -> String[]). Letting Struts populate the form bean is cleaner than manually calling request.getParameter(...); ’s pointer about parameter vs parameterValues is useful for debugging, but the framework will do that wiring automatically when names and setters match.

Example ActionForm:

public class MyForm extends ActionForm {
  private String country;        // single select
  private String[] colors;       // multiple select

  public String getCountry() { return country; }
  public void setCountry(String country) { this.country = country; }

  public String[] getColors() { return colors; }
  public void setColors(String[] colors) { this.colors = colors; }
}

Example JSP (Struts tag and plain HTML):

<html:form action="/save">
  <html:select property="country">
    <html:option value="US">United States</html:option>
    ...
  </html:select>

  <select name="colors" multiple="true">
    <option value="red">Red</option>
    <option value="green">Green</option>
  </select>

  <html:submit/>
</html:form>

Action execute snippet (how to read the bean):

public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
  MyForm f = (MyForm) form;
  System.out.println("country=" + f.getCountry());
  System.out.println("colors=" + Arrays.toString(f.getColors()));
  ...
}

Quick checklist/troubleshooting:

  • Confirm the <action> mapping name matches the <form-bean> name in struts-config and set scope="request" if sessions are not allowed.
  • Ensure select name (or property in Struts tags) exactly matches the form bean property and that public setter exists.
  • Use String for single selects, String[] for multi-selects.
  • Make sure each <option> has a value attribute (that value is what gets posted).
  • For session-scoped forms override reset() to clear arrays/checkboxes; for request scope a new form is built each request.
  • To debug, dump request.getParameterMap() or log Arrays.toString(request.getParameterValues("colors")) to see what the browser actually sent.

Referencing and : if multiple selection behavior is unclear, confirm how the browser selects values (Ctrl/Shift on most OSs) and then check the submitted parameter array as above.

Recommended Answers

All 3 Replies

Whether u r able to get the select box value in the action class whn u select or deselect the select box??????

hey i hav same problem.. i cant get select box values to action class,.... hw to map it.. i mean 1stly hw does the multiple select box value pass .. as an array.. as a string.. waa?

For single parameter you will use getParameter(java.lang.String name). If you look just few lines below this method in ServletRequest API you will see getParameterValues(java.lang.String name) to get multiple selection

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.