Calling an particular portion of an JSP into another JSP - java

I have two JSP
------------------------------------
first.jsp
.....
<jsp:include page="second.jsp"/>
....
<div id='anotherId' >
how to include that secound.jsp 's div tag
</div
-------------------------------------
second.jsp
....
<div id='abc'>
</div>
------------------------------------------
The reason why am doing like this means I have lot of conditions checking in my JSP i don't want to club all those in to a single JSP so I split that into small divs and I want to access that from my JSP. Just only to have clean code
Help me to sort this.

You need custom tags inside JSP's and reuse them. here is an example

Related

Bring common code into a separate jsp

I have more than two jsp pages. And into every page I have a common jsp code like this(navigation bar)
<nav>
<div class="nav-wrapper green darken-1 ">
SBT
<ul id="nav-mobile" class="right hide-on-med-and-down">
<li>Menu</li>
<li>Home</li>
<li>Test</li>
</ul>
</div>
</nav>
I don't wanna copy this or even more then this part into every jsp page. How to connect nav.jsp, which contain this code to every jsp page. And will it work slower or faster?
Create 3rd file say nav.jsp write your common code into it and simply include your nav.jsp wherever you want
You can use include action or include directive(consider the Advantage of both the ways and use the way which is convenient to you).

accessing information inside a jsp:include from the parent jsp

I'm trying to access form data that is filled out inside a jsp:included page from outside the page. I am using jquery and am open to using ajax. My code looks a little like this:
<form name=form1>
<jsp:include page="someFormData.jsp" />
//Other inputs out here in <% include %> files
<input type=button value=Submit onClick="return commonSubmit()"
</form>
I need to use the jsp:include style include because having everything on one page using
<%include...%> was causing an exception due to my jsp being too large. Alls I need to do is be able to submit the form and include the data inside "someFormData.jsp"

How can I asynchronously show/hide div in JSP using Java

I want to programmatically show a div tag after some processing has completed while rendering a JSP. What's the best way to do this using Java? Using jQuery I would do this:
$('#mydiv').removeClass("hide_me");
...or...
$('#mydiv').show();
How can I do this programmatically in Java while rendering the page?
Assuming you have the standard JSP setup including JSTL and have mapped it to 'c' you could just do:
<c:if test="${myCondition}">
<div id="mDiv">
content
</div>
</c:if>
It does seem from the comments like there is some confusion about rendering JSP on the server vs rendering content in the browser. Everything that happens in the JSP is server side work that has to completely finish before the browser receives the generated document and starts drawing it. You can't use JSP to change content that is already on the user's screen. You need javascript, html5, etc, for that.
With JSP Java runs on the server (unlike JavaScript that runs within browser) so conditionally render your <DIV> using Java if statement within JSP:
<% if( test="true" ) { %>
<DIV>....</DIV>
<% } %>
I think you are looking for something like this:
<div id="loader">Loading / GIF animation</div>
<div id="result" style="display:none;">
Lots of data.
Should be flushed to the browser every now and then.
This will take seconds...
</div>
<script type="text/javascript">
$("#loader").hide();
$("#result").show();
</script>

How to write content before and after the output of a JSP

I have JPSs that represent Components. I want to have these component JSPs write some HTML before and after the contents of the JSP is executed.
component.jsp
<#page session="false">
<%= "hello " + "world" %>
when this JSP/servlet is rendered, I want it to render:
<div class="component">
hello world
</div>
I want to be able to create various "wrappers", and depending on the JSP, include wrap the contents of the JSP with the correct content. If I want to change/augment the wrapper down the road, I want to only do it in one place (Could be 100s of components).
Can i do something with <#page extends="..."> possibly?
Thanks
What do you want is named: tag files. Introduced on JSP 2.0
With this approach you can write JSP tags using jsp, therefore you need to create a folder named WEB-INF/tags, and create a 'normal' jsp within this folder.
The tag that you want to create needs to have the following start instruction:
<%#tag description="tag description" %> in order to indicate this is a tag.
To use it you will need to reference the tags you want to use with the following instruction: <%# taglib tagdir="/WEB-INF/tags" prefix="custom"%>
So, you can do something like:
WEB-INF/tags/myTag.tag
<%#tag description="hello tag" %>
<%#attribute name="name" required="false" type="java.lang.String"%>
<html><head></head><body>
<h1>Hello <%=name%></h1>
<jsp:doBody />
</body>
index.jsp
<%# taglib tagdir="/WEB-INF/tags" prefix="custom"%>
<custom:myTag name="My Name">this is the content</custom:myTag>
The result will be a page printing
<html><head></head><body>
<h1>Hello My Name</h1>
this is the content
</body>
This is a terrible idea. JSPs with scriptlets are 1998 technology. No one writes these anymore.
If you must write JSPs, you're better off using JSTL and something like SiteMesh or Tiles to composite pages.
An even better idea would be to start moving towards something that might allow you to easily run a mobile solution alongside your web app. Services and templates would be my preference over JSPs.

