簡體   English   中英

在Filter類中填充JSP下拉列表

[英]Populate JSP drop down in Filter class

我想在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}" >

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM