Disabling form field based on value in form in Struts

Posted on 2005-04-25
Last Modified: 2010-03-31
Hi all,

          Based on a value in an Action Form, i want to disable fields.

          Heres the requirement, when from a page (say   a.jsp) i submit a form with hidden varibale "event". Now, if event = "new" i use the setEvent() method in the Action form to set it correspondingly. Then on the next page, based on this form value, i want to disable / enable a form field . Can i do that ??

          preferably using c-tag ? or any struts tag

Question by:letsbedecent
    LVL 15

    Expert Comment

    Yes, you can do it.
    You can use the struts logic tags
    to wrap the jsp part that generate the html for that form field.
    The easiest way to pass that info is to make the Action take that field value from the form
    and put t in the http request attributes then to use the empty tag or the equal tag.
    in your Action

    request.setAttribute("shoud_display", shouldDisplay?"yes":"no");

    then in your jsp

    <jsp:useBean id="testString" scope="request" type="java.lang.String" />
    <logic:someComparisonTag name="sould_display" value="yes">
    <!-- display your elemet here e.g --> <input type=....
    LVL 15

    Expert Comment

    More info on bean and logic tags see:
    Also see (code/Chapter4/SimpleStruts/index.jsp)

    Author Comment

    what is the id="testString"  here ??
    LVL 15

    Expert Comment

    Oops, sorry:
    <jsp:useBean id="sould_display" scope="request" type="java.lang.String" />
    <logic:someComparisonTag name="sould_display" value="yes">

    Is that more clear?

    Author Comment

    id should be the form-name isnt it ?? can it be a field in that form too !! should display is only a field right
    LVL 15

    Expert Comment

    >> should display is only a field right
    In the example above, yes.

    >> id should be the form-name isnt it ??
    in this case is the request attribute the action set -> request.setAttribute("shoud_display", shouldDisplay?"yes":"no");

    >> can it be a field in that form too !
    Yes, I think you can do something like that:
    <jsp:useBean id="the_name_of_your_form" scope="request" type="the_class_name_of_your_form" />
    <logic:someComparisonTag name="the_name_of_your_form.the_property_that_indicate_yes_to_display" value="the_value_that_indicate_yes_to_display">
    The value of the name can be the bean name or nested property or index

    Author Comment

    i have a hidden field in a form , on a.jsp
    when submitted this form i go to a action handler and come to b.jsp

    now on b.jsp also i have the same hidden field.

    can i on b.jsp do the following

    <logic:equal name="hidden_field" value="true">
      //Block that should get processed only when value is true
    LVL 15

    Accepted Solution

    Yes, though when this value is a request paramter (and not a request attribute set by the struts action) then attribute "name" should be replaced with "parameter"

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    Title # Comments Views Activity
    countClumps  challenge 10 68
    scores100 challenge 3 67
    Increment alphanumeric sequence 6 54
    strCount chalenge 3 33
    INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
    Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now