Embedded custom-tag in dynamic content (nested tag) not rendering

Embedded custom-tag in dynamic content (nested tag) not rendering.
I have a page that pulls dynamic content from a javabean and passes the list of objects to a custom tag for processing into html. Within each object is a bunch of html to be output that contains a second custom tag that I would like to also be rendered. The problem is that the tag invocation is rendered as plaintext.
An example might serve me better.
1 Pull information from a database and return it to the page via a javabean. Send this info to a custom tag for outputting.
<jsp:useBean id="ImportantNoticeBean" scope="page" class="com.mysite.beans.ImportantNoticeProcessBean"/> <%-- Declare the bean --%>
<c:forEach var="noticeBean" items="${ImportantNoticeBean.importantNotices}"> <%-- Get the info --%>
<mysite:notice importantNotice="${noticeBean}"/> <%-- give it to the tag for processing --%>
</c:forEach>
this tag should output a box div like so
*SNIP* class for custom tag def and method setup etc
out.println("<div class=\"importantNotice\">");
out.println(" " + importantNotice.getMessage());
out.println(" <div class=\"importantnoticedates\">Posted: " + importantNotice.getDateFrom() + " End: " + importantNotice.getDateTo()</div>");
out.println(" <div class=\"noticeAuthor\">- " + importantNotice.getAuthor() + "</div>");
out.println("</div>");
*SNIP*
This renders fine and as expected
<div class="importantNotice">
<p>This is a very important message. Everyone should pay attenton to it.</p>
<div class="importantnoticedates">Posted: 2008-09-08 End: 2008-09-08</div>
<div class="noticeAuthor">- The author</div>
</div>
2 If, in the above example, for instance, I were to have a custom tag in the importantNotice.getMessage() String:
*SNIP* "This is a very important message. Everyone should pay attenton to it. <mysite:quote author="Some Guy">Quote this</mysite:quote>" *SNIP*
The important notice renders fine but the quote tag will not be processed and simply inserted into the string and put as plain text/html tag.
<div class="importantNotice">
<p>This is a very important message. Everyone should pay attenton to it. <mysite:quote author="Some Guy">Quote this</mysite:quote></p>
<div class="importantnoticedates">Posted: 2008-09-08 End: 2008-09-08</div>
<div class="noticeAuthor">- The author</div>
</div>
Rather than
<div class="importantNotice">
<p>This is a very important message. Everyone should pay attenton to it. <div class="quote">Quote this <span class="authorofquote">Some Guy</span></div></p> // or wahtever I choose as the output
<div class="importantnoticedates">Posted: 2008-09-08 End: 2008-09-08</div>
<div class="noticeAuthor">- The author</div>
</div>
I know this has to do with processors and pre-processors but I am not to sure about how to make this work.
Just using
<bodycontent>JSP</bodycontent>
is not enough. You should do soimething like
JspFragment body = getJspBody();
StringWriter stringWriter = new StringWriter();
StringBuffer buff = stringWriter.getBuffer();
buff.append("<h1>");
body.invoke(stringWriter);
buff.append("</h1>");
out.println(stringWriter);
to get inner tags rendered (example is for SimpleTag doTag method).
However, in the question's code I see that inner tag is comming from a string which is not rendered as a part of JSP, but just some random string. I do not think you can force JSP translator to parse it.
You can use regexp in your case or try to redesign your code in a way to have a jsp like this:
<jsp:useBean id="ImportantNoticeBean" scope="page class="com.mysite.beans.ImportantNoticeProcessBean"/>
<c:forEach var="noticeBean" items="${ImportantNoticeBean.importantNotices}">
<mysite:notice importantNotice="${noticeBean}">
<mysite:quote author="Some Guy">Quote this</mysite:quote>
<mysite:messagebody author="Some Guy" />
</mysite:notice>
</c:forEach>
I whould go with regexp.
I would be inclined to change the "architecture of your tagging" in that the data you wish to achieve should not be by tag on the inside of the class as it is "markup" designed for a page(though in obscurity it is possible to get the evaluating program thread of the JSP Servlet engine).
What you would probably find better and more within standard procedure would be using "cooperating tags" with BodyTagSupport class extension and return EVAL_BODY_BUFFERED in doStartTag() method to repeat process the body and/or object sharing such as storing retrived data in the application hierarchy of the session or on the session for the user.
See oracle j2ee custom tags tutorial for more information.

Categories