1. 程式人生 > >第21講 struts2值棧OGNL訪問JavaBean,List集合 Map物件

第21講 struts2值棧OGNL訪問JavaBean,List集合 Map物件

一,OGNL訪問JavaBean 1在專案HeadFirstStruts2chapter04 ,新建com.cruise.model包,Student類,name age屬性,全參構造和無參構造,package com.cruise.model;public class Student {     private String name;     private String age;     public String getName() {        return name;     }     public void setName(String name) {        this

.name = name;     }     public String getAge() {        return age;     }     public void setAge(String age) {        this.age = age;     }     public Student() {        super();        // TODO Auto-generated constructor stub     }     public Student(String name, String age) {        super();        this
.name = name;        this.age = age;     } }2HelloWorldAction中,定義JavaBean,生成get() set()方法,execute()方法中, new Student("小八","21"),package com.cruise.action;import java.util.Map;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;import com.cruise.model.Student;import
 com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.util.ValueStack;public class HelloAction extends ActionSupport{          private Student student;          public Student getStudent() {        return student;     }     public void setStudent(Student student) {        this.student = student;     }     @Override     public String execute() throws Exception {                student= new Student("小八","21");        return "success";     } }3success.jsp 檔案,訪問JavaBean物件,"student.name"這裡的student與HelloAction中的屬性名一直 <%@ page language="java" contentType="text/html; charset=UTF-8"     pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags" %> 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>Insert title heretitle> head> <body>ognl訪問JavaBean的資料:<s:property value="student.name"/><s:property value="student.age"/><br> body> html>struts.xml檔案如下 xml version="1.0" encoding="UTF-8" ?> DOCTYPE struts PUBLIC     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"     "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="manage" namespace="/" extends="struts-default">     <action name="hello" class="com.cruise.action.HelloAction" >        <result name="success" >success.jspresult>     action> package> struts>4測試:這個案例已經在19講用到了, http://localhost:8080/HeadFirstStruts2chapter04/hello二,OGNL訪問List集合 2HelloWorldAction中,定義student的List集合,生成get() set()方法,execute()方法中,例項ArrayList package com.cruise.action;import java.util.ArrayList;import java.util.List;import com.cruise.model.Student;import com.opensymphony.xwork2.ActionSupport;public class HelloAction extends ActionSupport{          private Student student;     private List students;          public Student getStudent() {        return student;     }     public void setStudent(Student student) {        this.student = student;     }          public List getStudents() {        return students;     }     public void setStudents(List students) {        this.students = students;     }               @Override     public String execute() throws Exception {                student= new Student("小八","21");        students = new ArrayList();        students.add(new Student("ashely","23"));        students.add(new Student("jack","45"));        return "success";     } }3success.jsp 檔案,訪問JavaBean物件 <%@ page language="java" contentType="text/html; charset=UTF-8"     pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags" %> 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>Insert title heretitle> head> <body>ognl訪問JavaBean的資料:<s:property value="student.name"/><s:property value="student.age"/><br>ognl訪問List集合的資料:<s:property value="students[0].name"/><s:property value="students[0].age"/><br>ognl訪問Map集合的資料:<s:property value="students[1].name"/><s:property value="students[1].age"/><br> body> html>struts.xml同上 4測試:在第8講中,通過這種方式給students集合賦值 三,OGNL訪問Map集合 2HelloWorldAction中,execute()方法中,定義Map studentMap;get() set()方法package com.cruise.action;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import com.cruise.model.Student;import com.opensymphony.xwork2.ActionSupport;public class HelloAction extends ActionSupport{          private Student student;     private List students;     private Map studentMap;          public Student getStudent() {        return student;     }     public void setStudent(Student student) {        this.student = student;     }          public List getStudents() {        return students;     }     public void setStudents(List students) {        this.students = students;     }     public Map getStudentMap() {        return studentMap;     }     public void setStudentMap(Map studentMap) {        this.studentMap = studentMap;     }     @Override     public String execute() throws Exception {                student= new Student("小八","21");                students = new ArrayList();        students.add(new Student("ashely","23"));        students.add(new Student("jack","45"));                studentMap = new HashMap();        studentMap.put("goodStudent", new Student("cruise","26"));        studentMap.put("badStudent", new Student("Tom","23"));        return "success";     } }3success.jsp 檔案,訪問JavaBean物件 <%@ page language="java" contentType="text/html; charset=UTF-8"     pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags" %> 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>Insert title heretitle> head> <body>ognl訪問JavaBean的資料:<s:property value="student.name"/><s:property value="student.age"/><br>ognl訪問List集合的資料:<s:property value="students[0].name"/><s:property value="students[0].age"/><br>ognl訪問List集合的資料:<s:property value="students[1].name"/><s:property value="students[1].age"/><br>ognl訪問Map集合的資料:<s:property value="studentMap['goodStudent'].name"/><s:propertyvalue="studentMap['goodStudent'].age"/><br>ognl訪問Map集合的資料:<s:property value="studentMap['badStudent'].name"/><s:propertyvalue="studentMap['badStudent'].age"/><br> body> html>struts.xml同上 4測試 http://localhost:8080/HeadFirstStruts2chapter04/hello