JSP Tag Recursion - recursion

I am implementing a tree tag for one of my practice projects, where I would display the contents of a directory in the form of a tree (recursively). I had implemented a similar requirement as a Custom Tag in Java during the pre-JSP2.0 days.
Handling a directory needs recursion (to handle the subdirectories)! Is it possible to code this as tag files and can they be used in a recursive manner?

Here is an example of an recursive tag file that displays from a node all it's children recursivly (used to generate a YUI treeview):
/WEB-INF/tags/nodeTree.tag:
<%#tag description="display the whole nodeTree" pageEncoding="UTF-8"%>
<%#attribute name="node" type="com.myapp.Node" required="true" %>
<%#taglib prefix="template" tagdir="/WEB-INF/tags" %>
<li>${node.name}
<c:if test="${fn:length(node.childs) > 0}">
<ul>
<c:forEach var="child" items="${node.childs}">
<template:nodeTree node="${child}"/>
</c:forEach>
</ul>
</c:if>
</li>
This can be used in a regular JSP file like this:
<div id="treeDiv1">
<ul>
<c:forEach var="child" items="${actionBean.rootNode.childs}">
<template:nodeTree node="${child}"/>
</c:forEach>
</ul>
</div>

Related

How to prevent escape of html tags in Spring MVC?

I have some data inside my database with html tags like
<b>, <br>
But, when I try to store it into a model object and render it on a JSP, it is rendered with the tags. The tags are not evaluated.
Any idea on how to get my page to process those tags? Below is the code I use in my JSP.
<div class="col-xs-6 form-group">
<label>Comments</label>
${requestObject.comments}
</div>
Edit:
I tried the below code also, with no luck.
<c:out escapeXml="false" value="${requestObject.comments}" />
Database Content
<b>Oh Snap</b>
HTML Source
<b>Oh Snap</b>
I am expecting my text to be rendered bold
Oh Snap
Not sure whether this is what you are seeking, but this should allow HTML to be evaluted:
<%# taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<spring:htmlEscape defaultHtmlEscape="false" />
If you have Spring form or message tags, you can do: htmlEscape="false".
UPDATE:
Sadly, the c:out syntax will take precedence over the Spring tag on this but you can also try:
<spring:escapeBody htmlEscape="false">
<%=requestObject.comments%>
</spring:escapeBody>

Custom validation of CKEditor (RTE) in ASP.NET MVC

I'm using ASP.NET MVC & CKEditor (Basic) to get input (almost exactly like this stack overflow RTE.) User's are limited to bold,italic,links.
I'm assuming I need to validate this server-side, incase someone issues CURL requests to my controller with any desired HTML in it. (I.E so they weren't limited by client-side validation).
So I need to allow, bold, italic, link tags, lists, but absolutely nothing else, how can this be achieved? I'll have a custom validator on my view model, but what is the best way to structure this custom validator? What to look for etc?
This could be achieved through regex and negative lookahead.
Something like that <(?!br/|br /|a|/a|strong|/strong|b|/b|i|/i|ul|/ul|li|/li>).+?> would match all tags except <br />, <a></a>, <strong></strong>, <b></b>, <i></i>, <ul></ul>, and <li></li>.
Then you could replace the matched occurences with an empty string.
For example this input (tested on regextester.com) :
test test link
Some <strong>text in bold</strong> and <b>another one</b> but also something
in <i>italic</i>
<ul>
<li>Now</li>
<li>a list</li>
</ul>
<table>
<tr>
<td>And a table</td>
</tr>
</table>
<br />
<hr />
Would become :
test test link
Some <strong>text in bold</strong> and <b>another one</b> but also something
in <i>italic</i>
<ul>
<li>Now</li>
<li>a list</li>
</ul>
And a table
<br />

how to use drop down box in spring

I have recently started working on spring MVC. In the JSPx web page I need to take the country-code from user and save it into the database. For this purpose, initially, I was using textfield for user input. The code is as below-
<fieldset class="fieldset" >
<div class="fields">
<field:input field="country" id="label_organisation_view_country" disabled="${!isAddOrganizationView}"/>
</div>
</fieldset>
I have made an enum (public enum Country) containing the list of country-codes. Now, I want to use drop-down for getting the country-code instead of textbox, using the created enum.
Can anyone please let me know how to get it or redirect me to the same page?
using the spring form tag, that requires this :
<%# taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
you can do something like:
<form:form name="myFRM" id="myFRM" action="${myURL}" method="POST" modelAttribute="myBackingObject">
<form:select path="myCountry" id="MyCountry" name='MyCountry'>
<form:options items="${countries}" itemLabel="yourMethodForDisplayNameInEnum"/>
</form:select>
and in controller :
model.addAttribute("countries", CountryEnum.values());
model.addAttribute("myBackingObject", yourPojoContaingFieldMyCountry);
Check out the docs :
http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/
and specifically mvc
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html

ASP.NET Custom Navigation in Master Page

