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;
}
Related
I know it is a very classic question, but I don't know why this doesnt work for me!!!
<input id="code" value="45" type="hidden" name="code" >
<%=request.getParameter("code") %>
the java tag is supposed to show me 45, but it shows null
Jsp code will execute in your container and at that time the code value will be null.
And the below code prints non null when the incoming request(get/post) contains code parameter in it
<%=request.getParameter("code") %>
So to make it print 45 append this your.jsp?code=45 at end of url then it will print 45 in the page
You need html form tag to send the parameters.
Ex:
<form>
<input id="code" value="45" type="hidden" name="code">
<%=request.getParameter("code") %>
<button type="submit">Send</button>
</form>
$("#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.
Is there a way to get the name of the form element itself using JSP? I searched google and did not find any examples which show how to get the name of the form value specified in the JSP file.
For example, I have the below form,
<html>
<form name="register" action="servregister" method="POST"
onsubmit="return validate();">
</form>
</html>
I would like to get the string "register" to a string variable in JSP. Is there a way to do this?
No, a <form> is not submitted with the request. Instead create a hidden input element which holds that information.
<input type="hidden" name="name of form" value="value" />
or possibly the submit element
<input type="submit" name="distinguishing name" value="submit" />
Either of these in a form with be sent as a url-encoded parameter.
There is probably a better solution to what you are trying to do if you explain your goals.
Consider looking into different patterns for achieving your goals.
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" />
Using IE 7, JDK 1.6 and Sun Web server 7.
Inside the jsp form, we have:
<input type="text" name="id" maxlength="20" />
<input ... type="submit" name="send" value="Send"/>
i.e. a text box and a "Submit" button (called Send).
and the servlet has:
if (request.getParameter("send") != null && request.getParameter("send").trim().length() > 0) { ... }
Using Fiddler and IE, we can see that the following is sent when we populate the id text box and hit Enter:
id=123456
However, using Fiddler and IE, we can see that the following is sent when we populate the id text box and click the Send button:
userId=123456&send=Send
The end result is that hitting the Enter key effectively does nothing.
On other jsp pages, e.g. we have:
<input type="text" name="id" maxlength="20" />
<input ... type="submit" name="submitId" value="Submit"/>
and the servlet has:
if (request.getParameter("submitId") != null && request.getParameter("submitId").trim().length() > 0) { ... }
Using Fiddler and IE, we can see that the following is sent for both cases:
id=123456&submitId=Submit
So it seems to us that the behaviour is only exhibited on IE for forms where the "Submit" button is not called "Submit"?
Re-running the tests on Firefox 3.6 shows that the behaviour is correct and the same for both cases.
Any suggestions for getting IE to work correctly?
(Note: I have searched SO for a similar problem but the questions relating to this mainly all ASP related!).
This is indeed another IE anomaly in case of forms with only one input field. The only solid workaround for this is to add a second input field(!). You can hide it using CSS. No, type="hidden" ain't going to work.
<input type="text" name="id" maxlength="20" />
<input type="text" style="display: none;" />
<input type="submit" name="send" value="Send"/>
Why are you checking for request.getParameter("submitId") in your JSP when in fact submitId is the name of your submit button?
In my experience I never had to check the value for the submit button. I only used that button to trigger the form submit and would usually only be interested in retrieving the values for the other form parameters.
If you want to differentiate the submit methods by the name of the submit button, you might want to try adding a "hidden" property using input type="hidden".