I am using Quarkus Mailer and Quarkus Template to create an endpoint that will be responsible just for sending emails. For now it just receives the subject, body and the emails that the email should be sent to. I am using Quarkus Template so that I have a base html template for all emails. However I want to be able to pass html through the endpoint so that I am able to render different styles in the content of the template.
This is the part of the template where the body is rendered:
<tr style='mso-yfti-irow:5;height:343.95pt'>
<td width=621 valign=top style='width:466.05pt;border-top:none;border-left: solid #0E133C 2.25pt;border-bottom:none;border-right:solid #0E133C 2.25pt; padding:2.0cm 1.0cm 1.0cm 1.0cm;height:343.95pt'>
<p class=MsoNormal><span lang=PT style='mso-ansi-language:PT'>{sendEmailRequest.getBody()}<o:p></o:p></span></p>
</td>
</tr>
Basically the "sendEmailRequest.getBody()" has the html content and it is currently being rendered like this:
This is the code used to send the email:
public void sendEmail(final SendEmailRequest sendEmailRequest) {
final String html = template.data("sendEmailRequest", sendEmailRequest).render();
mailer.send((new Mail()).setSubject(sendEmailRequest.getSubject())
.setHtml(html)
.setTo(sendEmailRequest.getTos()));
}
Keep in mind that I want to keep using a base template for the email and not directly use the body received from the DTO as the whole email body. I already managed to use html from the endpoint, but that was wihtout using the template.
I posted the same question as an issue in the Quarkus repository and the solution was provided.
You can check it ou here: https://github.com/quarkusio/quarkus/issues/23893
The solution:
You need to output the unescaped value: (1) either use the raw or safe computed properties in the template ({content.raw} or {content.safe}) or (2) wrap the String value in a io.quarkus.qute.RawString (template.data("content", new RawString(content)).render()).
You can use #eval in your template to display raw html
{#eval htmlContent /}
Related
Could you help me to come up with solution.
There are JSP-page which sends form parameters to servlet.
Usually I parse parameters by HttpServletRequest.getParameter() which works fine for forms with tiny parameter numbers.
Now I'm developing application which has a lot of JSPs with number of parameters and the standard way of form processing is inconvenient.
I think that possible solution might be by using -action.
I don't understand whether it works for me.
I browsed a lot of materials but find nothing about it.
I mean that there is any information regarding possibility to get form parameters in jsp by ,
automatically create instance of the entity class,
map all the parameters to entity-properties and send the entity-instance to the servlet.
Please take a look at the code:
index.jsp
<html>
<head>
<title></title>
</head>
<body>
<form method="post" action="NewFormServlet" enctype="application/x-www-form-urlencoded">
<jsp:useBean id="client-bean" class="model.entity.Client" scope="request"/>
<h3>Please enter client information</h3><br>
Client first name<input type="text" name="first-name"/><br>
<jsp:setProperty name="client-bean" property="firstName" value="${requestScope.first-name}"/>
Client last name<input type="text" name="last-name"/><br>
<jsp:setProperty name="client-bean" property="lastName" value="${requestScope.last-name}"/>
Client address<input type="text" name="address" size="100"/><br>
<jsp:setProperty name="client-bean" property="address" value="${requestScope.address}"/>
Client city<input type="text" name="city"/><br>
<jsp:setProperty name="client-bean" property="city" param="${requestScope.city}"/>
Client postal code<input type="text" name="postal-code"><br>
<jsp:setProperty name="client-bean" property="postalCode" value="${requestScope.postal-code}"/>
<input type="hidden" name="jsp-identifier" value="client-form">
<input type="submit" value="Submit">
</form>
</body>
</html>
What is incorrect in this code? Thank you in advance.
You should first think about what occurs on server and what occurs in browser, as well as what is transmitted via HTTP. A form submission uses many phases :
on server : the JSP is executed using servlet context, session, and request attributes, with still full access at the previous request (parameters, ...) => all that generates a HTML page (with eventually css or javascript linked or included)
on browser : the browser gets and parses the HTML page, optionnaly gets linked resources (images, etc.), and display the form to the user
on browser : the user fills the input fields of the form and clicks the input button
on browser : the browser collates data form input fields, generate an new HTTP request (usually a POST one) and sends it to server
on server : the servlet container pre-processes the request (until that is is only a stream of bytes conforming to HTTP protocol) and calls the appropriate servlet method with a new HttpServletRequest reflecting current HTTP request, and a HttpServletResponse to prepare what will be sent back to browser after processing
All that means that anything you can do to request attributes in the JSP part will be lost at the time of processing of the submitted form by the servlet. You can only rely on session attributes, and on input form fields that will be accessible as request parameters.
So with your current JSP, the Servlet will find nothing in request attributes (it is a different HttpServletRequest) and will only be able to use parameters with names firstName, lastName, address, city, etc.
I can understand it is not the expected answer, but HTTP protocol is like that ...
EDIT per comment :
You can put the attribute in session, and then the servlet will use the same session as the JSP. But read again what I wrote above and think when things happen :
on server, when executing the JSP, you create an empty Client bean that you put in session scope, and use its value to initialize the form fields. Stop for the server part
on client, user fills the input fields - the server knows nothing on that - and submit the form through a new request
on server, the servlet has the values in request parameters, but the session still contains the previous values and so the Client bean has null values
I'm sorry but there's not enough magic for the server to automatically find in its attributes (either request or session) what comes from form submission : it only exists in request parameters, and it is the servlet job to process them and eventually put them in attributes.
Edit:
It appears that jsp:useBean is an old school way to collect up a group of parameter values for easier display on a page.
It does not add an attribute when the request is posted.
Based on that,
I see little value in the jsp:useBean tag,
since you can use el expressions to access attributes that you set in a servlet.
This does not help you get the posted parameter values into a bean in the servlet.
You can write a method on the bean to extract the parameter values from the request (visitor pattern).
For example:
class bean
{
private String value;
public void loadFromHttpServletRequest(final HttpServletRequest request)
{
value = request.getParameter("value");
}
}
Consider using a package like spring-mvc.
I am learning about play 2.0 and I have got a question on the form helpers.
For me it all comes down to what benefit is it actually giving by using it in the templates? Am I using it correctly?
First: using the form helper:
#form(action = routes.Application.addAccount("blank")) {
#inputText(accountForm("id"))
<input type="submit" name="action" value="submit ID"/><br />
}
Why is that better then just defining
Enter your id "<input type="text" name="id"/>"
I know I can use the form model to help with validation on the server side. - that's where I see the great benefits of form helper. But where does it help to actually include the form in the Scala template? Can i use the form helper to automatically generate useful things in the html like client side validation, etc?
Cheers
it helps you generate a lot more than just tags. From the documentation:
You feed them with a form field, and they display the corresponding HTML form control, with a populated value, constraints and errors
and
A rendered field does not only consist of an tag, but may also need a and a bunch of other tags used by your CSS framework to decorate the field.
(http://www.playframework.com/documentation/2.2.x/JavaFormHelpers)
So instead of
<label for=...>
... error mesages
<input ...
</label>
you have just one readable line
#inputText(accountForm("id"))
EDIT:
It will also read constraints on your java beans, e.g
#Constraints.Required
#Constraints.MinLength(5)
public String firstName;
and use html5 browser validations and display the coinstraints to the user.
(http://www.playframework.com/documentation/2.2.x/JavaForms)
I am working on a functionality where the application needs to generate user specific emails. This will be setup or configured on the user level using a email template which essentially contains a SQL query, column model, data type, subject, header, footer etc. The template serves as the dataset and layout for the email.
Now using this XML template I need to generate the HTML email. The application will read the XML, execute the SQL query and then match the resultset to the column model. Beyond this; is there any framework or API that can help generate the HTML response (nicely formatted css table) from Java objects or it has to be cooked using raw HTML tags (, etc.)?
I was also researching to see if BIRT or Jasper can provide HTML response but it doesn't seem like they are meant for that. If anyone has experience building a solution for such a use case please let me know.
Take a look at Thymeleaf. It's a HTML template engine.
It's as simple as this:
ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
resolver.setTemplateMode("HTML5");
resolver.setSuffix(".html");
TemplateEngine templateEngine = new TemplateEngine();
templateEngine.setTemplateResolver(resolver);
final Context context = new Context(Locale.CANADA);
String name = "John Doe";
context.setVariable("name", name);
// add more objects from your ResultSet
final String html = templateEngine.process("myhtml", context);
with a myhtml.html file:
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-3.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>My first template with Thymeleaf</title>
</head>
<body>
<p th:text="${name}">A Random Name</p>
</body>
</html>
Here the placeholder ${name} will replace the value A Random Name in the <p> element by the value you inserted in the context.
As for your requirement of reading and generating a table, Thymeleaf provides constructs to loop as many times as is required (ie. as long as you have data remaining). Example:
<tr th:each="prod : ${allProducts}">
will iterate through allProducts, assigning each object to the variable prod at each iteration. Take a look at the tutorials and the docs for more.
Notice, you have to write the HTML yourself.
Take a look at this answer for generating HTML report through Jasper
You can use XSLT to transform your XML to HTML. The result of your SQL query would have to be inserted as XML beforehand.
I'm trying to call render from a controller for a tag (function) inside a template instead of the template. This way I could use it for partial renderings of a page from ajax calls. Of course I could separate the components of the form in several templates an call render on those but I think it would be cleaner the other way.
What I was trying to do is like the following:
formpage.scala.htm
#()
<html>
...
#content
...
</html>
#**********************************
* Helper generating form *
***********************************#
#content() = {
<h3 class="form-heading">#Messages("employees")</h3>
#form(routes.AppController.save()) {
#inputText...
...
}
And using ajax render the content function, without having to separate it to a
separate file. This way I could render portions of the template without fragmenting it
in multiple files.
De facto the tag is just smaller template, so you can use tags for both - using in templates and controllers, the simplest sample:
/app/views/tags/mytag.scala.html
This is my tag...
In controller can be rendered as:
public static Result createFromTag(){
return ok(views.html.tags.mytag.render());
}
In other template you just to insert:
....
And there is my tag rendered
<b>#tags.mytag()</b>
More flexibility
Of course as it's template ergo Scala function as well you can just pass some params to it or even Html body:
/app/views/tags/othertag.scala.html
#(headline: String)(body: Html)
<h3>#headline</h3>
<div class="tagsBody">
#body
</div>
In controller can be rendered as:
public static Result createFromTag(){
return ok(
views.html.tags.othertag.render(
"Head from controller",
new play.api.templates.Html("This code becomes from <i>controller</b>")
)
);
}
(of course you can import these two for shorter code in action import play.api.templates.Html; and import views.html.tags.othertag)
Finally in your template you can use the tag as:
And there is my tag rendered <br/>
#tags.othertag("Head from template"){
some content for the tag's body from <b>The Template!</b>
}
final.
You'll find tags description in documentation.
I am new to JSP and Servlets.
What i want to know is the best way to pass some customized message to client web pages.
For example suppose i have a web page say student.jsp which has a form,to register a new student to our online application.after successfully inserting all the fields of the form,
user submits the form and data is submitted to our servlet for further processing.Now,Servlet validates it and add it to our database.so,now servlet should send a message indicating a
successful insertion of data entered by end user to end user (In our case student.jsp).
So,i could i pass this type of message to any client web page.
I don't want to pass this message as URL query String.
is there ant other better and secure way to pass these type of messages ...
use request.setAttribute("message", yourMessage) and then forward (request.getRequestDispatcher("targetPage.jsp").forward()) to the result page.
Then you can read the message in the target page via JSTL (<c:out value="${message}" />) or via request.getAttribute(..) (this one is not preferable - scriptlets should be avoided in jsp)
If you really need response.sendRedirect(..), then you can place the message in the session, and remove it after it is retrieved. For that you might have a custom tag, so that your jsp code doesn't look too 'ugly'.
I think it looks like this in JSTL:
<c:remove var="message" scope="session" />
I also think that, if "message" is a Java String, it can be set to the empty string after it's been used like this:
<c:set var="message" scope="session" value="" />
Actually, it also looks like it works if "message" is an array of Java Strings: String[]...