All,
I am trying to decide the best way to convert my current asp.net page into a master page. This issue I have is with the navigation. I have 3 aspx pages which are essentially html with nothing flash in as yet. These are:
Home
Reports
Admin
In my current pages, the menu built using static html as follows:
<div id="nav">
<ul>
<li>Home</li>
<li>Reports</li>
<li>Admin
</li>
</ul>
</div>
The class for each li a class is currently determined in the actual html source. So in Default.aspx the class for li a href="Default.aspx" ... would point to the selected tab class in my css, tab-selected. The other classes for the rest of li a's would be tab-unselected.
Again in reports.aspx, the class for li a href="reports.aspx" ... would point to the selected tab class in my css, tab-selected. The other classes for the rest of li a's would be tab-unselected.
My CSS is displaying the menus perfectly with the above.
I then thought I would try moving to Master pages. So I copied the above into the master page. This looks ok but the problem is as the navigation is in the Master page, how can I change the highlight the current page in the navigation.
Then I thought I would try and use the control and put the above navigation items in it. When I ran the navigation was rendered as a table and so completely broke my css. I thought tables were bad in terms of laying out things like menus and you should use unordered lists?
Question is, what is the best way to implement my navigation with ASP.NET?
Thanks
Andez
If you want to use your html list to display your navigation but style your list items using CSS then you could use this technique.
Use inline code blocks such as (assuming VB)
<% If Page.Request.Path.Contains("Default.aspx") Then %>
<li class="selected">
<% Else %>
<li>
<%End If%>
Home</li>
You would need an IF statement for each page and the code will look a bit scrappy. I'm not saying it's the best way but basically if you are using a single piece of html for you menu (but it in the Masterpage or a Control) you will need to use some logic code somewhere for the html to be different on each page. You could do this in the code-behind as well but adding some inline code blocks is the perhaps the easiest/quickest way. You could get more funky and write a helper class that wraps it all up in a select statement and just call
MenuHelper.RenderLink("Default.aspx")
but that will take more work - give me a shout if fancy trying this approach instead.
As for why your tables broke I don't know. My opinion would be if you want to use tables and your site/users won't suffer from it then go ahead - but if you have to consider mobile devices and screen readers etc then it is my understanding that tables will be harder to navigate. It's really up to you!
Cheers
Came across the following link:
http://forums.asp.net/t/1626206.aspx
Version 4.0 of the .NET Framework has some nice changes to the asp:Menu control. One of them is to render as a list. Yay! So for now I have moved from VS2008 to VS2010 Express.
After a little fiddling around I am now using a site map with an asp:Menu within a Master page as follows:
Web.sitemap
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode url="" title="" description="">
<siteMapNode url="Default.aspx" title="Home" description="" />
<siteMapNode url="reports.aspx" title="Reports" description="" />
<siteMapNode url="administration.aspx" title="Administration" description="" />
<siteMapNode url="ChildPage.aspx" title="Master Template" description="" />
</siteMapNode>
</siteMap>
Site.master
...
<head runat="server">
<link rel="stylesheet" type="text/css" href="styles/mystyle.css" />
</head>
<body>
<form id="Form1" runat="server">
<div id="header">
<div id="nav">
<asp:SiteMapDataSource id="nav1" runat="server" />
<asp:Menu ID="main-menu" runat="server" DataSourceID="nav1"
Orientation="Horizontal" StaticSelectedStyle-CssClass="tab-selected"
CssClass="nav"
StaticDisplayLevels="2" IncludeStyleBlock="False" RenderingMode="List" >
</asp:Menu>
</div>
</div>
<asp:ContentPlaceHolder ID="MainContent" runat="server" />
</form>
</body>
The thing with the Site Map is that you are required to have 1 top level siteMapNode which I leave as empty. StaticDisplayLevels is set to 2 in the asp:Menu, so the list is rendered as follows:
<ul class="level1">
<li><a></a></li>
<li><a class="level2 selected" href="/Web%20Portal/Default.aspx">Home</a></li>
<li><a class="level2" href="/Web%20Portal/reports.aspx">Reports</a></li>
<li><a class="level2" href="/Web%20Portal/administration.aspx">Administration</a></li>
<li><a class="level2" href="/Web%20Portal/ChildPage.aspx">Master Template</a></li>
</ul>
As you can see it puts the top level siteMapNode as the first item in the list. Which I do not want. To work around this I hide the element using the following CSS.
#nav li:first-child a
{
display: none;
}
Everything is now looking nice and very similar to the static html pages.
Andez
May be the solution I am trying to provide is not optimized but worked for me.
In the static HTML Menu in the Master page, have all the "li" tag with the ID as of the Page Name.
<ul>
<li ID="home"><a href="home.aspx">Home</li>
<li ID="About"><a href="about.aspx">About</li>
<ul>
Then in the Master Page use JQuery to get the Page Name. Some thing like
$(function () {
var filePath = window.location.pathname;
var fileName = filePath.substr(filePath.lastIndexOf("/") + 1);
var name = fileName.split('.');
var page_name = name[0];
$('#' + page_name).addClass("active"); // Add the CSS class which sets the active Menu
});
In CSS do something like
.active
{
background=#000;
color=#fff;
}
I hope this helps..

is it possible to implement a css template like this one into my jsp's?

I have a site I made using jsp's and I need to stylize it. Can I somehow implement a css template like this one in my jsp's? css template
If so, how?
Thanks!
Yes. It is definitely possible. Here is a tutorial on JSP templates. It teaches you about the include directive, which allows you to include static content such as HTML files like:
<%#include file='header.html'%>
or the template taglib that allows you to include templates with
<%# taglib uri='/WEB-INF/tlds/template.tld' prefix='template' %>
<html><head><title><template:get name='title'/></title></head>
<body background='graphics/background.jpg'>
<table>
<tr valign='top'><td><template:get name='sidebar'/></td>
<td><table>
<tr><td><template:get name='header'/></td></tr>
<tr><td><template:get name='content'/></td></tr>
<tr><td><template:get name='footer'/></td></tr>
</table>
</td>
</tr>
</table>
</body></html>
From a template or from a raw JSP file you can absolutely reference CSS. It's fairly simple. You reference the CSS in your HTML tags in the JSP the same way you would regularly:
<link rel="stylesheet" href="common.css" type="text/css">
For more information see W3.

Resources