wood burning stoves 2.0*
The moose likes Struts and the fly likes Showing Hiding <s:div> tag in Struts2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Showing Hiding <s:div> tag in Struts2" Watch "Showing Hiding <s:div> tag in Struts2" New topic
Author

Showing Hiding <s:div> tag in Struts2

Girish Kumar Prabhakar
Ranch Hand

Joined: Dec 09, 2008
Posts: 34
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
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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.
Richard Golebiowski
Ranch Hand

Joined: May 05, 2010
Posts: 213

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
Rancher

Joined: Sep 29, 2008
Posts: 12617

@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

Joined: Dec 09, 2008
Posts: 34
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

Joined: May 05, 2010
Posts: 213

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

Joined: Dec 09, 2008
Posts: 34
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

Joined: May 05, 2010
Posts: 213

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

Joined: Dec 09, 2008
Posts: 34
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
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Dec 09, 2008
Posts: 34
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
Rancher

Joined: Sep 29, 2008
Posts: 12617

But you're using a plain HTML div tag, and plain JavaScript. Not Struts specific.
Richard Golebiowski
Ranch Hand

Joined: May 05, 2010
Posts: 213

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

Joined: Dec 09, 2008
Posts: 34
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

Joined: Dec 09, 2008
Posts: 34
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
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: May 05, 2010
Posts: 213

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

Joined: Dec 09, 2008
Posts: 34
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

Joined: May 05, 2010
Posts: 213

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
Rancher

Joined: Sep 29, 2008
Posts: 12617

The errors are already *in* the request; they're contained within the action. Why do it twice?
Richard Golebiowski
Ranch Hand

Joined: May 05, 2010
Posts: 213

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

Joined: May 05, 2010
Posts: 213

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

Joined: May 05, 2010
Posts: 213

It is also interesting to see the difference when you use the following for login.jsp:

Girish Kumar Prabhakar
Ranch Hand

Joined: Dec 09, 2008
Posts: 34
Thanks a lot Richard & David for your Replies.
 
jQuery in Action, 2nd edition
 
subject: Showing Hiding <s:div> tag in Struts2
 
Similar Threads
show and hide using check box
how to hide and show contents with struts2 div tag
Any idea what could cause new <br> tags to get added to the rendered code?
struts tag vs html tag
div tag in struts 2