0

I am working on a project using struts2. In my Action class I have a String testString with setter/getter and I can access it from my jsp file using ${testString}. Works fine.

Now I need to access it form javascript and tried an internal script inside my jsp file like this:

<script >
function testJs() {
    alert("${testString}");
}
</script>

I used the testJs() method in an button onclick

<input type="button"  value="click me " onclick="testJs()" >

This also works fine, means when i click the button is shows me the value of the string(Hello World) of my Action class in an alert.

But the problem is when i added this method in a external (test.js) file and added the .js file in jsp file like this

<script src="resource/js/test.js" ></script>

The alert message don't show the String value. its just show ${testString}

The method testJs() get and parse the String value from an internal script but cant from an external script!!!

Any possible reason or explanation for this.?

If any body want some additional information please let me know.

2
  • The external javascript file is not processed by the template engine, therefore placeholder are not evaluated. A common approach is to define the variables in your template and then reference them in your external javascript file e.g. var messages = { testString: '${testString}'}; or process the test.js with your template engine.
    – Andreas
    Aug 7, 2014 at 10:08
  • can you please explain the approach using an example of considering my case. That will be helpful @Andreas
    – Saif
    Aug 7, 2014 at 10:14

1 Answer 1

0

When you write EL(Expression Language) in your .jsp file, that .jsp file is parse by its JSP container and EL are processed. Consider the following code :

anyfile.jsp

<script type="text/javascript">
function testJs() {
    alert("${testString}"); // JSP Container will replace '${testString}' with its value
}
</script>

JSP container did not processed any of the above code Except EL, and therefore only ${testingString} is replaced. I mean, container do not know about <script> or function testJs(). JSP container will only process the files with .jsp (or .jspx, or some other files) extension. In the above code your javascript was inside .jsp file.
But on the other hand, when you replaced above code with this -

<script src="resource/js/test.js" ></script>

Now the previous code is inside test.js file. But how container can know about that! You are expecting JSP container to understand this code i.e. to understand that test.js is added as javascript in the .jsp file by using <script> element and src attribute. But it didn't.
Container actually not have to do anything with this code. And your code will be sent to client without any changes.
But if you stills want to use your javascript code to work from other file, you can use following code :

anyfile.jsp

<script type="text/javascript">
    <jsp:include page="test.jsp" />
</script>

And

test.jsp

<%@page contentType="text/javascript" pageEncoding="UTF-8"%>
function go() {
    alert("${testString}");
}
6
  • well explained, but i got mu self confused when you gave the solution and the end. Can you please review it. @afzalex
    – Saif
    Aug 10, 2014 at 9:33
  • why do you think I am confused? tell me what exactly you want if this answer didn't satisfy you, I will edit it. @Saif
    – afzalex
    Aug 10, 2014 at 9:37
  • sorry for a silly typo. I said that i am confused not you . My problem is why i need another anyfile.jsp and in case of test.jsp you still writing javaScript code in .jsp file. i want to write it in a .js file.
    – Saif
    Aug 10, 2014 at 9:50
  • 1
    You didn't understand. if you use .js file, it will not remain to be parsed by JSP container. And .js file is for your browser, JSP container. And your .js file will remain unchanged, because container has not done any change to it. if you still want to do it you have to use ajax, which another javascript language. @Saif
    – afzalex
    Aug 10, 2014 at 9:59
  • so you are telling to use an .jsp file instead of .js file and use it as a javaScript source to overcome the problem? if that so its going to be a nice strick @afzalex
    – Saif
    Aug 10, 2014 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.