kshtjsnghl
kshtjsnghl

Reputation: 601

Query regarding JSP variable access

Forgive my ignorance, I am stuck with this. What I need to do is, access a date member in my bean and pass it to a method defined, something like below -

<%!
        SimpleDateFormat desiredDateFormat = new SimpleDateFormat("yyyy/mm/dd HH:mm:ss");


        String getFormattedDate(Date inputDate){
            if(inputDate == null){ 
                return null;
            }
            else{
                return desiredDateFormat.format(inputDate);
            }
        }

    %>

Html part of my jsp page looks like this.

<table cellpadding="0" id="proposals" cellspacing="0" border="1 px"
                        class="dataTable">
                        <thead>
                            <tr>
                                <th>Proposal Id</th>
                                <th>Release Candidate Id</th>
                                <th>Proposal Description</th>
                                <th>Application</th>
                                <th>Requester</th>
                                <th>Proposal Status</th>
                                <th>Proposal Creation Date</th>
                                <th>Planned Proposal Deployment Date</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <c:forEach var="proposal"
                                items="${serviceOutput.ret.proposalsList}">
                                <tr class="proposalRow" id="${proposal.id}">
                                    <td><a href="/proposal?action=view&tab=general&proposalId=${proposal.id}">${proposal.id}</a></td>
                                    <td><a href="/releaseCandidate?action=view&tab=general&releaseCandidateId=${proposal.releaseCandidateId}">${proposal.releaseCandidateId}</a></td>
                                    <td>${proposal.description}</td>
                                    <td>${proposal.application}</td>
                                    <td>${proposal.requester}</td>
                                    <td>${proposal.status}</td>
                                    <td><%= getFormattedDate(${proposal.creationDate})%></td>
                            <td><%= getFormattedDate(${proposal.plannedDeploymentDate})%></td>
                                    <td><a
                                        href="/proposal?action=view&tab=general&proposalId=${proposal.id}">Edit</a></td>
                                </tr>
                            </c:forEach>
                        </tbody>
                    </table>

As you would have guessed, I am not able to access these date members - creationDate and plannedDeploymentDate in the html code. Can anyone please suggest how I can do that.

Upvotes: 0

Views: 316

Answers (1)

BalusC
BalusC

Reputation: 1109695

Do not use scriptlets. Use JSTL <fmt:formatDate>.

Add this to top of your JSP:

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

Then replace

<td><%= getFormattedDate(${proposal.creationDate})%></td>
<td><%= getFormattedDate(${proposal.plannedDeploymentDate})%></td>
              

by

<td><fmt:formatDate value="${proposal.creationDate}" pattern="yyyy/mm/dd HH:mm:ss" /></td>
<td><fmt:formatDate value="${proposal.plannedDeploymentDate}" pattern="yyyy/mm/dd HH:mm:ss" /></td>

And remove that unnecessary scriptlet function.

See also:

Upvotes: 2

Related Questions