Hi i am getting a attribute value using check box dynamically in java server pages.If check box is checked then it give value no-follow else null.
but i want if my value is null then rel also not show in anchor tag. how can i achieve this
my code is
<% String relAttribute = properties.get("./relAttribute","false");
String relAttributeValue=relAttribute.equals("true")?"nofollow":"";%>
<a class="play" title="Play video" target="_top" href="<%=appURL%>" rel="<%=relAttributeValue%>" >
you can check your attribute with JSTL and place the content in the body of the c:if tag
<c:if test="${relAttribute is not empty)">
Related
I would like to select the text inside the strong-tag but without the div under it...
Is there a possibility to do this with jsoup directly?
My try for the selection (doesn't work, selects the full content inside the strong-tag):
Elements selection = htmlDocument.select("strong").select("*:not(.dontwantthatclass)");
HTML:
<strong>
I want that text
<div class="dontwantthatclass">
</div>
</strong>
You are looking for the ownText() method.
String txt = htmlDocument.select("strong").first().ownText();
Have a look at various methods jsoup have to deal with it https://jsoup.org/apidocs/org/jsoup/nodes/Element.html. You can use remove(), removeChild() etc.
One thing you can do is use regex.
Here is a sample regex that matches start and end tag also appended by </br> tag
https://www.debuggex.com/r/1gmcSdz9s3MSimVQ
So you can do it like
selection.replace(/<([^ >]+)[^>]*>.*?<\/\1>|<[^\/]+\/>/ig, "");
You can further modify this regex to match most of your cases.
Another thing you can do is, further process your variable using javascript or vbscript:-
Elements selection = htmlDocument.select("strong")
jquery code here:-
var removeHTML = function(text, selector) {
var wrapped = $("<div>" + text + "</div>");
wrapped.find(selector).remove();
return wrapped.html();
}
With regular expression you can use ownText() methods of jsoup to get and remove unwanted string.
I guess you're using jQuery, so you could use "innerText" property on your "strong" element:
var selection = htmlDocument.select("strong")[0].innerText;
https://jsfiddle.net/scratch_cf/8ds4uwLL/
PS: If you want to wrap the retrieved text into a "strong" tag, I think you'll have to build a new element like $('<strong>retrievedText</strong>');
I have to display the action message as html format.but the action message takes the html code as string.how to print that html in spring mvc.
Below is my code:
#RequestMapping("")
public ModelAndView openIndex(HttpSession session){
String msg="<html><span style="color:red">Invalid User</span></html>";
return new ModelAndView("index","msg",msg);
}
when i print the msg using jstl it displays like below
<html><span style="color:red">Invalid User</span></html>
But it should be print like below
Invalid User
How to achieve the above result?
Any help will be greatly appreciated!!!
You would be using tag in the jsp.Set the escapeXml attribute of to false.The escapeXml attribute determines whether characters <,>,&,'," in the resulting string should be converted to their corresponding character entity codes. Default value is true.
<c:out value="${msg}" escapeXml="false"></c:out>
Or directly use:
${msg}
on your jsp.
You can create one div on JSP page with.
Then set div background color as red
String msg="Invalid User";
return new ModelAndView("index","msg",msg);
On JSP page, you can simply get the value using ${msg}
OR
1.
String msg="Invalid User";
return new ModelAndView("index","msg",msg);
use jquery and in document.ready() function you can set msg as a div html
Something like $('#divID).html(${msg});
I want to carry out some validation in javascript before posting, so basically I want to:
Alter textbox value
use Onchange attached to the textbox to fire off a Javascript script.
the script will check that a colon is at the right place in the value.
if that isnt the case, change the textbox back colour to red.
all this is done before secondary validation in the struts form and action.
first problem is I cannot seem to pass the textbox value to the script, is this prevented under struts?
second problem is the changing of the back colour of the textbox on error.
<html:text property="date1_1" maxlength="8" value="<%=WeekOne.get(1)%>" size="15" onchange="validateBox(this.value)"/>
<script type="text/javascript">
function validateBox(textBox)
{
alert("here");
var p = textBox.value();
alert(p);
}
</script>
To solve your problems you need to fix some errors in the code. I will show you what to fix trying to achieve what you've written by numbers.
To alter textbox value you need to pass the textbox element itself not the value.
You already use it
doing some checks in the script against textbox value (value is not a function)
for this you should use background color CSS property of the textbox
assume second validation is on the server, checks are done on the client via javascript
For example
<html:text property="floor" styleClass="input" size="30" tabindex="12" onchange="validateBox(this)"/>
<script type="text/javascript">
var textBoxBackgroundColor;
function validateBox(textBox) {
var p = textBox.value;
if (textBoxBackgroundColor == null)
textBoxBackgroundColor = textBox.style.backgroundColor;
//validate
if (p < 0)
textBox.style.backgroundColor = '#000000';
else
textBox.style.backgroundColor = textBoxBackgroundColor;
}
</script>
will check that textbox floor is positive.
hi I am new to java and I am creating a jsp and I am using scriplet code. I would like to be able to have my object to not display on the screen when it is empty.
<% if (webApp.getInfo != null) { %> <h6><%=webApp.getInfo()%> <% } %>
Currently it is checking if the object is null but it is still displaying a line on the page when I run the JSP. How can I check to see if webApp.getInfo is empty and not have a line display or test display for those headers?
<c:if test="${!empty str}">
<h6>...</h6>
</c:if>
Note that:
this is meaningful only for strings, not for any object
this is JSTL, which is preferred to scriptlets (from your example)
your objects needs to be set as request attribute.
you only need this if you have additional tags inside the if-clause. If it is only the string that you want to output, there is no need to check - as BalusC noted this is not displayed.
you can trim whitepsaces from the jsp output using configurations provided by your servlet container, or if using servlet 3.0 - via <trim-directive-whitespaces> in web.xml
I want to dispaly a result parameter which return from servlet to jsp after an event happen.
so when i add new user for example a result parameter returns with value that the user has been added successfully , and i want to display the result in jsp page.
i make like this in jsp page
<%String result = String.valueOf(request.getAttribute("result"));%>
<%if (result != null){%>
<%= result%>
<%}%>
THE PROBLEM is that every time i open the page for the first time result prints as null to the browser even i write if not null print result value, where is the problem ? can someone help/
String.valueOf(..) returns "null" (a String with length 4) if the argument is null. So your quick solution would be not to use String.valueOf(..) at all.
However, it is not advisable to use scriptlets and java code like that in your JSPs. Use JSTL and EL instead. Your code would look like this:
<c:if test="${result != null}">
${result}
</c:if>