JavaBean和jsp的开发模型

1.实体类

 1 package com.zdsofe.javaBean.work;
 2 
 3 public class Student {
 4 
 5     public String name;
 6     public String sex;
 7     public int age;
 8     public String[] hobby;
 9     public String hobbys;
10     
11     public String[] getHobby() {
12         return hobby;
13     }
14     public void setHobby(String[] hobby) {
15         this.hobby = hobby;
16     }
17     public String getHobbys() {
18         String result="";
19         if(this.hobby.length>0)
20         {
21             
22             for(String str:hobby)
23             {
24                 result+=str+",";
25             }
26             
27         }
28         result=result.substring(0, result.length()-1);
29         hobbys = result;        
30         return hobbys;
31     }
32     public void setHobbys(String hobbys) {
33         this.hobbys = hobbys;
34     }
35     public String getName() {
36         return name;
37     }
38     public void setName(String name) {
39         this.name = name;
40     }
41     public String getSex() {
42         return sex;
43     }
44     public void setSex(String sex) {
45         this.sex = sex;
46     }
47     public int getAge() {
48         return age;
49     }
50     public void setAge(int age) {
51         this.age = age;
52     }
53     
54     
55     
56 }
View Code

2.jsp的动作元素

 1 <%@page import="com.zdsofe.javaBean.work.Student"%>
 2 <%@ page language="java" contentType="text/html; charset=UTF-8"
 3     pageEncoding="UTF-8"%>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 
12 <!--直接在脚本元素中使用  -->
13 <%/* 
14      String[] s={"游泳","爬山","跳舞"};
15      Student student=new Student();
16      student.setHobby(s);
17      out.print(student.getHobbys());  */
18 %>
19 
20 <!-- 利用jsp的动作元素来使用 -->
21 <% request.setCharacterEncoding("utf-8"); %>
22 <!-- 创建实体类 -->
23     <jsp:useBean id="user" class="com.zdsofe.javaBean.work.Student"></jsp:useBean>
24     <!--  调用set方法-->
25     <jsp:setProperty property="*" name="user"/>
26     
27     <%-- <jsp:setProperty property="age" name="user" value="34"/>
28     <jsp:getProperty property="age" name="user"/> --%>
29     
30     <!-- 调用get方法 -->
31     <jsp:getProperty property="name" name="user" /><br/>
32     <jsp:getProperty property="sex" name="user"/><br/>
33     <jsp:getProperty property="hobbys" name="user"/><br/>
34     
35 </body>
36 </html>
View Code

3.登录界面

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <form action="01.jsp" method="post">
11 用户名:<input type="text" name="name"/><br/>
12 性别:<input type="radio" name="sex" value="男"/>13      <input type="radio" name="sex"/ value="女">女<br/>
14 爱好: <input type="checkbox" name="hobby" value="游泳"/>游泳
15      <input type="checkbox" name="hobby" value="吃饭" />吃饭
16      <input type="checkbox" name="hobby" value="睡觉"/>睡觉<br/>
17      <button type="submit">提交</button>
18 </form>
19 
20 
21 </body>
22 </html>
View Code
原文地址:https://www.cnblogs.com/zclqian/p/7235628.html