jsp页面读取javabean中值的三种方式

先写一个javabean:
 1 public   class  UserBean  {
 2    private String username;
 3    private String pwd;
 4    public String getUsername() {
 5        return username;
 6    }

 7    public void setUsername(String username) {
 8        this.username = username;
 9    }

10    public String getPwd() {
11        return pwd;
12    }

13    public void setPwd(String pwd) {
14        this.pwd = pwd;
15    }
    
16}

再来一个提交表单的页面,文本框的name对应javabean中相应的属性:
 1 < form action = " consist.jsp "  method = " post " >
 2      < table >
 3          < tr >
 4              < td >< input type = " checkbox " /></ td >
 5              < td > 请输入用户名: </ td >
 6              < td >< input type = " text "  name = " username " ></ td >
 7          </ tr >
 8          < tr >
 9              < td >< input type = " checkbox " /></ td >
10              < td > 请输入密码: </ td >
11              < td >< input type = " password "  name = " password " ></ td >
12          </ tr >
13          < tr >
14              < td >< input type = " checkbox " /></ td >
15              < td colspan = " 2 " >< input type = " submit "  value = " 登录 " ></ td >
16          </ tr >
17      </ table >
18 </ form >

最后是在consist.jsp页面中设置并获取表单提交的相应的值:
 1 < jsp:useBean id = " User "   class = " mypack.UserBean "  scope = " request " ></ jsp:useBean >
 2
 3 <!--  设置javabean的值  -->
 4 < jsp:setProperty name = " User "  property = " username "  param = " username " />
 5 < jsp:setProperty name = " User "  property = " pwd "  param = " password " />< hr />
 6
 7 <!--  通过jsp标签  -->
 8 username: < jsp:getProperty property = " username "  name = " User " />< br />
 9 password: < jsp:getProperty property = " pwd "  name = " User " />< br />< hr />
10
11 <!--  通过java code,User即是jsp:userBean标签中的id的值  -->
12 username: <%= User.getUsername()  %>< br />
13 password: <%= User.getPwd()  %>< br />< hr />
14
15 <!--  通过EL表达式  -->
16 username:$ {User.username} < br />
17 password:$ {User["pwd"]} < br />< hr />


学的不够深入,还应该有其他的方式获取javabean的值,不过小弟现只知这三种,等发现了其他的方式,再来补充。若是“游侠”有其他方式,希望赐上代码啊!

你可能感兴趣的:(jsp页面读取javabean中值的三种方式)