I am working with thymleaf and I am trying to apply style directly from a spring object. I am trying to use the th:style attribute and pass in the value directly from the pojo.
I found this code online that works
th:style="${color == 'yellow' ? 'background:yellow' : 'background:blue'}"
But I want to pass in the value direct from the object.
Like this
th:style="${color == 'yellow' ? 'background:${color}' : 'background:blue'}"
or like this directly
th:style="background:${color}"
both ways throw up errors.
Any help appreciated.
The Thymeleaf docs have many examples of String concatenation... the easiest in this case would be literal substitution.
th:style="|background: ${color}|"
but you can just use a regular string concatenation as well.
th:style="${'background: ' + color}"
Why you don't use th:class="${color == 'yellow' ? 'yellowClass' : 'blueClass'}"
And style to both class.
If you already have class attr, you can using th:classappend
Related
I'm looking for a way to get the value of the class (for my example "AAABC") stored in a variable. I tried different key words with the getAttribute method, but none were successful. Key word "class" obviously gave me "gwt-Label", all the other key words gave me "null".
Using getAttribute is not necessary, if you can think of an other elegant way.
Example:
<div class="gwt-Label">AAABC</div>
driver.findElement(By.xpath("//div[#class='gwt-Label']")).getText();
This is solve your issue.
First you need to do the following to get the string from your class object:-
String example = object.toString();
// here in msg you will get the whole string < div class="gwt-Label"> AAABC< /div>
Now you can use approach like below to get your string:-
example = example.substring(example.indexOf(">") + 1);
As per the HTML to retrieve the class attribute you can use the following line of code :
String myClass = driver.findElement(By.xpath("//div[text()='AAABC']")).getAttribute("class");
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
I have a String getter and setter that i am setting in my bean.
I am trying to get the value in my jsp using jstl like this :
<jsp:useBean class="com.test.MyBean" id="results" scope="request"/>
<script type="text/javascript">
function setMyFields(){
var flag="<c:out value='${results.sdateFlag}'/>";
alert(flag);
var text_box = document.getElementById('mySelectedDate');
if(flag=="true"){
text_box.setAttribute('disabled', 'disabled');
}
}
window.onload = setMyFields;
</script>
I have imported jstl core as well in my jsp.
But when i do this i get an error like this :
javax.servlet.ServletException: Unable to find a value for "sdateFlag" in object of class "com.test.MyBean" using operator "."
at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:660)
at com.ibm._jsp._pageMyAmount._jspService(_pageMyAmount.java:306)
at com.ibm.ws.jsp.runtime.HttpJspBase.service(HttpJspBase.java:87)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1101)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:569)
at com.ibm.ws.wswebcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:478)
at com.ibm.wsspi.webcontainer.servlet.GenericServletWrapper.handleRequest(GenericServletWrapper.java:122)
at com.ibm.ws.jsp.webcontainerext.AbstractJSPExtensionServletWrapper.handleRequest(AbstractJSPExtensionServletWrapper.java:226)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java:321)
But ihave been debugging the java class using the java debugger and it seems the value is getting set. Then why is it not getting the value ?
EDIT : These are the setter and getter of my bean :
public void setDateFlag(String b) { sDateFlag = b; }
public String isDateFlag() { return sDateFlag; }
What's wrong here ? Am i missing something ?
You're treating the dateFlag as a boolean property, but it is in fact a String. So you "getter" should be called getDateFlag, not isDateFlag. As mentioned, the isPropertyName syntax is only applicable to properties of type boolean.
Also, <c:out value='${results.dateFlag}'/> isn't really needed. You should be able to simply do ${results.dateFlag}.
So , i got the problem fixed and it was a bizarre solution. The problem it seems is, any variable declared must follow java standards. And my problem was , i had given the variable name as sDateFlag in my original proprietary code, which does not follow the java naming conventions. When i removed it and gave it as flag , it started working. I R&D ed the answer using this link here : JSTL . Though it's not fully correct, it led me to the root cause of my problem. And i will like to convey my thanks to all of you guys , who have replied to me, to make the process easy .
We're using fortify to scan java source code & it is complaining below error:
Method abc() sends unvalidated data to a web browser on line 200, which can result in the browser executing malicious code.
We've below code on line 200:
Product
And Util.java hsa below code in getProduct method:
String prod = request.getParameter("prod");
Can any one tell me how to fix this XSS vulnerability?
Thanks!
You need to escape the output of Util.getProduct(request). Typically this is done using JSTL and a <c:out> tag and EL:
Product
N.B. you'll have to use a fairly up-to-date implementation of EL (as per JSTL or JSP 2.0 EL for getter with argument and Parameters in EL methods) in order to pass an argument to the getter method.
Since the code in your question contains scriptlets, I will strongly suggest that you read How to avoid Java code in JSP files? This question covers reasons to use JSTL+EL instead of scriptlets, as well as a bit of information about what those two acronyms actually refer to.
In case you don't have JSTL for this website, you can fix the problem by making sure you only print valid products:
public String getProduct( String prod ) throws InputValidationException {
if ( prod.equals( "myProduct1" )
|| prod.equals( "myProduct2" )
|| prod.equals( "myProduct3" )
// etc.
) {
return "/foo/page.jsp?product=" + safeProduct;
}
else {
throw new InputValidationException( "Invalid product key provided." );
}
}
i want to use and/or in JasperReport expression
i tried the following but it doesn't work:
($P{pId} == $F{id1}) or
($P{pId} == $F{id2} and F{return}=true)
? "good" : "bad"
but i am getting following exception:
Compilation exceptions: com.jaspersoft.ireport.designer.compiler.ErrorsCollector#1c9f37d net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed, calculator_report1_1318247835062_860381: 191: unexpected token: or # line 191, column 144. 1 error at net.sf.jasperreports.compilers.JRGroovyCompiler.compileUnits(JRGroovyCompiler.java:88) at net.sf.jasperreports.engine.design.JRAbstractCompiler.compileReport(JRAbstractCompiler.java:188) at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:215) at net.sf.jasperreports.engine.JasperCompileManager.compileReportToFile(JasperCompileManager.java:131) at com.jaspersoft.ireport.designer.compiler.IReportCompiler.run(IReportCompiler.java:509) at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:572) at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:997)
any ideas why i am getting this exception and how to solve it.
and and or are not valid Java operators. JasperReports uses Java expressions. And since all Jasper parameters and fields are objects, I doubt you want to compare them with ==. Use equals instead.
($P{pId}.equals($F{id1}) ||
($P{pId}.equals($F{id2}) && F{return}.booleanValue()))
? "good" : "bad"