Generating html with java

Automatically generate HTML pages in Java

I am developing a Java desktop application. I have a need to create HTML pages through my application. When the user clicks on View in Browser button a HTML page should be created with some details and shown it to the user. Is there a way I can do this? Is there are any resources I can use in this situation? Any suggestions are warmly welcome.

Is there a web server to show these html pages ? Is there a FTP/SSH connexion or something like that with it ?

No, I just want a way to show these information to the user. I need only html files so the user can view them in their browser. I have seen some application doing this. They create html pages as reports so users can view them.

5 Answers 5

import java.awt.Desktop; import java.io.*; class ShowGeneratedHtml < public static void main(String[] args) throws Exception < File f = new File("source.htm"); BufferedWriter bw = new BufferedWriter(new FileWriter(f)); bw.write("

Blah, Blah!

"); bw.write(""); bw.write(""); bw.close(); Desktop.getDesktop().browse(f.toURI()); > >

Result on this PC

enter image description here

You should use the package javax.swing.text.html.HTML . E.g., it has JEditorPane . It provides HTML 3.2 support. You should just to set the name of the URL, and the page will be displayed, if a network connection is available. See the example.

You can have a look at — javax.swing.text.html.HTML.Tag It provides some basic functionality. If it is not enough, you can think about using JavaServer Pages.

The Flying Saucer library is a pure Java library for rendering XML, XHTML, and CSS 2.1 content. Started by a member of the Java Swing team.

Build html using any of these libraries like jsoup, wffweb, j2html, jwebutils etc.. (referred from SO) and write it to a temporary file then call Desktop.getDesktop().browse(file.toURI());

Читайте также:  Ni4osi myarena ru ni4osi index php

May be something like this if we use wffweb

Html html = new Html(null) >; >>; File tempFile = File.createTempFile("temporary_html", "html", new File("/home/username/tmpdir")); html.toOutputStream(new FileOutputStream(tempFile)); Desktop.getDesktop().browse(tempFile.toURI()); 

Источник

Fast and fluent Java HTML5 builder

Import TagCreator to get started. j2html’s syntax is fluent and closely matched to HTML:

import static j2html.TagCreator.*; public class Main < public static void main(String[] args) < body( h1("Hello, World!"), img().withSrc("/img/hello.png") ).render(); >> 

The Java code above becomes the HTML below:

 

Hello, World!

Intended use

Consider using j2html if:

  • You love type safety. You love catching errors at compile time instead of waiting for some poor user to notice that something is wrong
  • You like to dynamically reuse your view code
  • You think template engines are too slow. This index page was rendered 100 000 times in less than a second on an i5-4670. That’s about a thousand times faster than Apache ‘Velocity’ (hah!)

Be careful about using j2html if:

  • You don’t know Java and HTML well
  • You’re creating a classic «website» that has a lot of static HTML (if it’s all generated then it’s fine)
  • Your application has a lot of text and you don’t use language files / a database (string concatenation would get very annoying)
  • You use a CSS framework which relies on a lot of copy pasting HTML from docs. You’ll have to translate these snippets to j2html

Why did you make this library?

First: j2html is a Java HTML builder. It’s not a template engine, and it doesn’t want to compete with template engines. We were looking for a good way to create HTML for a complex login solution which had many different forms (with different configurations, depending on user state and user actions, etc.), but very little actual HTML per page. The result was j2html. We decided to release the Java HTML builder we made, since it seems better than all the other Java HTML builders we found while researching the subject. Hopefully someone will find it useful!

A static page generator or a template engine would be better suited than a HTML builder for creating this page, but we had to do it.

Читайте также:  Php output error messages

Источник

How can I generate html from Java object?

I have a rest resource with a method that should return some data as html. We’re talking about a list of Reports, which is a simple class consisting of 6 string member variables. I am asking because the prospect of appending elements and values to a string seems slow and error prone, and I would really prefer to do it in an object oriented way.

3 Answers 3

For simple HTML, generate it directly as text. The other suggestions of serializing, XML, and transformations are all overkill.

There are Java libraries to help with generating HTML, such as these:

  • jwebutils
    A library for creating HTML 5 markup using Java. It also contains support for creating JSON and CSS 3 markup.
  • Jakarta Element Construction Set (ECS)
    A Java API for generating elements for various markup languages it directly supports HTML 4.0 and XML. Now retired, but some folks really like it.

But if you learn the basics of valid HTML, you can write your own routines.

As for speed, the StringBuilder class was added for the purpose of faster string manipulation. The tradeoff is no synchronization. That means not thread-safe. Depending on how your program is structured, you might use synchronization around the outside of the code doing the HTML rendering.

Here is an example class I just cooked up in Java 6, and a little app to run it. This code is just a proof of concept, not ready for prime time production.

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 ***" ); > > 

I tried to post the rendered HTML here, but StackOverflow tried to interpret it as HTML rather than display it.

Читайте также:  Вставьте html код этой рекламы

Bonus tip: You can use single-quote (APOSTROPHE Unicode 39) rather than double-quote (QUOTATION MARK Unicode 34) in your HTML & CSS for delimiting attribute values and such. Browsers handle both well. The single-quote makes coding so much easier in your Java code, eliminating the need to escape the double-quote marks. Notice my usage in code above, such as the ‘en’ and ‘utf-8’ .

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Java to HTML generator. Enjoy typesafe HTML generation.

License

tipsy/j2html

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Java to HTML generator. Enjoy typesafe HTML generation.

dependency> groupId>com.j2htmlgroupId> artifactId>j2htmlartifactId> version>1.6.0version> dependency>
compile 'com.j2html:j2html:1.6.0' 

Import TagCreator and start building HTML

import static j2html.TagCreator.*; public class Main < public static void main(String[] args) < body( h1("Hello, World!"), img().withSrc("/img/hello.png") ).render(); > >

The above Java will result in the following HTML:

body> h1>Hello, World!h1> img src pl-s">/img/hello.png"> body>

About

Java to HTML generator. Enjoy typesafe HTML generation.

Источник

Оцените статью