jstl 问题:2 最后一个要从我的 jsp 中删除的 scriptlet

发布于 2024-09-30 14:28:06 字数 1196 浏览 3 评论 0原文

我使用的代码工作正常,但这是我的 jsp 中的最后一个 scriptlet:

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

这是工厂声明:

public class MultiMillesimeFactory {

    private static MultiMillesime multiMillesime;

    private MultiMillesimeFactory(){
    }

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

Multimillesime 是一个标准类,其方法 getListMillesimeActif 返回一个列表。声明:

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

c:foreach在列表和枚举上工作正常,但这里的问题来自于getInstance的使用,我尝试过MultiMillesimeFactory.Instance.ListMillesimeActif但没有成功。

最后一个脚本是:

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

我不确定这两个都是可移动的。

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 returning 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘东忘西忘不掉你 2024-10-07 14:28:06

您可以将 <%=request.getContextPath() %> scriptlet 替换为 JSTL 标记:

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

并不罕见:

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

看到这样的内容 MultiMillesimeFactory:看起来这是您代码库中的一个类,我们无法神奇地为您调试该问题。听起来不像是 JSP 问题。

“尝试 MultiMillesimeFactory.Instance.ListMillesimeActif 但没有成功”是什么意思?


编辑:回复:您的评论:我不确定我明白您的意思,但我猜问题是您需要使用 MultiMillesimeFactory 正确查找它。但是,您确实应该尽可能避免使用 scriptlet。相反,使用适当的 servlet 将 ListMillesimeActif 注入到请求中,如下所示:

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

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

    //...
}

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);
        //...
    }

    //...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文