2117847720qq

导航

12.24

【已解决】JavaScript---items="${arraylist}" var="student"无法获取到值,null错误
第一、

bean层

只写数据结构

1、定义成员变量,私有的属性

2、重写tostring方法

3、写带全部参数的构造方法

4、写无参构造方法

5、写get、set方法

注意#     变量名和方法名的拼写错误,检查再三!!!

第二、JDBCUtil工具类

静态定义 Connection

    url

    user

    password

1 package com.ycw.util;
2
3 import java.sql.DriverManager;
4 import java.sql.PreparedStatement;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7 import com.mysql.jdbc.Connection;
8
9 public class JDBCUtil {
10 private static Connection conn;
11 private static String url="jdbc:mysql://localhost:3306/info?useUnicode=true&CharacterEncoding=utf-8";
12 private static String user="root";
13 private static String password="000429";
14 static {
15 try {
16 Class.forName("com.mysql.jdbc.Driver");
17 } catch (ClassNotFoundException e) {
18 // TODO 自动生成的 catch 块
19 e.printStackTrace();
20 }
21 }
22 public static Connection getConn() {
23 //获得连接
24 try {
25 conn = (Connection) DriverManager.getConnection(url,user,password);
26 if(conn!=null)
27 System.out.println("数据库连接成功!");
28 } catch (SQLException e) {
29 // TODO 自动生成的 catch 块
30 e.printStackTrace();
31 }
32 return conn;
33 }
34 public static void drop(Connection conn,PreparedStatement ps)
35 {
36 if(ps!=null) {
37 try {
38 ps.close();
39 System.out.println("ps关闭!");
40 } catch (SQLException e) {
41 // TODO 自动生成的 catch 块
42 e.printStackTrace();
43 }
44 }
45 if(conn!=null) {
46 try {
47 conn.close();
48 System.out.println("conn关闭!");
49 } catch (SQLException e) {
50 // TODO 自动生成的 catch 块
51 e.printStackTrace();
52 }
53 }
54 }
55 public static void drop(Connection conn , PreparedStatement ps , ResultSet rs) {
56 if(rs!=null) {
57 try {
58 rs.close();
59 System.out.println("rs关闭!");
60 } catch (SQLException e) {
61 // TODO 自动生成的 catch 块
62 e.printStackTrace();
63 }
64 }
65 if(ps!=null) {
66 try {
67 ps.close();
68 System.out.println("ps关闭!");
69 } catch (SQLException e) {
70 // TODO 自动生成的 catch 块
71 e.printStackTrace();
72 }
73 }
74 if(conn!=null) {
75 try {
76 conn.close();
77 System.out.println("conn关闭!");
78 } catch (SQLException e) {
79 // TODO 自动生成的 catch 块
80 e.printStackTrace();
81 }
82 }
83 }
84 }

第三、dao层

1、类体静态定义

注意导包问题:

导包都是    java.sql.ResultSet

        java.sql.PreparedStatement

1 1   private static Connection conn;
2 2 private static PreparedStatement ps;
3 3 private static ResultSet rs;
4 4 private static StudentEnity student;
第四、servlet层

1、设置字符集

1      request.setCharacterEncoding("UTF-8");
2 response.setCharacterEncoding("UTF-8");
3 response.setContentType("application/json;charset=utf-8");
第五、页面层

注意#

doget    处理href请求

href="${pageContext.request.contextPath}/servlet名"

e.g.      

显示全部信息

JSTL的使用:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

1 <c:forEach items="${arraylist}" var="student">
2
3 ${student.studentname}
4 ${student.studentid}
5 ${student.studentclass }
6 ${student.studentscore }
7 ${student.studentdate }
8
9 </c:forEach>

注意#

items 属性 = “${}”     在{} 里面的值和括号之间不能有空格,否则识别失败!

${类名.属性名}

这里实质上是调用了类的get方法,不是直接获取,是间接获取,所以成员变量依然定义成private,但是方法必须是公有的!

posted on 2024-12-24 15:53  我爱玩原神(原神大王)  阅读(8)  评论(0)    收藏  举报