JavaBean和jsp的开发模型

Posted 初夏的一棵歪脖子树

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

以上是关于JavaBean和jsp的开发模型的主要内容,如果未能解决你的问题,请参考以下文章

java_第一年_JavaWeb(10)

Jsp获取Java的对象(JavaBean)

JSP规范_JavaBean

JavaBean技术

JSPModel

JavaBean简单及使用