togeth 2013-07-10
第五个版本,我们测试显示多种从Action传到页面的对象,包括String,Bean,Map,List<String>,List<Bean>等等
这里主要包括Action中存储和jsp中显示
LoginAction.java
package com.coderdream.action; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Vector; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; 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.coderdream.db.StudentDao; import com.coderdream.db.UserDao; import com.coderdream.form.LoginForm; import com.coderdream.vo.StudentView; import com.coderdream.vo.UserView; public class LoginAction extends Action { /** * 处理客户端请求 */ @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // 得到客户的的提交数据 LoginForm lf = (LoginForm) form; UserDao userDao = new UserDao(); String userName = lf.getUserName(); String password = lf.getPassword(); UserView userView = new UserView(); userView.setUserName(userName); userView.setPassword(password); int result = userDao.queryUser(userName, password); // 业务调用 if (1 <= result) { StudentDao studentDao = new StudentDao(); List<StudentView> studentList = studentDao.quertAllStudent(); request.setAttribute("studentList", studentList); request.setAttribute("userView", userView); request.setAttribute("userName", userName); // 存放String的Map Map<String, String> stringMap = new HashMap<String, String>(); stringMap.put("Jan", "January"); stringMap.put("Feb", "February"); stringMap.put("Mar", "March"); stringMap.put("Apr", "April"); request.setAttribute("stringMap", stringMap); // 存放String和String数组的Map Map<String, String[]> stringMap2 = new HashMap<String, String[]>(); String vegetables[] = { "pepper", "cucumber" }; String fruits[] = { "apple", "orange", "banana", "cherry", "watermelon" }; String flowers[] = { "chrysanthemum", "rose" }; String trees[] = { "willow" }; stringMap2.put("Vegetables", vegetables); stringMap2.put("Fruits", fruits); stringMap2.put("Flowers", flowers); stringMap2.put("Trees", trees); request.setAttribute("stringMap2", stringMap2); // Vector<String> animals = new Vector<String>(); animals.addElement("Dog"); animals.addElement("Cat"); animals.addElement("Bird"); animals.addElement("Chick"); request.setAttribute("animals", animals); // 存放String的List List<String> stringList = new ArrayList<String>(); stringList.add("abc"); stringList.add("edf"); stringList.add("ghi"); stringList.add("jkl"); request.setAttribute("stringList", stringList); // 用户名密码验证成功,跳转到成功页面 return mapping.findForward("success"); } else { // 用户名密码错误,跳转到失败页面 return mapping.findForward("failing"); } } }
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%> <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html:html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登录成功</title> </head> <body> 用户登录成功! <br> 对象方式: <logic:present name="userView" scope="request"> <bean:write name="userView" property="userName" /> </logic:present> <br> <br> 属性方式: <bean:write name="userName" scope="request" /> <hr> <logic:iterate id="element" indexId="ind" name="stringMap"> <bean:write name="ind" /> <bean:write name="element" property="key" /> <bean:write name="element" property="value" /> <br> </logic:iterate> <hr> <logic:iterate id="element" indexId="ind" name="stringMap2"> <bean:write name="ind" /> <bean:write name="element" property="key" /> <br> <logic:iterate id="elementValue" name="element" property="value" length="3" offset="1"> ------<bean:write name="elementValue" /> <br> </logic:iterate> </logic:iterate> <hr> <logic:iterate id="element" name="animals"> <bean:write name="element" /> <br> </logic:iterate> <hr> <logic:iterate id="element" indexId="index" name="animals" offset="1" length="2"> <bean:write name="index" />.<bean:write name="element" /> <br> </logic:iterate> <hr> <logic:iterate id="element" indexId="index" name="stringList"> <bean:write name="index" />.<bean:write name="element" /> <br> </logic:iterate> <hr> <logic:iterate id="studentView" indexId="index" name="studentList"> <bean:write name="studentView" property="no" />. <bean:write name="studentView" property="name" />. <bean:write name="studentView" property="sex" />. <bean:write name="studentView" property="age" />. <bean:write name="studentView" property="dept" /> <br> </logic:iterate> </body> </html:html>
代码说明
这里主要看一下List<Bean>的代码,很多书上都没有!
运行结果
另外附上孙卫琴的第二章的Demo源代码。
java的内存管理就是对象的分配和释放问题。但同时,它也加重了JVM的工作。因为,GC为了能够正确释放对象,GC必须监控每一个对象的运行状态,包括对象的申请、引用、被引用、赋值等,GC都需要进行监控。