I am using this:
<li ${(requestScope['javax.servlet.forward.request_uri']=='/example') ? "class='active';" : ""}>
And this code activates my link to me but I've written such code to be active: ${(requestScope['javax.servlet.forward.request_uri'] +="?"+ requestScope['javax.servlet.forward.query_string']=='/example/?buy=e') ? "class='active';" : ""}
But I am getting an error.
Can I get your solution suggestions?
Try this below.
String uri = request.getScheme() + "://" +
request.getServerName() +
("http".equals(request.getScheme()) && request.getServerPort() == 80 || "https".equals(request.getScheme()) && request.getServerPort() == 443 ? "" : ":" + request.getServerPort() ) +
request.getRequestURI() +
(request.getQueryString() != null ? "?" + request.getQueryString() : "");
Note:
This snippet above will get the full URI, hiding the port if the default one was used, and not adding the "?" and the query string if the latter was not provided.
Related
URLEncodedUtils is deprecated in Android API 22+. What could I use instead in this code?
I need to change URLEncode.Utils.Format() line.
public String construct() {
return (iftrue? HOSTING_NAME : _NAME) + yDomain
+ (param.size() > 0 ? "?"
+ URLEncodedUtils.format(yParam, "utf-8") : "");
}
You can use java inbuilt method java.net.URLEncoder to construct url with param data. URLEncoder.encode(yParam, "UTF-8")
Change your method and check it.
`public String construct() {
return (iftrue? HOSTING_NAME : _NAME) + yDomain
+ (param.size() > 0 ? "?" + URLEncoder.encode(yParam, "UTF-8"): "");}`
Boolean isValid = true;
String message = "prefix" + isValid != null ? " midfix " : "suffix";
System.out.println(message);
What do you think is the result of this? I'd expect prefix midfix. But actually the result is: midfix!
Is this an error in the java library itself (1.7)? Should I report a bug for this? Or does this work as intended, and I'm misusing it?
It can be "fixed" using:
String message = "prefix" + (isValid != null ? " midfix " : "suffix");
but anyways shouldn't it work without the brakets?
It is evaluated as :
String message = (("prefix" + isValid) != null) ? " midfix " : "suffix";
which is equivalent to :
String message = ("prefixtrue" != null) ? " midfix " : "suffix";
Therefore "midfix" is returned.
If you wish "prefix midfix" to be returned use parentheses:
String message = "prefix" + (isValid != null ? " midfix " : "suffix");
In the browser (IE and Firefox) if you have a relative link and your URL is: http://domain/somepath/lastfolder/
the relative link becomes:
http://domain/somepath/lastfolder/linkdocname.html
if the URL is http://domain/somepath/lastdoc the relative link becomes:
http://domain/somepath/linkdocname.html
http://domain/somepath/lastfolder/ becomes:
http://domain/somepath/lastfolder/linkdocname.html
Is there a way to replicate this using JSP without writing a special function?
I tried to get the base URL using:
baseURL = request.getScheme() + "://" + request.getServerName() + ":"
+ request.getServerPort()+ request.getRequestURI();
but that gets me the whole path of the requst URI and doesn't drop off the last bit if it doesn't end in a "/" Then if I try:
baseURL = request.getScheme() + "://" + request.getServerName() + ":"
+ request.getServerPort() + request.getContextPath();
that gives me everything up to the web container folder but not any of the folders after that.
In your last string building attempt you are missing a port number, even though you specified ":"
Try this:
new URL(request.getScheme(), request.getServerName(),
request.getServerPort(), request.getContextPath());
You can also build the string yourself and remove default ports if needed:
public String getBaseUrl(HttpServletRequest request) {
if (( request.getServerPort() == 80 ) || ( request.getServerPort() == 443 )) {
return request.getScheme() + "://" + request.getServerName() +
request.getContextPath();
} else {
return request.getScheme() + "://" + request.getServerName() + ":" +
request.getServerPort() + request.getContextPath();
}
}
e.getCategory() != null ? e.getCategory().getName() : "";
This throws a NullPointerException and I do not understand why. Can anyone explain?
Explanation:
According to Java's precedence rules, your code was being parsed like this:
(("\"category\":" + "\"" + e.getCategory()) != null) ? e.getCategory().getName() : ""
with the entire concatenation (("..." + e.getCategory())!= null) as the condition.
Since "..." + e.getCategory() is never null, the code didn't work.
e is null.
Is e null?
Perhaps you should try this:
(e != null) ?
(e.getCategory() != null) ?
e.getCategory().getName() :
""
: ""
Or rather, a simplified form:
(e != null && e.getCategory() != null) ?
e.getCategory().getName() :
""
Solution found....
CORRECT
bufo.append("\"category\":" + "\"" + ((e.getCategory() != null) ? e.getCategory().getName() : "") + "\",");
PROBLEM
bufo.append("\"category\":" + "\"" + e.getCategory()!=null?e.getCategory().getName():"" + "\",");
I would like to be able to create a live template in Jetbrain's Idea that will log the method's arguments. Let's call it "larg". It would work like:
public void get(String one, String two) {
larg<tab>
to create
public void get(String one, String two) {
log.info("get: one = " + one + " two = " + two);
I'm fine with getting the method name in, but have not figured out how to pull in the method arguments. Any ideas?
I'm 4 years late, but the predefined template soutp pretty much does this using a groovyscript variable.
Here's the groovy script that does what you're looking for
groovyScript("'\"' + _1.collect { it + ' = [\" + ' + it + ' + \"]'}.join(', ') + '\"'", methodParameters())
Looks like it is not currently possible with a live template.
From the Jetbrain's forum:
There is no predefined live template function to do this for you automatically.
You can write a plugin that would provide such a function.
this is my groovy script
groovyScript("import com.intellij.psi.*;import com.intellij.psi.util.PsiTreeUtil; def file = PsiDocumentManager.getInstance(_editor.project).getPsiFile(_editor.document); PsiMethod method = PsiTreeUtil.findElementOfClassAtOffset(file, _editor.caretModel.offset, PsiMethod.class, false); PsiParameter[] parameters = method == null ? PsiParameter.EMPTY_ARRAY : method.parameterList.parameters; return parameters.size() == 0 ? '' : '\"' + method.name + ' : ' + parameters.collect { def prefix = it.name + ' = '; def type = it.type; return type instanceof PsiArrayType ? type.componentType instanceof PsiPrimitiveType ? prefix + '\" + java.util.Arrays.toString(' + it.name + ')' : prefix + '\" + java.util.Arrays.deepToString(' + it.name + ')' : prefix + '\" + ' + it.name }.join(' + \", ')", methodParameters())
In Android Studio in Kotlin I use this to log classname, methodname and parameters.
I use my name as tag to easily filter in logcat. I use these mainly for debugging, I don't commit these messages with my name.
Log.d("MYNAME", "$CLASS_NAME$:$METHOD_NAME$: $PARAMETERS$")
And then the parameters are defined as follows.
CLASS_NAME : kotlinClassName()
METHOD_NAME : kotlinFunctionName()
PARAMETERS : groovyScript("'' + _1.collect { it + ' = $' + it}.join(', ') ", functionParameters())
This would be the result if used in the main activity
fun aFunctionWithParameters( first: Int, second: String, third: ArrayList<String>){
Log.d("MYNAME", "MainActivity:aFunctionWithParameters: first = $first, second = $second, third = $third")
}
I based my solution on Rob's answer and edited it to my needs.