Freemarker missing spring message - spring

I am loading asynchronously (Ajax call to Spring MVC Controller) a free marker template. But i get error message complaining about missing message property.
This is my Spring controller method
#RequestMapping('/messages/{messageId}')
public String conversation(#PathVariable('messageId') Long messageId, ModelMap model) {
//BUILD CONVERSATION MODEL model.put....
"async/conversation"
}
async/conversation.ftl
.....<#spring.message "user.conversation.title"/>.....
I have message properties with
user.conversation.title Conversation
This message is working just fine in other scenario, i think there is some issue loading the template asynchronously.

Solved: I had forgotten to make an import.
<#import "/spring.ftl" as spring/>

Related

Getting Json response in Spring MVC using pebble template

I'm using pebble template for my Spring MVC application. Is there a way to return JSON response object back instead of HTML template to the client for a particular API call?
When I try to send the JSON object I'm getting the following exception.
com.mitchellbosecke.pebble.error.LoaderException: Could not find template "templates/test/1.html" (?:?)
at com.mitchellbosecke.pebble.loader.ClasspathLoader.getReader(ClasspathLoader.java:74)
at com.mitchellbosecke.pebble.loader.ClasspathLoader.getReader(ClasspathLoader.java:26)
....
....
Caused by
org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
I just needed to add #ResponseBody in my method and that fixed the issue.

Displaying entity level error messages in thymeleaf

I have written a custom annotation and placed the annotation on the entity level.
#ValidConfirmPassword
public class User {...}
The custom validator is returning the invalid message, but, I am not able to display the message on the UI.
How should I display the error messsage specific to the entity using Thymeleaf?

Custom Logging Interceptor is not getting triggered

We are developing a RESTFUL services in spring framework and the log framework we are using is apache CXF.
We get the request in the form of JSON from the consumers of our services and we need to mask some of the contents of JSON before printing in the log files.
We are trying to have a custom interceptor to intercept the logger messages but the request never reaches the custom interceptor class. Can you please provide your thoughts to resolve the issue.
Below are the additional details :
public class CustomLogInterceptor extends LoggingInInterceptor {
public String transform(String originalLogString) {
// Custom logic will be here to mask the originalLogString
}
}
spring context XML changes:
The CustomLogInterceptor class is included in the spring context XML file.
Any help is greatly appreciated!

How to send the send status code as response for 404 instead of 404.jsp as a html reponse in spring?

I created web application in spring and handled exception mappings for 404 and 500.If system doesn't find any resources for requested URL it redirects into custom 404.jsp instead of regular 404 page.Everything works fine till have decided to add a webservice in my app.I have included one more controller as a webservice there is no view for this controller and this needs to be invoke through curl command.
User may get into change the curl script.If they changed the URL it should show 404 status code.But it returns the custom 404.jsp as a html response instead of status code.Because dispatcher servlet will takes all urls with /*.
How I can solve this issue?
Please share your suggestions.
Spring 3.2 introduced the #ControllerAdvice, and as mentioned in the documentation:
It is typically used to define #ExceptionHandler
That means you can use the #ControllerAdvice to assist your #Controller like the following:
#ControllerAdvice
class GlobalControllerExceptionHandler {
#ResponseStatus(HttpStatus.NOT_FOUND) // 404
#ExceptionHandler(Exception.class)
public void handleNoTFound() {
// Nothing to do
}
}
For further details please refer to this tutorial and this answer.

How to get Spring mvc validation messages without using messages.properties?

I have a Spring mvc application with a Controller, Model, Service, and Validation classes. Everything works fine. In developing what I currently have, I stored the validation messages in a messages.properties file -which I registered in a configuration class like this:
#Bean
public MessageSource messageSource() {
final ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("[package name]/messages/messages");
return messageSource;
}
In the Validator, I add a validation message if the user didn't answer a field such as this:
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "age", "required", new Object[] {"Age"});
In the messages.properties file I have this (above 'age' is the name of the model object, 'required' is the key used to retrieve the message in the messages.properties file):
required = {0} is required.
Ok, again -this all works fine. But what I'd like to do is externalize the messages to a database (basically, these are corporate messages that all applications use).
My question is -how do I get Spring to use the database instead of messages.properties? Or is I do use messages.properties, to only store the message's id (database key) instead of the message itself?
I have a couple ideas of what to do -but no idea how to do either one. I 'think' that the Spring-form tag library ends up performing the work of getting the actual message. Perhaps I could somehow intercept that call? Or maybe somehow change the messageSource to something else rather than defining the messages.properties file.
Anyways, does anyone have any ideas?
You can try Errors.rejectValue(String field, String errorCode, String defaultMessage) method. I too had the same problem. In my case I used ...errors.rejectValue("firstName", "error.firstName", "plz enter ur first name!!.");
This should ideally use the defaultMessage in the scenario where it can't find the error code from message source.
Instead of using a ResourceBundleMessageSource use a custom MessageSource implementation, which back in a DB. Unfortunately Spring itself doesn't have such an implementation, as described in the jira issue: https://jira.springsource.org/browse/SPR-364
But implementing the MessageSource Interface is straightforward, you can even see a sample implementation attached to the jira issue: https://jira.springsource.org/secure/attachment/10261/JdbcMessageSource.java

Resources