display formatted json in the page

To Display formatted json we need to use
<pre> tag and stringify the js object.

Use JSON.Stringify

JSON.stringify(value[, replacer [, space]])


  • value The value to convert to a JSON string.
  • replacer If a function, transforms values and properties encountered while stringifying; if an array, specifies the set of properties included in objects in the final string. A detailed description of the replacer function is provided in the javaScript guide article Using native JSON.
  • space Causes the resulting string to be pretty-printed.

For example:

JSON.stringify({a:1,b:2,c:{d:3, e:4}},null,"    ")

will give you following result:

    "a": 1,
    "b": 2,
    "c": {
        "d": 3,
        "e": 4

Use angularjs built-in filter

If you are using angularjs, you can leverage the build in ‘| json’ filter.

<pre>{{data | json}}</pre>

Note the use of the pre-tag to conserve whitespace and linebreaks

Demo: http://plnkr.co/edit/lwqajlWvJGTaH0HTfnJd?p=preview

There’s also an angular.toJson method, but I haven’t played around with that (Docs)


Struts DispatchAction form with multiple buttons

DispatchAction provides a mechanism for grouping a set of related functions into a single action, thus eliminating the need to create separate actions for each functions. In this example we will see how to group a set of user related actions like add user, update user and delete user into a single action called UserAction.

The class UserAction extends org.apache.struts.actions.DispatchAction. This class does not provide an implementation of the execute() method as the normal Action class does. The DispatchAction uses the execute method to manage delegating the request to the individual methods based on the incoming request parameter. For example if the incoming parameter is “method=add”, then the add method will be invoked. These methods should have similar signature as the execute method.

public class UserAction extends DispatchAction {

 private final static String SUCCESS = "success";

 public ActionForward add(ActionMapping mapping, ActionForm form,
 HttpServletRequest request, HttpServletResponse response)
 throws Exception {
 UserForm userForm = (UserForm) form;
 userForm.setMessage("Inside add user method.");
 return mapping.findForward(SUCCESS);

 public ActionForward update(ActionMapping mapping, ActionForm form,
 HttpServletRequest request, HttpServletResponse response)
 throws Exception {
 UserForm userForm = (UserForm) form;
 userForm.setMessage("Inside update user method.");
 return mapping.findForward(SUCCESS);

 public ActionForward delete(ActionMapping mapping, ActionForm form,
 HttpServletRequest request, HttpServletResponse response)
 throws Exception {
 UserForm userForm = (UserForm) form;
 userForm.setMessage("Inside delete user method.");
 return mapping.findForward(SUCCESS);

If you notice the signature of the add, update and delete methods are similar to the execute method except the name. The next step is to create an action mapping for this action handler. The request parameter name is specified using the parameter attribute. Here the request parameter name is method.

 <action input="/index.jsp" parameter="method" name="UserForm" path="/UserAction" scope="session" type="com.vaannila.UserAction">
 <forward name="success" path="/index.jsp" />

Now lets see how to invoke a DispatchAction from jsp. We have a simple form with three buttons to add, update and delete a user. When each button is clicked a different method in UserAction class is invoked.

<script type="text/javascript">
function submitForm()
document.forms[0].action = "UserAction.do?method=add"
<html:form action="UserAction" >
 <bean:write name="UserForm" property="message" />
 <html:submit value="Add" onclick="submitForm()" />
 <html:submit property="method" value="update" />
 <html:submit property="method" >delete</html:submit>

Now consider the update and the delete button. The request parameter name specified in the action handler is “method”. So this should be specified as the property name for the submit button. The name of the method to be invoked and the value of the button should be the same. So when the button is clicked the corresponding method in the UserAction will be called. The delete button shows an alternate way to specify the value of the button.

Here the main constraint is the method name and the button name should be same. So we can’t have an update button like this “Update”. Inorder to avoid this you can call a javascript function on click of the button. Specify the action and submit the form from javascript. In this way we can have a different button name and method name. On click of the Add button the action value is set to “UserAction.do?method=add” and the form is submitted from javascript.


JAVA Output Alignment

The printf function works fine for space padding. For example:

// % - insert argument value
// , - insert locale-specific separators
// 20 - pad out to 20 spaces
// .2 - fix precision at two decimal places
// s - string argument
// f - float argument
System.out.printf("%20s %20s\n", "Column 1", "Column 2");
System.out.printf("%,20.2f %,20.2f\n", 1034354.54, 243554.4);
System.out.printf("%,20.2f %,20.2f\n", 55553.44, 4325.3);


            Column 1             Column 2
        1,034,354.54           243,554.40
           55,553.44             4,325.30


  • The format specifiers for general, character, and numeric types have the following syntax:

    The optional argument_index is a decimal integer indicating the position of the argument in the argument list. The first argument is referenced by “1$“, the second by “2$“, etc.

    The optional flags is a set of characters that modify the output format. The set of valid flags depends on the conversion.

    The optional width is a non-negative decimal integer indicating the minimum number of characters to be written to the output. When it is negative,  it means align LEFT.

    The optional precision is a non-negative decimal integer usually used to restrict the number of characters. The specific behavior depends on the conversion.

    The required conversion is a character indicating how the argument should be formatted. The set of valid conversions for a given argument depends on the argument’s data type.


Notepad++ format xml file (indent)


To Indent a XML file in notepad++, TextFX > TextFX HTML Tidy > Tidy Reindent XML

If the TextFX menu does not exist, you need to download & install the plugin. Plugins->Plugin Manager->Show Plugin Manager and then check the plugin TextFX Characters. Click ‘install,’ restart Notepad++.

libTidy.dll might not be included in the new version of NPP.

Solution is to download the 5.9 zip, then
copy one of the following folders:
ansi\plugins\Config\tidy or Unicode\plugins\Config\tidy
to your current Notepad++\plugins\Config folder


XML tools

Install the xml tools plugin would be a easier way to format xml. Using Pretty print under Plugins -> xml tools