简体   繁体   English

在 JSP 中格式化 HTML

[英]Formatting HTML in a JSP

I'm pretty new to Java and I apologize in advance if I'm wording this incorrectly.我对 Java 还很陌生,如果我的措辞不正确,我提前道歉。 I've got a small code snippet that has multiple opening and closing delimiters because I've got some HTML mixed with JSP.我有一个包含多个开始和结束分隔符的小代码片段,因为我有一些 HTML 与 JSP 混合在一起。 Without the HTML this can be done in just a few lines of code but I need the HTML to render and it leads to almost double the lines of code.如果没有 HTML,这只需几行代码即可完成,但我需要渲染 HTML,这导致代码行数几乎翻了一番。 I'm wondering if there is a better way to do this as opposed to having so many opening and closing delimiters.我想知道是否有更好的方法来做到这一点,而不是有这么多的开始和结束分隔符。 I know I can use a templating library but I'm trying to stay away from that and would like if at all possible to do this inside a JSP (not a separate class).我知道我可以使用模板库,但我试图远离它,如果可能的话,我希望在 JSP(而不是单独的类)中执行此操作。 Thanks for the help!谢谢您的帮助!

<%
try {
  List<Page> children = properties.getPath("getChild", "");    
%>    
  <ul>       
<%      
  for (Page children : e) {
    if (children != null) {         
%>
   <li><a href="#">Show a link</a></li>
<%
   }//end if statement
  }//end for loop
%>
  <li><a href="<%= currentPage.getPath() %>" href="<%= currentPage.getPath() %>">Another link goes here</a></li>    
  </ul>
<% 
} catch (NullPointerException e){
%>
//show some content here
<% } %>

Use JSTL instead of all that Java code you have in your JSP.使用 JSTL 而不是 JSP 中的所有 Java 代码。 You can read about JSTL here - http://docs.oracle.com/javaee/5/tutorial/doc/bnakc.html您可以在此处阅读有关 JSTL 的信息 - http://docs.oracle.com/javaee/5/tutorial/doc/bnakc.html

I think that this kind of problem can really be solved by using templating libraries or the included view convention for a framework.我认为这种问题真的可以通过使用模板库或框架的包含视图约定来解决。

These libraries were created to solve these kinds of problems and to organize the view as a whole.创建这些库是为了解决此类问题并将视图组织为一个整体。 It will not only clear up the clutter in your view, it will also adhere to the MVC pattern.它不仅会清除您视图中的杂乱,还会遵守 MVC 模式。

In struts, for example, we will do something like this:例如,在 struts 中,我们会做这样的事情:

<s:textfield name="myParameter" /> 

and this:和这个:

<html:link page="/linkoutput.jsp" paramId="id" paramName="name"/>

For more reasons why you should be using templating or frameworks visit this question .有关您应该使用模板或框架的更多原因,请访问此问题 As what Dave Newton said:正如戴夫牛顿所说:

Attempting to do it all in jsp is precisely the wrong approach.试图在 jsp 中完成这一切恰恰是错误的方法。

Hope this clears it up.希望这能解决问题。

  1. Never catch NPE just to catch NPE.永远不要为了抓住 NPE 而抓住 NPE。

  2. Consider using JSTL http://jstl.java.net/getStarted.html It provides plenty of tags for iteration and so on.考虑使用JSTL http://jstl.java.net/getStarted.html它提供了大量的迭代标签等等。

You'd be much better of using something like JSF.使用 JSF 之类的东西会好得多。 Just as JSP, this is also included in Java EE.与 JSP 一样,这也包含在 Java EE 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM