简体   繁体   中英

Is there any way to change the jsp custom tag value using javascript at clientside

My intention is to change my jsp custom tag value using javascript at client side. The the tag is giving some date value. Before redering the value I'm calling javascript function and trying to do required changes on it and returning the result date. But failing to handle jsp tag and javascript according to my requirement. I did like this..

<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>
<%@ taglib uri="/WEB-INF/fmt.tld" prefix="fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<script>
var createdDate;
window.onload = function() {
createdDate = '${myBean.createdDate}';
    // modifying the createdDate 
}
function getModifiedDate(){
return createdDate;
}
</script>
<table>
.
.
.
<tr><td>
 <!-- I modified the code like this -->
<fmt:formatDate value='<script>getModifiedDate();</script>'  type="both" pattern="${viewDatePattern}" />
</td></tr>
.
.
.
</table>

previously it is like this..

<fmt:formatDate value="${myBean.created}" type="both" pattern="${viewDatePattern}" /> 

I executed the application with my changes but no luck,please provide the solution to render the date value through the tag after its modification from the script.

I came up with one kind of solution for this, prior to that I missed out few things while posting the question.. JSP and its tags(inbuilt/custom) are something related to service side but not for client browser. JSP engine writes a servlet according to tags what we written in our jsp page. we can call jsp some tags inside javascript function but calling a javascript function inside a tag cannot be possible. For above scenario, I wrap the rendered tag value with a <div> , and located this tag inside the javascript function and did my modifications on the rendered data and then set the result as innerHTML of same <div> .. here is the approach..

  <div id="requestDateId" style="height: 5px;">
    <fmt:formatDate value="${myBean.created}" type="both" />
    </div>   

inside script..

<script>
  window.onload = function() {
    var requestDate = document.getElementById("requestDateId").innerHTML;
    if(requestDate)
    document.getElementById("requestDateId").innerHTML = foo(requestDate);
  }
  function foo(requestDate){
     // my logic 
    return requestDate;
  }
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM