How can I make Spring-MVC output HTML - java

How can I make Spring-MVC output HTML. Right now I have Spring-MVC filling in data in a List like the one below:
<ul>
<li><c:out value="${memberrequest.name}"/></li>
<li><c:out value="${memberrequest.title}"/></li>
<li>District: <c:out value="${memberrequest.district}"/></li>
<li><c:out value="${memberrequest.school}"/></li>
<li><c:out value="${memberrequest.requestor}"/></li>
</ul>
the out put looks like:
But if the data is blank like title, school etc I don't want a line to blank so I was thinking that I could creating this list in the spring code and then pass the html back to the JSP page but it looks like Spring will not let you do it

Returning HTML from your controller defeats the very idea of MVC, because your controller takes over the responsibility of the view.
Instead, use the <c:if> tag within your JSP. More information here: http://docs.oracle.com/javaee/1.4/tutorial/doc/JSTL4.html#wp74001 (note: the first paragraph of this explanation seems to indicate that you should use a scriptlet; it really doesn't, and if you read down you'll see the JSTL approach).

If you really want to do this you can use the escapeXml="false" attribute on c:out like so:
<c:out value="${myHtmlString}" escapeXml="false"/>
But as parsifal says, it's best to construct your HTML in your view if you can.

Related

how to add <div> tag data to arraylist in java

i want pass data /innerHTML to the another jsp page ,so how could i pass it ?is it possible to use arraylist or any other way to pass this large data to another page?
Is it possible add tag data to the arraylist in javascript?or how can we pass div tag innerhtml to the next page in jsp?
i want pass data /innerHTML to the another jsp page
This seems wrong approach to me,because the main adavantage of Jsp is actually writing HTML on demand,where ever you need it.
Pass the essesntial data which decides the html to the jsp using the session or request but passing html is a bad idea.
Instead of passing the HTML view data, just pass what needs to be displayed and let the target JSP decide how to render it. For example, if you wanted the target JSP to print a welcome message for the logged-in user; then instead of passing the HTML
target.jsp?msg=<div><p>Welcome <b>John</b></p></div>
You would pass a request attribute named user as
request.setAttribute("user", "John");
which can now be accessed in your target JSP (using EL) as
<c:if test="${not empty user}">
<div><p>Welcome <b>${user}</b></p></div>
</c:if>
In your case, your Cart along with its List<CartItems>, should be stored in HttpSession. This way the shopping data remains accessible to any JSP/Servlet that needs to update the cart.
Wrap your div tag div tag. and get the html tags from ouside wrapper div tag.
<div id="wrapper">
<div id="what_you_want">
some contents...
</div>
</div>
$("#wrapper").html() - It contains whole html code in wrapper div.
Try some Javascript. If you really insist on using kinda abnormal methods (I do too sometimes), you can create an input field of type hidden and put the cart info into that input as a delimited string.

Including a page using spring

I'm using spring to display a jsp page. That's fine. Now I'd like to include another page in it. I know I can use the <jsp:include> tag in my page however I'd like to use a controller to pass some logic to the page which is to be included. Is it possible please?
Thanks,
Krt_Malta
You might want to checkout a template engine like Freemarker or Velocity. Here's a description of how Spring integrates with view technologies.
Your controller can add objects to the Model and Spring will add them to the HTTPRequest as attributes, is that what you mean? Including a jsp fragment doesn't affect that, your jsp fragment can access the request attributes. Otherwise it's unclear to me what you mean by "passing some logic to the page".
Look into JSTL (Jsp Standard Tag Library).
You can implement conditional logic in your JSP using the JSTL <c:if> or <c:choose> tags. Then, instead of splitting up the logic into multiple files and using <jsp:include> to include the logic you want, you can build all the logic into your page and the controller can set request (or other scope) attributes to turn on the logic you desire.
For example:
<c:if test="${Order66}"
&ltc:forEach items="${JediMembership}" var="jedi">
kill ${jedi}
&lt/c:forEach>
</c:if>
<c:if test="${Order67}"
two large pizza, extra cheese.
</c:if>
The controller then set "Order66" and / or "Order67" in the request (or any other scope).

