4

i have a requirement where i need to display some fields on the JSP. These fields are dynamic in nature, meaning, for ex:, if i changed some value in the dropdown, some fields will be hidden and some other fields might come. I dont want to write Javascripts for show/hide of divs, rather want logic to be coded somewhere at server side.

I have an idea of implementing a custom tag library, but i wnat if i could get an out of the box solution.

any new suggestions or solutions are welcomed.

Adeel Ansari
  • 39,541
  • 12
  • 93
  • 133
Ashish
  • 421
  • 9
  • 22

4 Answers4

1

You had better do it in JavaScript. Having said that, you can send AJAX request to get the new form fields based on the input provided. For example, have a <div> to set the HTML coming from the server.

Adeel Ansari
  • 39,541
  • 12
  • 93
  • 133
0

Use struts framework , there are some tags which can hide and show fields based on values

Logic/logicout tags example

developer
  • 9,116
  • 29
  • 91
  • 150
  • I'd not recommend using Struts 1 anymore. For new projects you should definitely switch to Struts 2. – Thomas Sep 06 '11 at 09:21
  • @Thomas : based on requirment we can shift to struts1 or 2 , still some projects are building on struts 1 – developer Sep 06 '11 at 09:25
  • If existing projects use struts 1, then you don't have to switch, but please don't suggest to use struts 1 for new projects (the OP didn't mention it but I assume he's not using struts right now). – Thomas Sep 06 '11 at 09:30
  • @Thomas : He didnot mention it, may be he want to use some mvc framework – developer Sep 06 '11 at 09:32
  • As I said, if he wants to use a __new__ (meaning new for him, not currently using) framework, then please suggest a current one (like struts 2 and not struts 1). – Thomas Sep 06 '11 at 09:34
  • I am not using any framework as of now. I was thhinking of Struts 2 only. – Ashish Sep 06 '11 at 09:48
0

If you want to use a web framework, try Struts 2. It provides tags like <s:if test="some ognl expression" ...> to selectively render html content.

Otherwise you could just go with the JSTL core tags, which provide a <c:if text="some Java EL expression" ...> and a <c:choose ...> tag ( Example ).

Remember to reload the page after changing select box values in order to update the UI. For this some JavaScript might be needed.

Community
  • 1
  • 1
Thomas
  • 87,414
  • 12
  • 119
  • 157
  • I am not favouring tags as they will clutter the JSP. I want the tag should be intelligent enough to render or not to render the body. – Ashish Sep 06 '11 at 09:50
  • @Ashish Well, the tags would still have to have a body in the jsp in order to render it. Alternatively you could have tags that render their body themselves (i.e. a combobox that checks itself whether it is visible or not), however, that would require special JSP components. In that case you might want to have a look at JSF(-like) frameworks (JSF 2.0 doesn't support JSP anymore), which provide tags with a `rendered` attribute taking an expression. – Thomas Sep 06 '11 at 10:52
  • @Ashish - Please also update your question and mention you want those "self-checking"/intelligent tags, so that others are aware of this. – Thomas Sep 06 '11 at 10:53
0

DOM (Document Object Model) in Javascript is very powerful and cross browser.

to remove a node on UI

1.removeChild(nodename)

to add a node on UI

2.elementNode.insertBefore(new_node,existing_node)

I used it. it works well. more information on DOM.

http://www.w3schools.com/dom/default.asp

Balaswamy Vaddeman
  • 8,360
  • 3
  • 30
  • 40