access object using custom tag in JSP

1.8k Views Asked by At

I am trying to pass session object from sessionConstant.java to my COMP.jsp file....

sessionConstants.java

public class SessionConstants {


    public static final String COMP_TYPE_GRID_JSON = "companyTypeGridModelObj";



}

and in jsp file i have a *.js script.......

<script type="text/javascript" charset="utf-8">
    var aa = "${cons.COMP_TYPE_GRID_JSON}";
    var bb = "${sessionScope.companyTypeGridModelObj}";

    var jsonModal = <myComp:JSON object = "${bb}" />


</script>

Q1. how do i pass value in var aa instead of 'companyTypeGridModelObj' in sessionScope Object defined in var bb ...

Q2. how do i pass var bb in my jstl

please help

for code in java where this attribute is being passed

NgGridModelConvertor ngGridModelConvertor = new NgGridModelConvertor();
NgGridModel ngGridModel = ngGridModelConvertor.getNgModelData1(columnModeList, dataMapList, rtlOrientation);
getSession().put(SessionConstants.COMP_TYPE_GRID_JSON, ngGridModel);
1

There are 1 best solutions below

5
On

You can refer to this thread which may answer your problem.

First you need to insert you value inside the session scope. Then you can access your data from your JSP file. As you are using tag, I suppose you're not using separate js file. If you do use separate file, then you'll have to use another way to pass your data, as Ajax call or hidden input.

Servlet :

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    HttpSession session = request.getSession(); 
    session.setAttribute("Questions", getQuestion());
}

JSP :

<c:out value="${sessionScope.Questions.questionPaperID}" />

OR to have named value :

<c:set scope="session" var="varName" value="${expression}" />
<c:out value="${varName}" />

for your situation :

<c:set scope="session" var="bb" value="${sessionScope.companyTypeGridModelObj}" />
<p> My value is : ${bb}</p>

EDIT : Use of TagLib

Here you have the official documentation on TagLib. You should read this, it will probably give you more information.

For your problem : In your tag file you declare a tag with attributes. That way, when you call this tag you can pass your ${bb} value to the tag.

In my tag file (reference to my personal code, maybe it can be improved)

// Import c library
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
// Import fn library
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
// Declare my tag in my own library (give location)
<%@taglib tagdir="/WEB-INF/tags/" prefix="bookmark" %>
// Declare an attribute for this tag
<%@attribute name="directories" required="true" rtexprvalue="true" type="java.util.List"%>
// use the attribute
<c:forEach var="directory" items="${directories}">
</c:forEach>

In my main JSP

// Import my tag lib
<%@ taglib tagdir="/WEB-INF/tags/" prefix="bookmark" %>
// Use my tag and pass ${directories} value to the tag
<bookmark:directories directories="${directories}"></bookmark:directories>