How can I generate html from a java object?

I have a rest resource with a method that should return some data as html. We are talking about a list of reports, which is a simple class consisting of 6 string member variables.

I ask because the prospect of adding elements and values ​​to a string seems slow and error prone, and I would rather do it in an object oriented way.

+5
source share
3 answers

The usual way to generate HTML for a web response is to use JSP or Java templates such as Velocity or FreeMarker.

XML, XSLT XML HTML. XSLT ( ), XML, XSLT.


, , , HTML- Java-. JSP, , HTML ; .

+4

HTML, . , XML .

Java, HTML , :

HTML, .

, StringBuilder . . , . , , , HTML.

, Java 6, . , .

package com.example;

/**
 * @author Basil Bourque
 *         © 2012 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
 */
public class ListToHtmlTransformer {

    /**
     * @param collection
     *            of report titles.
     * @return string containing source code for HTML5.
     */
    public String render( java.util.Collection< String > reports ) {
        // This source code is not yet tested or made bullet-proof. Only meant for demonstrating concepts.
        // Warning: This code is not thread-safe. Changes must be made before serious use.
        // Warning: This code should be modified to produce proper HTML, such as escaping certain characters.
        // This code generates minimal HTML5 as suggested here: http://www.brucelawson.co.uk/2010/a-minimal-html5-document/
        // Big tip: Note that HTML allows the use of apostrophe (single-quote) in place of double-quote. Mixes better with Java source code.
        // The backslash + 'n' is an escape sequence in Java to generate a linefeed (Ascii/Unicode 10) for use here as a NewLine.
        // In real life, you woud test your rendered HTML with an HTML validator such as:
        // • http://html5.validator.nu/
        // • http://validator.w3.org/
        StringBuilder html = new StringBuilder();
        html.append( "<!doctype html>\n" );
        html.append( "<html lang='en'>\n" );

        html.append( "<head>\n" );
        html.append( "<meta charset='utf-8'>\n" );
        html.append( "<title>Report of Reports</title>\n" );
        html.append( "</head>\n\n" );

        html.append( "<body>\n" );
        html.append( "<h1>List of Reports</h1>\n" );
        // Make a list in HTML
        html.append( "<ul>\n" );
        // Loop the list of reports passed as argument.
        for ( String report : reports ) {
            html.append( "<li>" + report + "</li>\n" );
        }
        html.append( "</ul>\n" );
        html.append( "</body>\n\n" );

        html.append( "</html>" );

        return html.toString();
    }

}

.

package com.example;

import java.util.ArrayList;

/**
 * 
 * @author Basil Bourque
 *         © 2012 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
 */
public class App {

    /**
     * @param args
     */
    public static void main( String[] args ) {
        ArrayList< String > listOfReports = new ArrayList< String >();
        listOfReports.add( "Some report #1" );
        listOfReports.add( "Some report #2" );
        listOfReports.add( "Some report #3" );
        listOfReports.add( "Some report #4" );
        listOfReports.add( "Some report #5" );
        listOfReports.add( "Some report #6" );

        ListToHtmlTransformer renderer = new ListToHtmlTransformer();
        String renderedHtml = renderer.render( listOfReports );

        System.out.println( "The following HTML was rendered: " + new java.util.Date().toString() );
        System.out.println( renderedHtml );
        System.out.println( "*** End of HTML ***" );
    }

}

HTML , StackOverflow HTML, .

: (APOSTROPHE Unicode 39), (QUOTATION MARK Unicode 34) HTML CSS .. . Java, . , 'en' 'utf-8'.

+7

You can serialize the object in XML and then provide XLST to generate HTML output. But if it's just 6 bites, I would go ahead and just concatenate the strings.

0
source

All Articles