how to join a string and variable to th:src - java

I want spring to search an image under path: /upload-dir/ID.jpg
ID is a variable that depends on the object's id.
Ive made something like this:
th:src="|#{/upload-dir/} ${dog.id} .jpg|"
but this is not working at all. I beg for any advice how to make it work :)

You can use the following structure to do so.
th:src="#{'/upload-dir/' + ${dog.id} + '.jpg'}"

Related

Fast variable insertion into string

Is there any Intellij Idea hotkey or a plugin to quickly insert variable into string?
e.g. i have string
"My code is working, yay! Result is = ",
and i have to add construction "+variable +", resulting in
"My code is working, yay! Result is = "+ variable +"."
to properly insert a variable.
When i have to insert variables into 20+ string, its driving me nuts :)
tried to find solution on google or plugin repository, no result
PS: i know about soutv hotkey, but it cant help me since i have to change already existing strings in code
Added Live Template
Abbreviation: ++
Description: Insert variable into string
Expand with: Enter
Template text: "+ $EXPR$ +" (do not forget double quotes)
Edit Template Variable:
expression:variableOfType("") default Value: "expr"
settings screen
You type ++ in a string, press Enter, and template text is added, and you only need to choose a variable. Works like a charm.
Thanks for the idea, #Meo

JDBC SqlParameterValue - determine SQL Type of parameter

I'm working in Java and trying to determine something like so:
I have a SqlParameterSource with an array named "ids" in it. I need to determine what type these ids are in, for example numeric or varchar. I specify that earlier in the code with for example:
return con.createArrayOf("varchar")
or
return con.createArrayOf("numeric")
I have tried this:
if (parametersource.getSqlType("ids") == something) {
// do something
}
else {
//do something else
}
I can't figure out how to do this. getSqlType seems to return an int but I don't know what to compare it to to get the correct comparison.
There is another method named getTypeName but I don't get how this works.
I solved it with:
parameterSource.getValue("objtype").toString();
Fetches the value from "objtype" which I set in my ParameterSource alongside my array to make my end goal easier.
Thanks friends, I love you all. :)
If you're talking about Spring SqlParameterSource,try parametersource.getTypeName("ids")which is inherited from AbstractSqlParameterSource

Retrofit query params

I'm trying to configure a request to http: //where.yahooapis.com/v1/places.q(name_here);count=50?....
Not the best solution, I guess, but I tried
#GET("/v1/{location}")
Places getLocations(#Path("location") String locationName);
and pass there
getLocations("places.q(" + locationName + ");count=50");
But it still doesn't work as the string (); is translated into %28%29%3B.
Can you suggest any solutions? It would be better to dinamycally modify only the name_here part, something like
#GET("/v1/places.q({location});count=50)
If it is not possible how do I have to pass symbols (); so that they are converted correctly?
I just tried
#GET("/v1/places.q({location});count=50")
Places getLocations(#Path("location") String name)
a bit later and it works fine. I thought it will insert something like "/" or modify it, but it does exectly what I need.

Minecraft 1.7.2 setting textures efficiently

I set textures in each individual file, this is the non efficient way to set it
this.setUnlocalizedName("ItemName");
this.setTextureName("MyModName:ItemName");
This way made sense to me, but didn't work:
this.setUnlocalizedName("ItemName");
this.setTextureName(OtherClassName.MODID + ":" + this.getUnlocalizedName());
the 'OtherClassName.MODID' is referring to a variable in another class that contains 'MyModName'
The this.getUnlocalizedName() gets the UnlocalizedName that has been declared, 'ItemName'
Any help? I am not sure why it doesn't work.
getUnlocalizedName is slightly weird - it returns the string you passed into setUnlocalizedName, but with "item." at the start. The joys of working with deobfuscated code...
This would work:
String name = "ItemName";
this.setUnlocalizedName(name);
this.setTextureName(OtherClassName.MODID + ":" + name);
Note that it's not more efficient as in faster to run, but it might be faster to write if you change the item name a lot.

Mysql session variable in JDBC string

am using this connection string to connect to mysql from java:
jdbc:mysql://localhost:3306/db?noDatetimeStringSync=true&useUnicode=yes&characterEncoding=UTF-8
is it possible to set the session variable in the string so that SET UNIQUE_CHECKS=0; would be executed upon connecting to server? the obvious
jdbc:mysql://localhost:3306/db?noDatetimeStringSync=true&useUnicode=yes&characterEncoding=UTF-8&unique_checks=0
doesn't seem to work, based on the fact that
'jdbc:mysql://localhost:3306/db?noDatetimeStringSync=true&useUnicode=yes&characterEncoding=UTF-8&unique_checks=blahblah`
doesn't generate any error.
Cheers!
How about using sessionVariables:
jdbc:mysql://localhost:3306/db?noDatetimeStringSync=true&useUnicode=yes&characterEncoding=UTF-8&sessionVariables=unique_checks=0
Your question is thus more "How do I concat Strings in Java?" ?
If so, then just use the + operator:
int uniqueChecks = 0; // Assign session variable here.
String url = "jdbc:mysql://localhost:3306/db?unique_checks=" + uniqueChecks;
Alternatively you can also use String#format() wherein you can use the %d pattern to represent a decimal:
int uniqueChecks = 0; // Assign session variable here.
String url = String.format("jdbc:mysql://localhost:3306/db?unique_checks=%d", uniqueChecks);
You can use Statement.execute() to run pretty much every statement the DB understands, including such a SET-statement.
The advantage of using an URL parameter or a dedicated method is that the JDBC-driver is actually aware that the option was set and can react accordingly. This may or may not be useful or necessary for this particular option, but it's vital for other options (for example toggling autocommit with such a statement is a very bad idea).
BalusC, thanks for a reply! actually I need to do that in Talend etl tool(which itself is a java code generator) and the only line i can edit is the "jdbc:mysql://localhost:3306/db?noDatetimeStringSync=true&useUnicode=yes&characterEncoding=UTF-8" string, which gets translated to this java code:
String url_tMysqlBulkExec_1 = "jdbc:mysql://"
+ "localhost" +
":"
+ "3306"
+ "/"
+ "db"
+ "?"
+ "noDatetimeStringSync=true&useUnicode=yes&characterEncoding=UTF-8&unique_checks=0";
that's the limitation, sorry for not pointing that out earlier.
According to mysql docs, there is no possibility to set the unique_checks setting, i guess i need to look for other solution than URL parameters (Joachim, thanks for reminding me that these things are called "URL parameters" - help a lot while googling :)

Categories