javaBean

javaBean

规范类的操作由set和get来

先实现一个这样的Peaple(我这里写错了)类

package kooing;

public class Peaple {
    private String name;
    private int age;
    private String sex;
    private boolean secret;
    public int getAge(){
        return age;
    }
    public String getName(){
        return name;
    }
    public String getSex(){
        return sex;
    }
    public boolean getSecret(){
        return secret;
    }
    public void setAge(int a){
        age=a;
    }
    public void setName(String a){
        name=a;
    }
    public void setSex(String a){
        sex=a;
    }
    public void setSecret(boolean a){
        secret=a;
    }
}
View Code

然后又html提交数据

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <div align ="center">
            <form action="useBean.jsp" method="post">
                <fieldset style='width=300'>
                    <legend>请填写 Person信息</legend>
                    <table align="center" width="400">
                        <tr>
                            <td align="right" style="font-weight:bold;">姓名:</td>
                            <td><input type="text" name="name" value="" style="200px;"/></td>
                        </tr>
                        <tr>
                            <td align="right" style="font-weight:bold;">年龄:</td>
                            <td><input type="text" name="age" value="" style="200px;"/></td>
                        </tr>
                        <tr>
                            <td align="right" style="font-weight:bold;">性别:</td>
                            <td>
                                <input type="radio" name="sex" value="male冯绍峰" >male121
                                <input type="radio" name="sex" value="female" >female
                            </td>
                        </tr>
                        <tr>
                            <td align="right" style="font-weight:bold;"></td>
                            <td>
                                <input type="submit" name="search" value="提交" class="button">
                            </td>
                        </tr>
                    </table>
                </fieldset>
            </form>
        </div>
    </body>
</html>
View Code

用的时候要声明

<jsp:useBean id="person1" class="kooing.Peaple" scope="page"/>

传送数据给类

<jsp:setProperty name="person1" property="*"/>
或者
<!--<jsp:setProperty name="person1" property="name" param="name"></jsp:setProperty>
<jsp:setProperty name="person1" property="name" value="age"></jsp:setProperty>
<jsp:setProperty name="person1" property="name" value="sex"></jsp:setProperty>

接收数据

      姓名: <%=person1.getName()%><br>
      年龄: <%=person1.getAge()%><br>
      性别: <%=person1.getSex()%><br>
      <hr>
      使用getProperty<br>
   姓名:<jsp:getProperty name="person1" property="name"/><br>
     年龄:  <jsp:getProperty name="person1" property="age"/><br>
      性别:  <jsp:getProperty name="person1" property="sex"/>

 在同一个页面的时候貌似不用传输过去,直接声明后使用

其实还有一个scope属性,来区分app和session的作用域(既是全站和个人的域(可以用来统计访问数

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<!DOCTYPE html>
<jsp:useBean id="session1" class="kooing.Counter" scope="session"></jsp:useBean>
<jsp:useBean id="app" class="kooing.Counter" scope="application"/>
<jsp:useBean id="page1" class="kooing.Counter" scope="page"/>
<jsp:useBean id="request1" class="kooing.Counter" scope="request" />

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        个人:<jsp:getProperty name="session1" property="counter"/>
        <hr/>
        一共:<jsp:getProperty name="app" property="counter"/>
        <hr />
        一共:<jsp:getProperty name="page1" property="counter"/>
        <hr />
        一共:<jsp:getProperty name="request1" property="counter"/>
        <hr />
     </body>
</html>
View Code
原文地址:https://www.cnblogs.com/vhyc/p/6425312.html