Using a value as an argument into another value in Thymeleaf - java

So lets say I have a #ModelAttribute of userCredentials which is a List<String> object.
and I have another ModelAttribute of type Map<String,String> of roles.
I can access them separately in HTML using Thymeleaf with:
${userCredentials.contains('<Hardcoded-value>')}
The problem i want the hardcoded value replaced and to use for example:
${userCredentials.contains('roles.client')}
Do you know how can i successfully use a model attribute as a parameter to the other model attribute. It works with the hardcoded values

You can use Thymeleaf pre-processing:
${userCredentials.contains(__${roles.get('client')}__)}
Thymeleaf executes the preprocessing part in a first pass. So it will replace what is there with:
${userCredentials.contains(<result of roles.get() call here>)}
And then execute the template rendering.

Related

How to get the value from a Form without validating the fields?

I have a scala template and I've passed a form instance with a Ticket instance populated with my data in order to render my page ticketForm : Form[Ticket]. However, part of my template renders a List<Object> items into a <ul> and I don't want to pass this list as a parameter on the template as I already have it as a property on the ticket object itself. I was using ticketForm.get().getItems but it runs the validation and throws an exception.
Is there any another way to do that?
Do not use get, just ticketForm('myproperty')
An example of generation radio group:
https://github.com/playframework/playframework/blob/master/framework/src/play/src/main/scala/views/helper/inputRadioGroup.scala.html
In the case, if you want to process a list, like the #repeat helper
#helper.repeat(userForm("emails"), min = 1) { emailField =>
#helper.inputText(emailField)
}
Here is the realization of the repeat helper:
https://github.com/playframework/playframework/blob/master/framework/src/play/src/main/scala/views/helper/Helpers.scala#L80

NiFi custom processor expression language

I'm trying to make a custom processor in Apache NiFi that can add an attribute/string to the JSON object in the flowfile content. At the moment it works when I just use a string but it's not working when I use NiFi's expression language although I have it supported in my code.
The expression language is 100% correct as it works in another processor and I've also tried different attributes to make sure it's not the attribute.
The property:
public static final PropertyDescriptor ADD_ATTRIBUTE = new PropertyDescriptor
.Builder().name("Add Attribute")
.description("Example Property")
.required(true)
.addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
.expressionLanguageSupported(true)
.build();
Later in my code when I want to get the value and put in the JSON object I use:
jsonObject.put("hostname", context.getProperty(ADD_ATTRIBUTE).evaluateAttributeExpressions().getValue());
I also made a Unit Test and it works when I assign a text value to the testrunner.setProperty. However I don't know how I can assign an attribute to the testrunner or how I can use expression language in my test.
Thanks in advance for any suggestions or a solution!
I'll put my answer from Hortonworks Community Connection here too FWIW:
If the expression refers to attributes on a flow file, you will need to pass a reference to the flow file into evaluateAttributeExpressions:
FlowFile flowFile = session.get();
jsonObject.put("hostname", context.getProperty(ADD_ATTRIBUTE).evaluateAttributeExpressions(flowFile).getValue());
If the property contains an attribute name (rather than an Expression containing an attribute name) and you want the value from the flow file:
jsonObject.put("hostname", flowFile.getAttribute(context.getProperty(ADD_ATTRIBUTE).getValue()));
If the attribute value itself contains Expression Language and you want to evaluate it, take a look at the following class:
org.apache.nifi.attribute.expression.language.Query
Regarding testing...
Assuming you are evaluating the expression language against an incoming FlowFile (evaluateAttributeExpressions(flowFile)) then you can do the following:
runner.setProperty(ADD_ATTRIBUTE, "${my.attribute}");
Then create an attribute Map that has my.attribute in it:
final Map<String,String> attributes = new HashMap<>();
attributes.put("my.attribute", myAttribute);
Then enqueue some content with the attributes:
runner.enqueue(fileIn, attributes);
runner.run();
An example from the code base:
https://github.com/apache/nifi/blob/1e56de9521e4bc0752b419ffc7d62e096db1c389/nifi-nar-bundles/nifi-solr-bundle/nifi-solr-processors/src/test/java/org/apache/nifi/processors/solr/TestPutSolrContentStream.java#L243

Jersey : Is it possible to specify multiple values in #DefaultValue() annotation

I am playing around with Java API for RESTful Web Services (JAX-RS) ,
and encountered #DefaultValue annotation in Jersey implementation of JAX-RS.
Here is the code snippet
#GET
#Path("/query")
public Response getUserWithQueryParams(
#DefaultValue("defaultId") #QueryParam("from")String from,
#DefaultValue("defaultName") #QueryParam("to") String to,
#DefaultValue("mobileNo")#QueryParam("orderBy") List<String> orderBy
){
My third argument is of List<String> which can have multiple values
for example I explicitly pass the parameters
users/query?from=100&to=200&orderBy=age&orderBy=name
Now my third argument have values [age,name] ,
but if i don't pass any explicitly parameter then Is there any way to set multiple default values . ?
This won't work how you want it to. If the object is type List it will have a single value inserted in the list. The object in the list will be the value of your default value for the list. Check this out Why not try checking if orderBy == null if it does then add your default values to orderBy?

What is the best way to get one param from form (Play2 Framework)?

I am want to create simple form for searching records via one parameter (for example, name).
Seems like creating a class with one property (name) and than use helpers for forms - is not a best way.
Is there any examles how can I get POST data from request and fetch property value from that data?
Thanks a lot for wasting your time.
You already answered your own question, I just want to provide some more information:
You are right about creating a class with one single property, however keep in mind that you can use validation annotations (like #Required, #Email, etc.) in this class - so if there is some (super) complex logic behind this property this might also be a valuable option.
The second solution would be to use DynamicForms - you use them when you don't really have a model that is backing up the submission form. It goes like this:
public static Result index() {
DynamicForm requestData = Form.form().bindFromRequest();
String name = requestData.get("name");
return ok(name);
}
And of course the third option to get the values is like you mentioned:
String name = request().body().asFormUrlEncoded().get("name")[0];
If you do not what to use form validation, I don't think you need to create a class. Instead, you can use AJAX function like $.ajax(), that will be route to your specific controller function. Moreover, you can call your model function from your controller then at last return the result. The result will be caught by the $.ajax() function.
$.ajax
type: "POST"
url: url
data: data
success: success
dataType: dataType

Get command object

I am writing a spring 2.5 application and in my jsp I'm writing my own tags.
It's about a list of objects...when I change the number of rows that list shows(a combobox), I am doing a submit on my form returning back to the view(obviosly with the new number of rows returned).
When listing with my own tags I need to get the properties from my command object.
I have access to the pageContext object but I can't figure where the command object is stored.
By default, the command object is stored under a "command" attribute (request or session scope depending on your configuration of the sessionForm property). You can change that by setting the commandName property on your controller and your command object will be included in the model under this name (and not the default "command").
Once in your tag code, you can use request.getAttribute("command") or, if sessionForm=true, session.getAttribute("command") to get access to your command object (assuming the default name "command"). If you changed the name of the command using the commandName property then use that instead of "command".
Usually you wouldn't care in what scope the command is, so having access to the pageContext object, you can do a pageContext.findAttribute("command") and that will look it up in all scopes.

Categories