开发者

jstl problem : 2 last scriptlet to remove from my jsp

I use a code that works fine but here's the last scriptlets in my jsp :

<%  List listMillesime= MultiMillesimeFactory.getInstance().getListMillesimeActif();
    pageContext.setAttribute("listMillesime",listMillesime);
    %>
...
<c:forEach var="millesime" items="${listMillesime}">
...
</c:forEach>

Here is the factory declaration :

public class MultiMillesimeFactory {

    private static MultiMillesime multiMillesime;

    private MultiMillesimeFactory(){
    }

    public static MultiMillesime getInstance() {
        if (multiMillesime == null) {
            multiMillesime = new MultiMillesime();
        }
        return multiMillesime;
    }
}

Multimillesime is a standard class with a method getListMillesimeActif r开发者_如何学JAVAeturning a list. Declaration :

public class MultiMillesime {
...
   public List getListMillesimeActif() throws Exception {
     List _l = Collections.synchronizedList(new LinkedList());
...
     return _l;
   }
}

c:for each works fine on list and enum but here the problem comes from the use of the getInstance, I've tried MultiMillesimeFactory.Instance.ListMillesimeActif without success.

And the last scriptlet is :

<a href='<%=request.getContextPath() %>

I'm not sure that both of these are removable.


You can replace the <%=request.getContextPath() %> scriptlet with a JSTL tag:

<a href="${pageContext.request.contextPath}" />

It is not uncommon to see something like this, either:

<c:set var="ctx" value="${pageContext.request.contextPath}"/>
...
<a href="${ctx}" />

As for the issue with the MultiMillesimeFactory: it looks like that's a class in your codebase, and we can't magically debug that issue for you. It doesn't sound like a JSP problem.

What does "tried MultiMillesimeFactory.Instance.ListMillesimeActif without success" mean?


Edit: re: your comment: I'm not sure I understand what you mean, but I'd guess the problem is that you need to use the fully qualified class name of MultiMillesimeFactory to look it up properly. However, you really should avoid using scriptlets as much as possible. Instead, use a proper servlet to inject the ListMillesimeActif into the request, like this:

public class MyServlet extends HttpServlet implements Servlet
{
    //...

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    {
        //...
        List listMillesime = MultiMillesimeFactory.getInstance().getListMillesimeActif();
        request.setAttribute("millesime", listMillesime);
        //...
    }

    //...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