How do I pass information from a servlet to a JSP page

Is it possible to have a servlet that contains an object (an ArrayList in this case) that then does the equivalent of displaying a jsp page and passing that object to the jsp page. In this case the ArrayList contains database results an I want to iterate through and display the results on the JSP page.
I am not using any MVC framework, is it possible to do this with the basic Servlet/JSP architecture.
Yes.
in the servlet call request.setAttribute("result", yourArrayList);
then forward to the jsp:
getServletContext().getRequestDispatcher("your.jsp")
.forward(request, response);
using JSTL, in the jsp:
<c:forEach items="${result}" var="item">
...
</c:forEach>
If you don't want to use JSTL (but I recommend using it), then you can get the value using request.getAttribute("result") in the JSP as well.
Alternatively, but not recommended, you can use request.getSession().setAttribute(..) instead, if you want to redirect() rather than forward().
You can pass objects to jsp's by embedding them within the Request.
request.setAttribute("object", object);
and within the jsp:
request.getAttribute("object");
You can put it using request.setAttribute("myobj", myObj); see javadoc
If you are trying to make some kind of "component" then it's better to convert the JSP page into a custom tag. Here is excellent article about that: http://onjava.com/pub/a/onjava/2004/05/12/jsp2part4.html

display tags jsp tags

i am using diaply tags library for displaying my tables.But i cant use the <% %>
tagst there.If i try to us it it gives me error.but i can use tag there.
If i try to use followin code in my jsp it give an error sayin shoul hav a matcheing ending tag.
i have follown java code in jsp
List<Course> = (List<Course>)request.getAattribute("crc");
here Course is a class/bean.
can anyone suggest me such library that i can use with struts for auto paging,displaying list in tables,and with other features provided by display tag.I want to use struts and i want the view to look good and yet easy to devlop.that is i want to achieve high class userinterface with littel effortr toward displaying o/p / view.
can anyone provide the example of disploay tag with struts
You can use the name attribute of the table tag (normally like this:)
<display:table name="crc" ...>
</display:table>
To use the crc List as the basis of Javabeans to display.
See http://displaytag.sourceforge.net/1.2/displaytag/tagreference.html#display-el:table for more information about the table tag.

How Do I make dynamic-attributes Work in JSP Tag Files?

So according to my JSP reference book, as well as every other reference I can find on the web, I'm supposed to be able to do something like:
<%# tag dynamic-attributes="dynamicAttributesVar" %>
and then when someone uses an attribute that I didn't define in an attribute directive, I should be able to access that attribute from the "dynamicAttributesVar" map:
<%= dynamicAttributesVar.get("someUnexpectedAttribute") %>
However, that doesn't work, at all; I just get a "dynamicAttributesVar cannot be resolved" error when I try.
Now, I did discover (by looking at the generated Java class for the tag) that I can "hack" a working dynamic attributes variable by doing:
<% Map dynamicAttributesVar = _jspx_dynamic_attrs; %>
Now, that hack doesn't work unless I also use the dynamic-attributes parameter on my tag directive, so it seems that the parameter is doing something.
But what I want to know is, how can I make it do what it does for every other JSP user out there?
Just trying to get a badge for answering a four year old question.
I have this problem as well and came across some help at O'Reilly to use JSTL instead of scriptlets.
The original poster could have used this code to get all keys/values:
<c:forEach items="${dynamicAttributesVar}" var="a">
${a.key}="${a.value}"
</c:forEach>
This would get a specific value:
<c:out value="${dynamicAttributesVar['someUnexpectedAttribute']}"/>
Isn't "dynamicAttributesVar" the name of the key in the page context that the dynamic attributes are put into? So you could do
<c:out value="${dynamicAttributesVar.someUnexpectedAttributes}"/>
or if you must use scriptlets:
Map dynamicAttributes = (Map) pageContext.getAttribute("dynamicAttributesVar")
(Disclaimer: I haven't tried it, I've just used dynamic attributes in tags with direct Java implementations... but it seems reasonable)

Categories

Resources