<%for (String st : geocodePhoto.keySet()) {%>
alert(<%=st%>); // not work
alert(<%=geocodePhoto.get(st).getX()%>); // work fine
alert(<%=geocodePhoto.get(st).getY()%>); // work fine
alert(<%=geocodePhoto.get(st).getDate()%>); // not work
<%}%>
getX is return double value and getDate return String value like 'yy:mm:dd hh:mm:ss'
st has same form 'yy:mm:dd hh:mm:ss'
2,3 line alert is work fine but 1,4 line alert is doesn't work
what is wrong?
The <%= %> tag in JSP acts as if it calls String.valueOf() with the expression in the tag as the parameter, and writes the returned value to the output. So, your generated JavaScript source probably looks something like this:
alert(13:11:23 10:30:17);
alert(-0.06);
alert(51.5);
alert(13:11:23 10:30:17);
You're trying to pass text to the first and last calls to alert, but you aren't putting the text in quotes - so, you're getting a syntax error. The middle two calls are writing numbers into your JavaScript source - as a numeric constant is valid JavaScript, they work without being quoted.
So, your JSP code should look like this:
alert("<%=st%>");
alert(<%=geocodePhoto.get(st).getX()%>);
alert(<%=geocodePhoto.get(st).getY()%>);
alert("<%=geocodePhoto.get(st).getDate()%>");
Pass string through ""
alert("<%=st%>");
alert("<%=geocodePhoto.get(st).getDate()%>");
Related
I’m trying to use a JSTL conditional statement on a .jsp page in a Spring MVC app.
I have a model called user and an attribute called gender. The value of gender is displayed when I use this line in the .jsp:
${user.gender}
Specifically, in this case the value is ‘M’. But I want to display a literal ‘Male’ when the value is ‘M’. Based on the documentation I’ve found, my code should be:
<c:if test="${user.gender=='M'}">Male</c:if>
However, that doesn’t display anything. Any ideas what I’m doing wrong here?
The gender variable is defined as a String in my User model class.
I found a fix. As it turns out, my ${user.gender} value was for some reason getting padded with spaces. Thus, I had to trim off those spaces for the conditional to work. For that I used the JSTL trim function:
<c:set var="genderstring" value="${user.gender}"/>
<c:set var="genderstringtrimmed" value="${fn:trim(genderstring)}" />
Then I changed the conditional, which now works:
<c:if test="${genderstringtrimmed=='M'}">Male</c:if>
try
<c:if test="${user.gender.equals('M')}">Male</c:if>
Following is a form in a resultSet loop. So, there might be multiple forms according to range of results.
/* RESULT SET LOOP STARTED with `i` as iterator running from 1 to 5 */
<form action='Jaga' method='post' >
<input name='input-<%=i%>' />
</form>
/* RESULT SET LOOP ENDED */
So, on form-submission, Jaga Servlet receives information. How do i get to know which 'input-iterator' combination was used from which form.
request.getParameter('here');
What do i fill in-place of 'here' in Jaga Servlet to get correct input box value from correct form?
If you take a look at
https://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameter(java.lang.String)
You can see that it returns a String or null if parameter doesn't exist, so you can simply make a for loop and check for the first not null return . Something like:
for(int i=1;i<=5;i++)
if(request.getParameter("input-"+i)!=null)
// handle stuff
EDIT: For getting the parameter names, try:
PrintWriter out = response.getWriter();
Enumeration<String> parameter = request.getParameterNames();
while(parameter.hasMoreElements())
out.println(parameter.nextElement());
I have a web application running Java Tapestry, with a lot of user-inputted content. The only formatting that users may input is linebreaks.
I call a text string from a database, and output it into a template. The string contains line breaks as /r, which I replace with < br >. However, these are filtered on output, so the text looks like b<br>text text b<br> text. I think I can use outputRaw or writeRaw to fix this, but I can't find any info for how to add outputRaw or writeRaw to a Tapestry class or template.
The class is:
public String getText() {
KMedium textmedium = getTextmedium();
return (textmedium == null || textmedium.getTextcontent() == null) ? "" : textmedium.getTextcontent().replaceAll("\r", "<br>");
}
The tml is:
<p class="categorytext" id="${currentCategory.id}">
${getText()}
</p>
Where would I add the raw output handling to have my line breaks display properly?
To answer my own question, this is how to output the results of $getText() as raw html:
Change the tml from this:
<p class="categorytext" id="${currentCategory.id}">
${getText()}
</p>
To this:
<p class="categorytext" id="${currentCategory.id}">
<t:outputraw value="${getText()}"/>
</p>
Note that this is quite dangerous as you are likely opening your site to an XSS attack. You may need to use jsoup or similar to sanitize the input.
An alternative might be:
<p class="categorytext" id="${currentCategory.id}">
<t:loop source="textLines" value="singleLine">
${singleLine} <br/>
</t:loop>
</p>
This assumes a a getTextLines() method that returns a List or array of Strings; it could use the same logic as your getText() but split the result on CRs. This would do a better job when the text lines contain unsafe characters such as & or <. With a little more work, you could add the <br> only between lines (not after each line) ... and this feels like it might be a nice component as well.
Should be direct HTML printing in a JSP declaration tag function legal?
<%! void recursivePaintLevels(List<String> things, int deepLevel){ %>
<ul class="level-<%=deepLevel%>">
<% for (int i=0; i<things.size(); i++){ %>
<li class="whatever">
//(...)
</li>
<% } %>
</ul>
<% } %>
And then call it like this in normal JSP body flow:
//(...)
<% recursivePaintLevels(things, 1); %>
I mean would be like using same normal JSP logic of implicit out.println() but in a method.
For me it is not working (Eclipse says 'Syntax error, insert "Finally" to complete TryStatement') but I am not sure if my error has something to do with it.
I also know I should use JSLT and EL, but this is my choice.
No, it's not legal. The JSP page is effectively implemented as one big method that executes all of the code within the page. In java, you can't simply insert other methods nested within a method.
You code would generate something like this:
public void _jspService(...) {
...
void recursivePaintLevels(...) {
...
}
}
And that's simply not legal java.
Instead you should defer the code to a utility library class bundled with your web app.
You MIGHT be able to create a recursive tag file, I have not tried that.
I believe this is pretty much legal and valid though a bad practice.I think the problem here is because of the double quotes "level-<%=deepLevel%>"
Try separating that using
<% String str= "level-"+deepLevel; %>
and then use
<ul class="<%=str%>"> .
or simply replace the whole line with out.println
EDIT:
It appears that the body of the methods in jsp should not have any scriptlets. I have tried to embed one scriptlet with no content and observed that the generated java file adds the first part (from delcaration to the content before scriptlet )at the begging and the remaining part of the method at the end (and all member variables and other method declarations become part of this method). Apologies for providing wrong answer (as i have noticed that behavior with the cached jsp).
Looks like out.prinltn is the only solution for this problem
I'm not sure if I can do this, but I'm trying to print a string with Expression Language only (no JSTL). The string is going to often-times be null, and I want an empty string printed to HTML instead of the word "null". This is what I'm trying (without success):
<%
myString = "";
myString = someMethodThatMayReturnNull(); // returns string or null
%>
<b>${myString}</b><br />
<b>${ (empty myString) ? "myString-is-empty" : "myString-is-NOT-empty" }</b><br />
The reason I don't want to use JSTL is because I'm trying to make the minimum changes to a fairly good sized system.
Scriptlets and EL doesn't share the same variable scope. You need to prepare EL variables as attributes of page, request, session or application scope. As you have right now, it is always empty.
<%
myString = "";
myString = someMethodThatMayReturnNull(); // returns string or null
request.setAttribute("myString", myString); // It's now available by ${myString}
%>
Note that you normally do that Scriptlet part inside a Servlet. Also note that EL is null-safe, so anything which is truly null, i.e. if (object == null) passes, then it simply won't be printed at all.
Or, if your concrete problem is that it prints literally ${myString} and so on in the browser, while it worked with JSTL <c:out>, then it means that you aren't using a JSP 2.0 compatible servletcontainer or that your webapp's web.xml is not declared conform at least Servlet 2.4, or that you have isElIgnored set to true. Verify and fix it all.
EL print the variables only when its available in pageContext scope. Here the variables are in page scope. If the variable is in some other scope, you have to specify the scope while using it. Do something like this to print the value of 'mystring' variable.
<%
myString = "stackoverflow";
pageContext.setAttribute("myString",myString);
%>
${myString}
It will print the value of myString, if myString is 'null' it will print empty string(nothing will be printed).
suppose if put the variable in request scope, you have to use "requestScope"
<%
myString = "stackoverflow";
request.setAttribute("myString",myString);
%>
${requestScope.myString}
why do u want to check String null in jsp code?
If you do not want to use scriptlet then you can check string null in mentod itself and return empty string in case of null.
myString = someMethodThatMayReturnNull(); // it should never return null.
And method is from other code then you can wrap this with your java code.