我想在JSP的下拉列表中填充数据库中的值。
问题是,我没有从任何servlet向该JSP转发请求。我直接从URL打开此JSP。

http://mywebsite/thisJsp.jsp


现在,我不想在JSP中使用任何Java代码,而正在使用EclipseLink JPA。我所做的是创建了一个过滤器类。

public class MyFilter implements Filter{

    private FilterConfig filterConfig = null;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;

        String servletPath = httpRequest.getServletPath();
        if(servletPath != null & servletPath.endsWith("thisJsp.jsp")) {
            CatalogDB cdb = new CatalogDB();
            List<Catalog> catalogs = cdb.getCatalogs();
            request.setAttribute("catalogs", catalogs);
        }
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {

    }

}


现在,我在web.xml中配置了此过滤器

<filter>
        <filter-name>myFilter</filter-name>
        <filter-class>MyFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>myFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>


问题是流程进入过滤器并从数据库中获取值。它甚至将其设置在请求对象中。但是在JSP上,我收到一个错误,找不到“目录”。

最佳答案

非常抱歉,JSP中存在小错误。
我在用

<c:forEach var="catalog" items="catalogs" >


代替

<c:forEach var="catalog" items="${catalogs}" >

09-05 18:52
查看更多