JSP/Servlet 属性验证
有没有办法验证从 Servlet 传递到 JSP 的请求属性?
例如,在我的 Servlet 中我做了这样的事情:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Foo foo = new Foo();
request.setAttribute("foo", foo);
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/bar.jsp);
dispatcher.forward(request, response);
}
bar.jsp 看起来像这样:
<html>
<body>
${foo.getBaz}
</body>
</html>
在 bar.jsp 中,有没有办法确保“foo”属性是 Foo 对象?我们使用 Maven JSPC 插件来编译 JSP,并且最好能够在编译时捕获重构/重命名错误(例如 Foo.getBaz() 被重命名为 Foo.getFluff())。
Is there a way to validate request attributes passed from Servlet to JSP?
For example, in my Servlet I do something like this:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Foo foo = new Foo();
request.setAttribute("foo", foo);
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/bar.jsp);
dispatcher.forward(request, response);
}
bar.jsp looks like this:
<html>
<body>
${foo.getBaz}
</body>
</html>
In bar.jsp, is there a way to ensure that the "foo" attribute is a Foo object? We use the Maven JSPC plugin to compile JSPs and it'd be nice to catch refactoring/renaming errors (like Foo.getBaz() being renamed to Foo.getFluff()) at compile-time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在Java中查看运算符的实例。它应该解决您的问题。
check out the instanceof operator in java. It should solve your problem.
您可以使用如下脚本:
然后在 HTML 中使用
baz
作为<%= baz %>
You could use a scriptlet such as this:
and then use
baz
within your HTML as<%= baz %>