$("#medicine").append('<tr class="hide1 newRow" id="row'+medCurrentIndex+'">'
+'<td>'+medtype[medCurrentIndex]+'</td>'
+'<td>'+medicineName[medCurrentIndex]+'</td>'
+'<td>'+frequency[medCurrentIndex]+'</td>'
+'<td>'+dose[medCurrentIndex]+'</td>'
+'<td>'+quantity[medCurrentIndex]+'</td>'
+'<td>'+numberofDays[medCurrentIndex]+'</td>'
+'<c:choose>'
+'<c:when test="${role eq 'doctor' }">'
+'<td><button class="btn" type="button" name="edit" value="Edit" onclick="editMedRow('+medCurrentIndex+');">Edit</button></td>'
+'<td><button class="btn" type="button" name="delete" value="Delete" onclick="deleteMedRow('+medCurrentIndex+');">Delete</button></td>'
+'</c:when>'
+'<c:otherwise>'
+'<td><input type="text" id="cost" /></td>'
+'</c:otherwise>'
+'</c:choose>'
+'</tr>');
is this code legal to write in javaScript of JSP?
because its giving me following error:
org.apache.jasper.JasperException: Validation error messages from TagLibraryValidator for c in /WEB-INF/views/prescriptionTemporary.jsp79: Illegal text inside "c:choose" tag: "'
Any Java code inside JSP will be evaluated at render time. This means, the server will read the JSP code and will start replacing any scriptlet or custom tag (like JSTL) for the necessary Java code. This can be easily explained by this example:
<script type="text/javascript">
var x = '${x}';
</script>
Or in JSTL form:
<script type="text/javascript">
var x = '<c:out value="${x}" />';
</script>
Assuming x is a request attribute with a value of "Hello World", this will generate this output in HTML:
<script type="text/javascript">
var x = 'Hello World';
</script>
In your case, it will try to convert this part of the code to its JSTL form:
<c:choose>'
<!-- ^ this character is invalid. There must be a <c:when> right after <c:choose>
v this character is also invalid. There must be a <c:when> right after <c:choose> -->
+'<c:when test="${role eq 'doctor' }">'
In short, you should not try to append any kind of Java code (scriptlet, expression language, custom tags like JSTL, etc) from JavaScript.
Writing jstl tags in javascript is not going to work. JQuery's .append(...) method is going to fire on the client side long after jstl tags are parsed.
If your goal is to choose, via jstl, what to append, a significant rewrite is needed.
edit:
Try this:
$("#medicine").append('<tr class="hide1 newRow" id="row'+medCurrentIndex+'">'
+'<td>'+medtype[medCurrentIndex]+'</td>'
+'<td>'+medicineName[medCurrentIndex]+'</td>'
+'<td>'+frequency[medCurrentIndex]+'</td>'
+'<td>'+dose[medCurrentIndex]+'</td>'
+'<td>'+quantity[medCurrentIndex]+'</td>'
+'<td>'+numberofDays[medCurrentIndex]+'</td>'
<c:choose>
<c:when test="${role eq 'doctor' }">
+'<td><button class="btn" type="button" name="edit" value="Edit" onclick="editMedRow('+medCurrentIndex+');">Edit</button></td>'
+'<td><button class="btn" type="button" name="delete" value="Delete" onclick="deleteMedRow('+medCurrentIndex+');">Delete</button></td>'
</c:when>
<c:otherwise>
+'<td><input type="text" id="cost" /></td>'
</c:otherwise>
</c:choose>
+'</tr>');
I want to emphasize that although the above snippet might work, I wouldn't recommend it. A better solution would be to dump ${role} into a global variable or a hidden input and do the decision making from the JS side. Or set the whole append string in jstl. Mixing the two makes this significantly harder to read.
Related
I am working on jsp for loops.I have a requirement where I loop the list and display the data for each iteration..I have html div tag with class name to display all the data for each iteration. however, if one of the condition is met in that loop I should not display that particular data. When I tried to hide through .className.hide() it is hiding all the div data in the previous iterations.
Sample code
`
<s:iterator value="dispDomainList" var="curDomain">
<c:choose>
<c:when test="${dFlags.AFlag == 'false'}">
<script type="text/javascript">
$(document).ready(function() {
$('.pClass).hide();
});
</c:when>
</c:choose>
<c:choose>
<c:when test="${dFlags.BFlag == 'false'}">
<script type="text/javascript">
$(document).ready(function() {
$('.pClass).hide();
});
</c:when>
</c:choose>
<div class="col-lg-12 pClass>
DATA HERE
</div>
</s:iterator>
`
if my dFlags.AFlag is true then it shoudld display data. Now if dFlags.BFlag is false, it hides the whole data from dFlags.AFlag condition too. help me out here.
$('.pClass).hide();
will hide the whole div where you expect your data to be displayed. You do not really need jQuery to hide/show data in the loop as you already use JSTL tags. Just use
<c:if test="${dFlags.AFlag == 'true'}">
<!-- display data -->
</c:if>
<c:if test="${dFlags.BFlag == 'false'}">
<!-- do nothing -->
</c:if>
here is my problem. How could I hide the value of the parameter from the url? because I don't have idea how to hide it. it keep on appearing like this (http://localhost:8084/YIP/MentorServlet?action=peribadi&mentorid=951218-02-5598)
<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×
</a>
<% String id=request.getParameter("mentorid");%>
<li>
Utama
</li>
<li>
Peribadi
</li>
Some options:
do nothing: this is the best one, as there is no such thing as securely hiding something in HTML. Whoever looks into the page source, will see how the servlet in question can be invoked
switch to a form and a submit button, something what #alayor shows. If you use POST, the parameters will not appear in the URL
switch to a form, but keep the looks of an anchor and submit form from JavaScript (some docs some overcomplicated examples)
manipulate browser history from the target page (docs1, docs2)
keep mentorid in a session variable on server-side: hackers never see it
keep mentorid in an encrypted cookie: hackers see it, but can not decode. However they can try reusing it later (replay attack)
the various other ones I have forgotten and/or never even heard about
You can create an HTML for instead of an anchor.
<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×
</a>
<% String id=request.getParameter("mentorid");%>
<li>
Utama
</li>
<li>
<form action="/MentorServlet" method="POST">
<input type="hidden" name="action" value="peribadi" />
<input type="hidden" name="mentorid" value="<%=id%>" />
<button>Peribadi</button>
</form>
</li>
This way you can avoid sending the parameter in the URL and it will send in the HTTP Request Body instead.
I have below CheckBox in JSP file
<input type="checkbox" name="vehicle" value="Bike"
onclick="javascript:selectCustomers(${sessionScope.custId});">
Getting the following error:
org.apache.jasper.JasperException: customer.jsp(1419,33) According to TLD or attribute directive in tag file,
attribute onclick does not accept any expressions
Can we not use expression language in JavaScript (in my case under onClick() Event)?
When a JSP page is called, the following happens, in this order:
Server checks to see if the .jsp has already been compiled and whether or not it has changed since it was last compiled.
Server runs the jsp through the Jasper compiler, which interprets the jsp into Java code, anything that is not Java (CSS, HTML, JavaScript, etc) is placed in a String.
The Java code is compiled and executed.
The results are placed in the response and sent to the user.
So, your statement: ${sessionScope.custId} is executed before the the HTML is sent to the user, and the input of selectCustomers() function is already set to before calling it.
For more info have a look at my another post JSP inside ListItems onclick
How to verify it?
Right click in the browser and look at the view source.
Try below sample code that might help you.
Enclose ${...} inside the single quotes.
<c:set var="custId" value="1234" scope="session" />
Before :
<c:out value="${sessionScope.custId}"></c:out>
<input type="checkbox" name="vehicle" value="Bike"
onclick="javascript:selectCustomers('${sessionScope.custId}');">
<c:set var="custId" value="4321" scope="session" />
After:
<c:out value="${sessionScope.custId}"></c:out>
View Source code: (Right click in browser to view it)
Before : 1234
<input type="checkbox" name="vehicle" value="Bike"
onclick="javascript:selectCustomers('1234');">
After: 4321
Try this:
<input type="hidden" id="custId" name="custId" value="${sessionScope.custId}">
<input type="checkbox" name="vehicle" value="Bike" onclick="javascript:selectCustomers();">
function selectCustomers(){
var custId = document.getElementById('custId').value;
}
In my struts2 Application I have a JavaScript function to do the cancel. this method is in the header and only do a window.location to my return URL. Since we did a migration from version 2.0.11 of struts 2.3.8 release, it no longer works. When I look at the generated HTML code we see that fails to interpret the tag s:property because the URL is empty. I do not see what does not work.
in my IDE :
function cancel() {
if (!isModified || (isModified && askConfirmCancel())) {
window.location.replace('<s:property value="#urlBack"/>');
}
}
result with Firebug :
function cancel() {
if (!isModified || (isModified && askConfirmCancel())) {
window.location.replace('');
}
}
in JSP file :
<tr>
<td height="6%"align="center">
<s:submit cssClass="button" key="common.initDelegate.label" align="center" theme="simple"/>
<s:url id="urlBack" action="myAction" includeParams="none" escapeAmp="false">
<s:param name="period.periodId" value="%{period.periodId}"></s:param>
</s:url>
<input type="button" onclick="javascript:cancel()" value="<s:text name="common.button.cancel"/>"/>
</td>
</tr>
<s:property value="#urlBack"/> -- do we need "#" before urlBack. All we need is urlBack property in action class with public String getUrlBack() method. And the variable should get initialized before Action returns the success/result.
First you need to create a variable urlBack then use it for render URL. For example
<s:url var="urlBack" action="myAction" escapeAmp="false">
<s:param name="period.periodId" value="%{period.periodId}"></s:param>
</s:url>
<input type="button" onclick="cancel();" value="<s:text name="common.button.cancel"/>"/>
<script type="text/javascript">
function cancel() {
if (!isModified || (isModified && askConfirmCancel())) {
window.location.replace('<s:property value="#urlBack"/>');
}
}
</script>
also
includeParams by default is none since Struts 2.1.3.
id attribute is replaced by var
javascript: is a protocol for URLs not for JS functions, see this.
In action I have a variable which has getter on it.
private String myActionVariable = "predefined....";
public String getMyActionVariable () {
return myActionVariable;
}
In jsp, I try to use my variable in this way:
<input type="button" class="styledButton"
onclick="javascript: doAjax('myActionName',false);"
value="${myActionVariable}"
But it is not shown. However, if I output this variable from the javascript code included within the same jsp file:
alert (${myActionVariable})
I will get the value of it....
Any idea please ? ...
You can use a Standard <input/> HTML Tag with an <s:property /> Struts2 Tag for the value, like this:
<input type="button" class="styledButton"
onclick="javascript:doAjax('myActionName',false);"
value="<s:property value="%{myActionVariable}"/>"/>
or a Struts2 Tag directly like this:
<s:submit type="button" cssClass="styledButton"
onclick="javascript: doAjax('myActionName',false);"
value="%{myActionVariable}" />
Note that with Struts2 Tag, class attribute becomes cssClass (and style becomes cssStyle), and that %{} is the right OGNL syntax, instead of ${} (that is JSTL syntax)
EDIT: when using Struts2, forget about JSTL, you won't need them anymore.
You should be using struts2 tag.
<input type="button" class="styledButton" onclick="javascript: doAjax('myActionName',false);" value="${myActionVariable}">
Instead of this, use
<s:submit type="button" cssClass="styledButton" onClick="javascript: doAjax('myActionName',false);" value= "myActionVariable" />