十、建立读取留言信息的Action类
1、建立GuestBook的JavaBean类
package com.meixin.beans; public class Guestbook { private int id; private String name; private String email; private String url; private String content; private String title; private String time; public String getTime() { return time; } public void setTime(String time) { this.time = time; } //id是主键,不提供set方法 public int getId() { return id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } }
2、读取留言的Action类
package com.meixin.struts.action; import java.sql.SQLException; import java.util.List; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.sql.DataSource; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import com.meixin.beans.Guestbook; public class ReadAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { // 读取留言的SQL语句,以id降序排列,后发的留言先显示 String sql = "select * from guestbook order by id desc"; try { // 建立上下文对象,初始化 Context context = new InitialContext(); // 获取数据源,在Tomcat中的context.xml中设置的数据源名字要一样 DataSource ds = (DataSource) context .lookup("java:/comp/env/jdbc/mssqlds"); // 获取数据库的连接 // ds.getConnection(); // 用QueryRunner封装数据源,这个类完成了许多数据库的操作 QueryRunner qr = new QueryRunner(ds); /** * 执行SQL语句,获取留言记录,这里将返回的每一个元素保存在Guestbook当中 * 得到一个List,将来我们对List进行操作时的对象,就是一个Guestbook 类型的对象 */ List list = (List) qr.query(sql, new BeanListHandler( Guestbook.class)); for(int i = 0;i<list.size();i ) { System.out.println(((Guestbook)list.get(i)).getName()); } // 用request.setAttribute()将结果传入到JSP页面中 request.setAttribute("list", list); } catch (NamingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } //转向forward值为guestbook.display对应的页面 return mapping.findForward("guestbook.display"); } }
十一、建立display.jsp文件
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <% String path = request.getContextPath(); String basePath = request.getScheme() "://" request.getServerName() ":" request.getServerPort() path "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>显示留言</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <!-- 用c:forEach循环输出在Action中setAttribute()方法传来的list中的值 --> <c:forEach items="${requestScope.list }" var="article"> <c:out value="${article.id}"></c:out> <c:out value="${article.name}"></c:out> <c:out value="${article.email}"></c:out> <c:out value="${article.url}"></c:out> <c:out value="${article.title}"></c:out> <c:out value="${article.content}"></c:out> <c:out value="${article.time}"></c:out> <br> <br> </c:forEach> <br> </body> </html>
十二、读取留言的结果
十三、在保存留言后自动进入读取留言页面
1、建立forward
2、struts-config.xml内容如下
已在struts-config.xml加入了以下代码
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans> <form-bean name="inputForm" type="org.apache.struts.validator.DynaValidatorForm"> <form-property name="title" type="java.lang.String" /> <form-property name="content" type="java.lang.String" /> <form-property name="url" type="java.lang.String" /> <form-property name="email" type="java.lang.String" /> <form-property name="name" type="java.lang.String" /> </form-bean> </form-beans> <global-exceptions /> <global-forwards> <forward name="guestbook.display" path="/form/display.jsp" /> <forward name="guestbook.read" path="/read.do" redirect="true" /> </global-forwards> <action-mappings> <action attribute="inputForm" input="/form/input.jsp" name="inputForm" path="/input" scope="request" type="com.meixin.struts.action.InputAction" /> <action name="readForm" path="/read" type="com.meixin.struts.action.ReadAction" /> </action-mappings> <message-resources parameter="com.meixin.struts.ApplicationResources" /> <!-- 加入Struts的一个插件,对表单进行验证,这里只能手动添加 --> <plug-in className="org.apache.struts.validator.ValidatorPlugIn"> <set-property property="pathnames" value="/org/apache/struts/validator/validator-rules.xml,/WEB-INF/validations.xml" /> </plug-in> </struts-config>
3、修改InputAction.java文件
//保存后自动转向到读取留言页面 return mapping.findForward("guestbook.read");