Urgently! How to write code in JSP Select Option..

Discussions

Web tier: servlets, JSP, Web frameworks: Urgently! How to write code in JSP Select Option..

  1. Urgently! How to write code in JSP Select Option.. (3 messages)

    hi,
      I want to use a Java Bean.the situation is:
     I use a select option to get a option value,
    ...
    <Select ..>
    <option>...</option>
    ....
    </select>
    ...
     then trigger to get the property of this Java Bean according to this option value. How can I do?
    Because , if I use <Script>, it is easy to trigger some event, but can not access the Java Bean.

    Thanks for your help!
    zhousr
  2. you do this this many ways (i think)

    here's what i'd do. the control for this application will be

    input.html -> process.jsp <--> JavaBean

    input.html
    ----------
    <form action="process.jsp" method="post">
    <select name="mode">
    <option value="1">mode 1
    <option value="2">mode 2
    </select>
    <input type="submit" value="Submit">
    </form>

    process.jsp
    -----------
    <jsp:useBean id="bean" class="foo.ModeBean" scope="request" >
      <jsp:setProperty name="bean" property="*" />
    </jsp:useBean>

    <%
      String mode = bean.getMode();
      if("1".equals(mode)) {
         // do stuff for this mode;
      }

      if("2".equals(mode)) {
         // do stuff for this mode; ect.
      }
    %>

    JavaBean
    --------
    public class Mode {
      private String mode;
      
      public Mode() {
        mode = "";
      }

      public void setMode(String mode) {
        this.mode = mode;
      }

      public String getMode() {
        return mode;
      }
    }

    Hope this makes sense =P

    It's a MVC wannabe design model =)
  3. whoops! this should be the mode bean... =p

    public class ModeBean {
      private String mode;
       
      public ModeBean() {
        mode = "";
      }

      public void setMode(String mode) {
        this.mode = mode;
      }

      public String getMode() {
        return mode;
      }
    }
  4. thanks