jQuery adding to JSP page

后端 未结 3 842
生来不讨喜
生来不讨喜 2021-01-02 12:48

I have a piece of jQuery code which I found on the internet and I want to integrate it to my jsp page, I use Spring form tags.

Here is the jQuery code:



        
相关标签:
3条回答
  • 2021-01-02 13:19

    For Dynamic Web Project(designed using MVC Model)

    Add like this in head section:

    <script type="text/javascript" src="${pageContext.request.contextPath}/jQuery.js"/></script>
    

    I kept my jQuery.js in WebContent folder(with jsp pages).

    0 讨论(0)
  • 2021-01-02 13:23

    jQuery, like any JavaScript, is added in a <script> tag in the <head> tag of your JSP page. You either add all the code or just a link to the .js file containing your jQuery, like for example :

    <script src="./libs/jquery/1.10.1/jquery.min.js"></script>
    

    Having done that, you want now to leverage your jQuery in the HTML tags, you do that as for any HTML page. Namely, in your case, you don't have to take away the spring tags. Let it generate the select/options via your ${listOfInstitutionsNames}, just add class="testclass" to your spring form tag, like this :

    <form:form  cssClass="testclass" id="myform" modelAttribute="fboAttribute" method="post" action="add" >
    

    When rendering the form on a browser, Spring will include in the generated HTML the class attribute with value of testclass.

    Hope that helps, best of luck.

    0 讨论(0)
  • 2021-01-02 13:42

    if what you mean is that you want to bind Java side information to JS var, you can do as I did:

    1. At Java side, use Google's Gson to encode Java object to Json string.

    2. At Java side, use org.apache.commons.lang.StringEscapeUtils.escapeJavaScript(String) to make you Json string escaped as JavaScript.

    3. At JSP side, do something like this:

        <script>
        var jsonObject = JSON.parse("<%=yourJsonString%>");
        </script>
    
    0 讨论(0)
提交回复
热议问题