gpt4 book ai didi

java - 填充 Filter 类中的 JSP 下拉列表

转载 作者:太空宇宙 更新时间:2023-11-04 14:23:24 26 4
gpt4 key购买 nike

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

关于java - 填充 Filter 类中的 JSP 下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26931157/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com