jsp 結合使用jstl 和 javabean

來源:互聯網
上載者:User

網上看到的javabean的代碼都比較簡單只是返回簡單的String,但是實際使用中有時候可能會有需要一次返回多條資料的情況,下面例子就是使用List返回多條資料的例子,以此類推像dataset,table之類的資料應該也是可以這樣得到的

javabean的代碼,為了方便直接寫的固定資料firstname

package com.myapp.struts;import java.beans.*;import java.io.Serializable;import java.util.LinkedList;import java.util.List;public class NewBean implements Serializable {    public static final String PROP_SAMPLE_PROPERTY = "sampleProperty";    private List<String> firstname;    private String sampleProperty;        private PropertyChangeSupport propertySupport;        public NewBean() {        propertySupport = new PropertyChangeSupport(this);    }        public String getSampleProperty() {        return sampleProperty;    }        public void setSampleProperty(String value) {        String oldValue = sampleProperty;        sampleProperty = value;        propertySupport.firePropertyChange(PROP_SAMPLE_PROPERTY, oldValue, sampleProperty);    }        public void addPropertyChangeListener(PropertyChangeListener listener) {        propertySupport.addPropertyChangeListener(listener);    }        public void removePropertyChangeListener(PropertyChangeListener listener) {        propertySupport.removePropertyChangeListener(listener);    }    public List<String> getFirstname() {        List<String> l = new LinkedList<String>();        l.add("1");        l.add("2");        return l;    }    public void setFirstname(List<String> firstname) {        this.firstname = firstname;    }    }
使用javabean的jsp頁面,在寫class的時候開始寫的是NewBean總是找不到這個類,後來改成包含命名空間的全名才可以

<%@page import="com.myapp.struts.NewBean"%><%@page contentType="text/html" pageEncoding="UTF-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><!DOCTYPE html><html>    <head>        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">        <title>JSP Page</title>    </head>    <body>        <jsp:useBean id="test1" class="com.myapp.struts.NewBean" scope="page"/>        <c:forEach var="student" items="${test1.firstname}">            <p>                ${student}            </p>        </c:forEach>                <h1>Hello World!</h1>    </body></html>




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.