构造JSP / Java代码,其中逻辑不在JSP文件中 [英] Structure JSP/Java code where logic is not in the JSP file

查看:142
本文介绍了构造JSP / Java代码,其中逻辑不在JSP文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个设计问题和良好实践问题。如何构建Java Web开发,使JSP文件中没有很多逻辑。您是否应该为if结构或循环使用任何JSP标记。我看到很多代码由于JSP文件设计不当而很难遵循逻辑。然后,当您想要输出不同的格式(如PDF或XML文档)时,您无法执行此操作,因为所有逻辑都在JSP文件中。

This is a design question and good practice question. How do you structure your Java web development such that a lot of logic is not in the JSP file. Should you use any of the JSP tags for if structures or loops. I see a lot of code where the logic is hard to follow because of poorly designed JSP files. And then when you want to output to a different format like a PDF or XML document, you can't do it because all the logic is in the JSP file.

Are您有任何关于Java Web开发的提示或实践。

Are there any tips or practices that you follow for Java Web development.

我目前正在使用Spring,Hibernate,Struts的组合......并使用一些Servlet代码。

I am currently using a combination of Spring, Hibernate, Struts...and work with some Servlet code.

有一些与Java开发相关的良好实践。我们中的许多人已经有一段时间了解他们。 JSP开发有哪些好的做法。

There are good practices associated with Java development. Many of us that have worked a while know them. What are some good practices for JSP development.

推荐答案

避免在JSP中放置逻辑的最简单方法就是执行所有逻辑之前将请求转发给JSP。您在JSP中需要做的唯一逻辑是一些基本的循环(例如,用于创建HTML表行),评估条件语句和数据格式。

The easiest way to avoid placing logic in JSPs is simply to perform all that logic before forwarding a request to a JSP. The only logic you should need to do in a JSP is some basic looping (e.g. for creating HTML table rows), evaluating conditional statements, and data formatting.

所有这些可以通过使用JSP标记库和EL在JSP中使用scriptlet代码(Java代码)来完成。最重要的标签库是 JSTL 。 JSTL标记库提供了您在视图中需要执行的大部分逻辑,尽管您可能偶尔也会使用小众第三方标记库,例如 displaytag ,可以减少为特定任务编写的JSP代码量。

All of this can be done without using scriptlet code (Java code) in the JSPs by using JSP tag libraries and EL. The most important tag library is JSTL. The JSTL tag library provides most of the logic you should ever need to perform in a view, though you may occasionally also use niche 3rd party tag libraries like displaytag which can reduce the amount of JSP code you need to write for specific tasks.

这篇关于构造JSP / Java代码,其中逻辑不在JSP文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