日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

jsp:setProperty

發(fā)布時間:2023/12/20 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jsp:setProperty 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

類聲明:

package test;

public class Student {
?? ?private int age;

?? ?public int getAge() {
?? ??? ?return age;
?? ?}

?? ?public void setAge(int age) {
?? ??? ?this.age = age;
?? ?}
}
jsp代碼:? ??

<jsp:useBean id="student" scope="session" class="test.Student"></jsp:useBean>
<jsp:setProperty property="age" name="student"? value="12"/>
?<jsp:setProperty property="age" name="student"? param="12"/>? 此處12只是一個參數(shù)名字
?<jsp:getProperty property="age" name="student"/>


=》12

源碼如下:

?

????? test.Student student = null;
????? synchronized (session) {
??????? student = (test.Student) _jspx_page_context.getAttribute("student", PageContext.SESSION_SCOPE);
??????? if (student == null){
????????? student = new test.Student();
????????? _jspx_page_context.setAttribute("student", student, PageContext.SESSION_SCOPE);
??????? }
????? }
????? out.write("\r\n");
????? out.write("? \t");
????? org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper(_jspx_page_context.findAttribute("student"), "age", "12", null, null, false);
????? out.write("\r\n");
????? out.write("? \t");
????? org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper(_jspx_page_context.findAttribute("student"), "age", request.getParameter("12"), request, "12", false);
????? out.write("\r\n");
????? out.write("? \t");
????? out.write(org.apache.jasper.runtime.JspRuntimeLibrary.toString((((test.Student)_jspx_page_context.findAttribute("student")).getAge())));

?

解釋:

1.

<jsp:useBean id="student" scope="request" class="test.Student"></jsp:useBean>

<jsp:setProperty property="age" name="student"? value="12"/>必須和<jsp:useBean配套使用

2.在useBean中聲明要放在哪個useBean范圍內(nèi):page,request,session,application,然后指定id也就是屬性名和class也就是屬性類型。

3.利用setProperty往useBean聲明的變量中放置值,property為屬性的變量名,name為useBean中聲明的id名這兩者必須相同,因為是將值設置到id指定的屬性中去,value直接設置屬性值,param則是接受傳遞過來的參數(shù)值設置到屬性中,如request.getParameter。

param例子:

test2.jsp:

<jsp:forward page="test3.jsp">
<jsp:param value="10" name="age"/>
</jsp:forward>

test3.jsp:

<jsp:useBean id="student" scope="request" class="test.Student"></jsp:useBean>
<jsp:setProperty property="age" name="student" param="age"/>
<jsp:getProperty property="age" name="student"/>


結果為:10


4.通過getProperty從id中取直,property指定屬性名,那么指定從哪個id中取值。

注意:修改scope范圍生成的servlet中的源代碼只有以下部分發(fā)生改變

? synchronized (session) {
??????? student = (test.Student) _jspx_page_context.getAttribute("student", PageContext.SESSION_SCOPE);
??????? if (student == null){
????????? student = new test.Student();
????????? _jspx_page_context.setAttribute("student", student, PageContext.SESSION_SCOPE);
??????? }
????? }

此代碼先判斷在session中是否存在student對象不存在創(chuàng)建一個放入session中,如果存在則不創(chuàng)建。之后設置值時就是往該對象中放置。



?

轉載于:https://www.cnblogs.com/pangblog/p/3292229.html

總結

以上是生活随笔為你收集整理的jsp:setProperty的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。