JSP as a template for plain text

I have a program that currently has a ton of code that I would like to debug. This code takes several text files and passes it through an interesting written interpreter to create a report in the form of a simple text file, which is sent to other systems. Theoretically, this allows a non-programmer to be able to modify the report without having to understand the internal workings of Java and the interpreter. In practice, any minor changes probably require a transition to the interpreter and its settings (and the domain-specific language is not entirely friendly even to other programmers).

I would like to reverse engineer this code. As a web programmer in the first place, the first thing that came to mind when we thought that β€œa non-programmer can change a report ...” I replaced the report with a web page and said to myself β€œah-ah-ah aa ". That would give me the good of what you see, almost what you get for people along with taglibs and java scriptlets (as undesirable as they might be later), and not with awkwardly written DSL statements.

While you can use jspc to compile jsp in java (the other part of the application runs ejbs on the jboss server, so jspc not too far away), the template code that it uses is trying to connect the output to pagecontext from the servlet context. This involves using code in the sense that it works inside a web container (not impossibility, but kluge), and then removes the headers.

Is there another template approach (or library) for java that can be used to print to a text file? Everything that I looked at is either optimized for the Internet or closely connected with a specific application server (and designed to work on the Internet).

+4
source share
4 answers

So you need a small version of JSP.

See if this one works (JSTP) for you

http://jstp.sourceforge.net/manual.html

+3
source

Give Apache speed . It is incredibly simple and does not assume that it works in the context of a web application.

This is completely subjective, but I would say that the syntax for a non-programmer is easier to understand than the JSP and tag libraries.

+3
source

If you want to become a real tread developer in your company, you can create a Grails application to use it and use Groovy templates (perhaps in combination with the Quartz plugin for planning), it can be a little difficult to sell if there is a lot of existing code to replace, but I like it...

http://groovy.codehaus.org/Groovy+Templates

If you need a safe bet, then (also excellent) speed should be like this:

http://velocity.apache.org/

+1
source

You probably want to test the rhythm pattern mechanism , with good performance (2 to 3 times faster than speed) and elegant syntax (.net Razor like), and was developed specifically for the Java programmer.

Template, generate a string of usernames separated by "," from the list of users

 @args List<User> users @for (User user: users) { @user.getName() @user_sep } 

Template: if-else demo

 @args User user @if (user.isAdmin()) { <div id="admin-panel">...</div> } else { <div id="user-panel">...</div> } 

Calling a Template Using a Template File

 // pass render args by name Map<String, Object> renderArgs = ... String s = Rythm.render("/path/to/my/template.txt", renderArgs); // or pass render arguments by position String s = Rythm.render("/path/to/my/template.txt", "arg1", 2, true, ...); 

Invoke a template using inline text

 User user = ...; String s = Rythm.render("@args User user;Hello @user.getName()", user); 

Invoke a string interpolation pattern

 User user = ...; String s = Rythm.render("Hello @name", user.getName()); 

ToString mode

 public class Address { public String unitNo; public String streetNo; ... public String toString() { return Rythm.toString("@_.unitNo @_.streetNo @_.street, @_.suburb, @_.state, @_.postCode", this); } } 

Auto ToString mode (follow apache commons lang reflectionToStringBuilder, but faster than it)

 public class Address { public String unitNo; public String streetNo; ... public String toString() { return Rythm.toString(this); } } 

The document can be found at http://www.playframework.org/modules/rythm . The full demo application runs on GAE: http://play-rythm-demo.appspot.com .

Note. Demo and doc are created for the play-rythm plugin for Play! Framework, but most of the content also applies to the clean rhythm template engine.

Source:

+1
source

Source: https://habr.com/ru/post/1434668/


All Articles