I have a little problem with my app in Spring MVC. I want to edit users data in my app. So I have edit controller which has listWorkers, redirectWorker and editWorker method.
#RequestMapping("/print")
public String listWorkers(Model model)
{
model.addAttribute("workerList", workerService.getAllWorkers());
return "print";
}
#RequestMapping("/edit")
public String redirectWorker(HttpServletRequest request)
{
String parameter = request.getParameter("workers");
String path = "redirect:/edit/" + parameter;
return path;
}
#RequestMapping("/edit/{worker}")
public String editWorker(#PathVariable("worker")
String login, Model model)
{
model.addAttribute("worker", workerService.getWorker(login));
return "edition";
}
I have a problem with resources and images folders. When I'm using for example, print method everything is good but when I try to use editWorker method my logo and css files are not loading. I have a request mapping for this folders in servlet-context file:
<resources mapping="/resources/**" location="/resources/" />
<resources mapping="/images/**" location="/images/" />
When I'm using print method I have URL like this:
http://localhost:8080/WWP/print
and I can see image and style.
But when I'm using edit method on my specific user I have URL like this:
http://localhost:8080/WWP/edit/caro
And in this way I can't see image and style. I have a warn:
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/WWP/edit/resources/images/logo.png] in DispatcherServlet with name 'appServlet'
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/WWP/edit/resources/styles/menu.css] in DispatcherServlet with name 'appServlet'
I have images and styles catalogues in resource folder which is directly in webapp folder (cause I'm using maven). It works earlier, but when I have something like /*/* it doesn't work. I suppose that something is wrong with resurces mapping in my servlet-context file.
I' m loading my css and image file like this:
<img src="resources/images/logo.png">
<link rel="stylesheet" href="resources/styles/menu.css" type="text/css"></link>
Thanks in advance for your help.
Don't use relative paths. Use absolute paths instead:
<img src="<c:url value='/resources/images/logo.png'/>">
<link rel="stylesheet" href="<c:url value='/resources/styles/menu.css'/>" type="text/css"></link>
The JSTL <c:url> tag makes sure the context path is prepended to the absolute path passed as argument in the value attribute, so if your webapp is deployed to http://localhost:8080/WWP, the generated HTML code will be
<img src="/WWP/resources/images/logo.png">
<link rel="stylesheet" href="/WWP/resources/styles/menu.css" type="text/css"></link>
The <c:url> tag should also be used for every other URL: anchor hrefs, form actions, etc.
Related
I am in the process of learning Spring Boot and became unstuck when trying to post model data to an HTML file.
I have a controller, where I populate the model and call an HTML page from. When I put a breakpoint inside this method, the model data gets populated correctly, but the HTML page is rendered with only the name of the HTML file, and nothing else (no browser errors either). I am thinking it may have something to the with the file structure, and the fact that my RestController already has a path specified (because when I create a clean new controller with no explicit class-based #RequestMapping specified and call the template from the root path + name of HTML file, it renders correctly). I do have the ThymeLeaf dependency installed, and "userView.html" is placed inside the "template" directory.
ReaderController.java extract:
#RequestMapping("/reader")
public class ReaderController {
...
#RequestMapping(value = "/userView")
public String getUser(Model model) {
// business logic goes here
model.addAttribute("userName","Somebody");
model.addAttribute("url", "www.example.com");
return "userView";
}
userView.html extract:
<body>
<h1>User Data</h1>
<p th:text="'Username: ' + ${userName}"/>
<p th:text="'Url: ' + ${url}"/>
</body>
http://localhost:8080/reader/userView only renders the word "userView".
I found the solution to the issue. I inadvertently used the #RestController annotation instead of the #Controller annotation to the controller class. This link helps to explain the issue.
I am trying to call a REST endpoint and then display a ThymeLeaf template:
The Endpoint:
#GetMapping("/devices")
public String getDeviceDetailU(Model model) {
List<FinalDevice> devices = deviceService.getAll();
model.addAttribute("devices", devices);
return "deviceList";
}
For the endpoint I tried returning /deviceList, /deviceList.html, deviceList.html.
Whenever I navigate to the endpoint, I simply get the string that was returned.
Here is the template:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org"
xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<body>
Hello World!
</body>
</html>
While I understand, at this point, it will not display the list, I just want to be forwarded to the template.
If I go to localhost:8080/deviceType I display that template. This to me indicates it is not a security or configuration issue.
Any ideas?
This should all work according to this tutorial.
You probably have #RestController instead of just a #Controller.
If you want templates to be rendered you need to use #Controller. #RestController means that all your #Mappings simply serialize the return value and output it as json or xml (which is why you are seeing the string deviceList instead of the template).
I'm trying to write a docpad plugin that will allow me to insert meta tags unique to each page, for example og:title or og:description. I've been able to accomplish this globally with the populateCollections event for global values, but have not been able to do this per page.
I'd like for this to work without the need for a template function so that the meta tag is inserted automatically based on the document's meta. One way might be to grab the contentRendered value in the writeBefore event and do string manipulation that way, but that seems hacky.
Any ideas?
This worked for what I needed. Basically, I'm getting the rendered content right before the file is written using the writeBefore event, and doing a very simple string replace which adds the meta tags and their unique values, which is pulled from the model in the collection.
writeBefore: (opts) ->
docPad = #docPad
templateData = docpad.getTemplateData()
siteUrl = templateData.site.url
for model in opts.collection.models
if model.get('outExtension') == 'html'
url = #getTag('og:url', siteUrl+model.get('url'))
title = #getTag('og:title', model.get('title'))
content = model.get('contentRendered')
if content
content = content.replace(/<\/title>/, '</title>'+url+title+description)
model.set('contentRendered', content)
# Helper
getTag: (ogName, data) ->
return "\n <meta property=\"#{ogName}\" content=\"#{data}\" />"
Great answer David, leaving this one if someone faced the same issue I did.
Check if meta tag is broken, if it is - don't render:
renderBefore: (opts) ->
for model in opts.collection.models
if model.get('date').toLocaleDateString()=='Invalid Date'
model.set('write', false)
docpad.log model.get('title')+' has broken date format!\n\n\n\n\n'
false
I am using partials in with collections. Adding what is needed in the document like this:
```
title: Meetings and Events
layout: page
description: "This is my custom description."
tags: ['resources']
pageOrder: 3
pageclass: rc-events
```
I needed a custom CSS class by page. Then you can call it in your default template like this.
<div id="main" class="container <%= #document.pageclass %>">
Should be the same for meta
<meta name="description" content="<%= #document.description) %>" />
or check your docpad.coffee file and put together helper function for prepared content based off of a default site value combined with a #document value. Then you can just call something like the default:
<meta name="description" content="<%= #getPreparedDescription() %>" />
Which is built by this helper function:
# Get the prepared site/document description
getPreparedDescription: ->
# if we have a document description, then we should use that, otherwise use the site's description
#document.description or #site.description
I am trying to get Apache Tiles' put-list-attribute to work with Thymeleaf.
Here is what I tried:
From Tiles config:
<put-list-attribute name="jsFilesList">
<add-attribute value="/js/libs/jquery-1.8.1.js"/>
<add-attribute value="/js/libs/jquery-ui-1.9.0.custom.js"/>
<add-attribute value="/js/libs/bootstrap.js"/>
</put-list-attribute>
From thymleaf template:
<script th:each="jsFile : ${jsFilesList}" th:src="#{${jsFile}}" type="text/javascript" ></script>
Nothing is rendered... It seems the list is empty from the thymeleaf template point of view...
Can anyone please help?
Tiles attributes are not by default available in the request scope. They live in their own tiles scope.
In jsps, to expose a tiles attribute from its scope to the request scope do the following:
<%# taglib prefix="tilesx" uri="http://tiles.apache.org/tags-tiles-extras" %>
<tilesx:useAttribute id="jsFilesList" name="jsFilesList"/>
I would like to convert BufferedImage to an image that will display on JSP page. How can I achieve this?
First, JSP is a view technology providing a template to write HTML/CSS/JS in and the ability to interact with backend Java code to control page flow and access backend data. Your problem is more in HTML.
Now, to display an image in a HTML page, you need the HTML <img> element. To define/allocate an image, you just have to let the src attribute point to an URL. E.g.
<img src="url/to/image.jpg" />
(it can be either relative to the current context, or an absolute URL, e.g. starting with http://)
If the image is dynamic, as in your case, you need to have a Servlet which listens on the url-pattern matching the image URL. E.g.
<img src="imageservlet/image.jpg" />
(here the servlet is obviously to be mapped on an URL pattern of /imageservlet/* and the image identifier, here the filename, is here available by request.getPathInfo())
The <img src> will fire a GET request, so you just have to implement doGet() method of the servlet. To send a HTTP response all you need to do is to write some content to the OutputStream of the response, along with a set of response headers representing the content (Content-Type, Content-Length and/or Content-disposition). You can use ImageIO#write() to write a BufferedImage to an OutputStream.
You can find a basic example of such an image servlet here. You just have to replace Files#copy() with ImageIO#write().
response.setContentType("image/png");
ImageIO.write(bufferedImage, "png", response.getOutputStream());
As a completely different alternative, you can also let the servlet convert the image to a Base64 encoded string and pass it on to the JSP:
ByteArrayOutputStream output = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", output);
String imageAsBase64 = Base64.getEncoder().encodeToString(output.toByteArray());
request.setAttribute("imageAsBase64", imageAsBase64);
request.getRequestDispatcher("/WEB-INF/some.jsp").forward(request, response);
And finally show it in the forwarded JSP using the data URI scheme as below:
<img src="data:image/png;base64,${imageAsBase64}" />
You only need to keep in mind that this doesn't give the server nor the client the opportunity to cache the image. So this approach is plain inefficient in case the image is not temporary.
See also:
How to retrieve and display images from a database in a JSP page?
Simplest way to serve static data from outside the application server in a Java web application
You need not convert BufferedImage to Image to display it on the jsp page. Because, Java 6 JAXB provides javax.xml.bind.DatatypeConverter.printBase64Binary(byte[]) String to convert byte[] in to base 64 string. The base 64 string can be displayed using the <img html tag by specifying the source data as base 64 i.e. src="data:image/jpg;. Here is the sample program referred from this post.
sample.jsp (test passed) :
<%#page import="java.awt.image.BufferedImage"%>
<%#page import="javax.imageio.ImageIO"%>
<%#page import="java.io.*"%>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
BufferedImage bImage = ImageIO.read(new File("/home/visruth/Desktop/Visruth.jpg"));//give the path of an image
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write( bImage, "jpg", baos );
baos.flush();
byte[] imageInByteArray = baos.toByteArray();
baos.close();
String b64 = javax.xml.bind.DatatypeConverter.printBase64Binary(imageInByteArray);
%>
<div>
<p>As of v6, Java SE provides JAXB</p>
<img src="data:image/jpg;base64, <%=b64%>" alt="Visruth.jpg not found" />
</div>
</body>
</html>
IMO, this approach is perfect for small sized images like <img src="data:image/gif;base64,R0lGODlhAQABAIAAAHd3dwAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==" width="200" alt="thumbnail" height="200">. Otherwise using direct url of the image will be fine in src attribute eg:- <img src="uri-of-image/profile-pic.jpg" width="600" alt="No Profie Pic" height="600">