给个例子来复习一下,模拟地址本显示。
联系人类:Contact
这里顺便学些一下自动排序的做法,定义Contact类,实现Comparable接口。Contact是个简单的POJO,getter和setter从略。此外MonthDay和Instant是Java 8增加的时间处理类。
public class Contact implements Comparable<Contact>{
private String firstName;
private String lastName;
private String phoneNumber;
private String address;
private MonthDay birthday;
private Instant dateCreated;
public Contact() { }
public Contact(String firstName, String lastName, String phoneNumber,
String address, MonthDay birthday,Instant dateCreated) {
......
}
..setters 和 getters....
@Override
public int compareTo(Contact other) {
int last = lastName.compareTo(other.lastName);
if(last == 0){
return firstName.compareTo(other.firstName);
}
return last;
}
}
Servlet:将数据传递给jsp文件
ListServlet如下。一般而言,应该将jsp隐藏到/WEB-INF这个安全目录下,而不是在外面可直接读取的位置。
@WebServlet(name = "ListServlet",
urlPatterns = "/list")
public class ListServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/* Contact类实现了排序接口Comparable,在此我们定义排序Set,采用TreeSet方式*/
private static final SortedSet<Contact> contacts = new TreeSet<>();
/* 预设数据,模拟地址本*/
static{
contacts.add(new Contact("Flowingflying", "Wei", "13312345678", "Guangzhou",
MonthDay.of(Month.MAY, 20),Instant.parse("2016-08-05T21:34:12Z")));
contacts.add(new Contact("John", "Smith", "17712345678", "Guangxi",
MonthDay.of(Month.APRIL, 21),Instant.parse("2016-08-04T20:31:13Z")));
contacts.add(new Contact( "Peter", "Smith", "555-0712", "315 Maple St",
null,Instant.parse("2012-10-15T15:31:17Z")));
}
... ...
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if(request.getParameter("empty") != null){/* 为了jsp测试,传递一个空的地址本过去 */
request.setAttribute("contacts", Collections.<Contact>emptySet());
}else{/* 传递地址本信息过去 */
request.setAttribute("contacts", contacts);
}
/* 跳转到jsp中,注意jsp存放的路径*/
request.getRequestDispatcher("/WEB-INF/jsp/view/list.jsp").forward(request, response);
}
}
jsp:跳转
index.jsp跳转至/list
<c:redirect url="/list" />
在base.jspf中已经给出引入:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
web.xm有如下信息:
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<url-pattern>*.jspf</url-pattern>
<page-encoding>UTF-8</page-encoding>
<scripting-invalid>false</scripting-invalid>
<include-prelude>/WEB-INF/jsp/base.jspf</include-prelude>
<trim-directive-whitespaces>true</trim-directive-whitespaces>
<default-content-type>text/html</default-content-type>
</jsp-property-group>
</jsp-config>
但是,在Eclipse有个问题,如果我们不在本文件中给出taglib,那么jsp就不能自动使用智能拼词,并出现warning,这个是很讨厌的,所以我喜欢在jsp中多写两行,引入tablib。
jsp:list.jsp显示信息
<!-- 下面这句并没有什么实质意义,至少在Eclipse如此,但是仍建议加上,表面传递的信息contacts所对应的类为.Set<Contact> -->
<%--@elvariable id="contacts" type="java.util.Set<cn.wei.flowingflying.chapter07.Contact>" --%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>List</title>
</head>
<body>
<h2>Address Book Contacts</h2>
<c:choose><-- 如果信息为空如何,不为空如何 -->
<c:when test="${fn:length(contacts) == 0}"><!-- 信息为空 -->
There are no contacts in the address book.
</c:when>
<c:otherwise><!-- 信息不为空,链接地址为list?empty可以模拟 -->
There are ${fn:length(contacts)} contacts in the address book.<br/>
<c:forEach items="${contacts}" var="contact">
<c:out value="${contact.lastName},${contact.firstName}"/><br/>
<!-- 使用c:out的方式,而不是直接EL输出,可以避免里面带有xml的字符,有则进行转义,一来避免引发混乱,-->
<!-- 二来防止用户输入的时间注入恶意代码。采用直接输入的是两个时间,不是String,不会携带xml字符。 -->
<c:out value="${contact.phoneNumber}"/><br/>
<c:out value="${contact.address}"/><br/>
<c:if test="${contact.birthday != null}">
BirthDay: ${contact.birthday}<br/>
</c:if>
Created:${contact.dateCreated}<br/><br/>
</c:forEach>
</c:otherwise>
</c:choose>
</body>
</html>
相关链接: 我的Professional Java for Web Applications相关文章