• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Showing Hiding <s:div> tag in Struts2

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I have 3 div elements in my jsp.

I want to show only one div element at a time and hide the others. There is no style attribute in s:div tag

How can i hide/show the s:div element.

Thanks in advance

Girish
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JavaScript and the div's ID? Not really sure what you're asking. There's no "style" attribute, but there is a "cssStyle" attribute.

Note that the div tag is really meant as one of the Ajax tags, so you might need to do it via Dojo if the standard attributes don't mix-and-match the way you need them to.
 
Ranch Hand
Posts: 213
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You hide or show it like any HTML element
To hide use document.getElementById("").style.display="none"
To show use document.getElementById("").style.display=""
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Richard: that's not necessarily true, depending on the usecase. When dealing with Dojo it's better to work within the Dojo framework, in my opinion.

That said, the Dojo tags were deprecated for a reason--it's a *very* old version of Dojo (0.4.3, current is 1.5) and I really don't think it's worth working with.
 
Girish Kumar Prabhakar
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot David and Richard for your replies.

Let me explain in my requirement.

My JSP have around 50 fields for the user to input, so i thought of making div elements and having these fields in different div elements. But at a time only one div element should be visible and others should be hidden. On clicking next and previous buttons on the div we can hide/show divs.

So, how can i hide/show these div elements.

Also, should i use <s:div> element or normal HTML <div> element for this.

Richard : I know about document.getElementById("").style.display="none" , but <s:form> does not have any on page load function. So the first time, on page load how to hide these div elements.

Thanks again for your replies.
Girish
 
Richard Golebiowski
Ranch Hand
Posts: 213
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your situation:
Use the regular HTML div tag
Set the div display to "none" or on the bottom of the page after form and everything else have a Javascript section that executes to set the DIVs hidden.
 
Girish Kumar Prabhakar
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Richard,

Thanks a lot for your reply.

Working on the solution provided by you, i have 2 doubts:

1. Is it fine to use normal HTML elements like <div> instead of Struts2 tags. Is it a good practice.

2. I tried calling javascript from <s:button> onclick. It gives me an error :
There is no Action mapped for namespace / and action name pages/

I used the following line in my jsp to call javascript :
<s:submit onclick="add()" label="Next"></s:submit>

Is it correct way to call javascript or i should use HTML <input> tag for creating button.

Thanks a lot again.
Girish
 
Richard Golebiowski
Ranch Hand
Posts: 213
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) You can use regular HTML tags along with Struts tags.
2) To call the Javascript to set the display for the divs to none, do it like the following at the bottom of the page:

 
Girish Kumar Prabhakar
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Richard, what should i give in the onclick attribute of s:submit, so that i can hide div1 and show div2.

Below is my jsp code.



Also, I tried without using <s:submit> in the above jsp, but still the divs are being displayed.

Thanks again for all your help.
Girish


 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags when posting code or configuration. Unformatted code and configuration is unnecessarily difficult to read. You can edit your post by using the button.

If this is no longer Struts-specific I shall move it to HTML/JavaScript.
 
Girish Kumar Prabhakar
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi David,

I have formatted my code.

It is not only related to HTML/Javascript , since i am using Struts2 and trying to apply it in Struts2.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But you're using a plain HTML div tag, and plain JavaScript. Not Struts specific.
 
Richard Golebiowski
Ranch Hand
Posts: 213
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is caused by how Struts is rendering the HTML. View the HTML and you will see what is happening.
Use "<s:form theme="simple">" for the form tag and it will work.

Below is my test page.
 
Girish Kumar Prabhakar
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Richard,

Thanks a tonn, this code is working fine. ThankYou.

But just one more query.

If i use theme="simple" , my layout messes and action errors are not displayed. Is there any other alternative through which i can get this functionality(showing/hiding of div).

Thanks a lot again.
Girish
 
Girish Kumar Prabhakar
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Richard ,
one more thing when using theme="simple", the lables of <s:textfield> are not getting displayed.

My part of jsp code is :



Thanks


 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
His solution is problematic in that you'll no longer have labels, error messages, etc.

His original analysis, however, is correct; you can't wrap table rows in div tags--you would actually need to hide/show the table rows themselves. In order to do that you'd need to either use CSS selectors to find them, or enhance the template to give a useful ID or class to the rows themselves. This is something I've been meaning to do for a really long time but never get around to it--implemented it, but never checked it in :(
 
Richard Golebiowski
Ranch Hand
Posts: 213
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What David said is correct. Because of the Struts templates, there are some constraints about what you can do in your form. For this reason, I usually use the simple theme and handle the labels on my own.
 
Girish Kumar Prabhakar
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Richard,

Can you please tell me how you handle labels for text fields and add action errors when using simple theme.

Thanks
 
Richard Golebiowski
Ranch Hand
Posts: 213
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Labels are just plain HTML as follows:



For action errors, the simplest method I use is setting a Session attribute with the error message. The most complex method I use is DWR to push messages to the client.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The errors are already *in* the request; they're contained within the action. Why do it twice?
 
Richard Golebiowski
Ranch Hand
Posts: 213
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The session error messages are legacy so I do what they always did and don't mess with it. The DWR is used for more extreme case, usually when there is some long running process and I want to provide feedback to the client so they can see what is happening on the server.
 
Richard Golebiowski
Ranch Hand
Posts: 213
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As it turns out, if you want you can still use the Struts ActionError, ActionMessage, and FieldError by:

Note: this example is based on modifying this example


in login.jsp, the form to this




Change validate method in LoginAction.java to this:



Run the example and you will see that the validation is happening and the messages are displaying.
 
Richard Golebiowski
Ranch Hand
Posts: 213
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is also interesting to see the difference when you use the following for login.jsp:

 
Girish Kumar Prabhakar
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Richard & David for your Replies.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic