从开始接触web到现在学习这一块已经接近尾声经历了大半个月的学习,回想一下,有MySQL . JDBC . HTML . JAVASCRIPT .CSS . BOOSTRAP . XML . TOMCAT .SERVLET . COOKIE . SEESION,然后现在综合以上的学习内容,我们就在实体操作,进行案例的练习。在这段日子中,我们常常会遇到各种bug,比如什么exception,404,500,特别是在jsp中,由于HTML本身语法不严谨,导致有代码编写写错也不会报错,就我个人而言,我就把form标签中的form写成from,最后一个个找错,很恼火!
昨天,我敲了一下案例的代码,需求是通过查看用户信息这个超链接,展示一个用户表格的数据。而我昨天编写就出了这么个bug:ClassNotFoundException,排查了两个小时,最后发现在细节上出了问题,木有在WEB-INF目录下创一个lib文件夹,而是把资源jar包直接导在WEB-INF 底下,实在太同意了,所以以后写项目一定要细心,按标准格式规范写。最后展示一下我的代码:
package cn.itcast.domain;
public class User {
private int id;
private String name;
private String gender;
private int age;
private String address;
private String qq;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
", address='" + address + '\'' +
", qq='" + qq + '\'' +
", email='" + email + '\'' +
'}';
}
}
package cn.itcast.web.servlet;
import cn.itcast.domain.User;
import cn.itcast.service.ServiceUser;
import cn.itcast.service.impl.ServiceUserImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/servletUserList")
public class ServletUserList extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//用于解决请求乱码问题
request.setCharacterEncoding("utf-8");
ServiceUser service = new ServiceUserImpl();
List<User> users = service.findAll();
request.setAttribute("users",users);
request.getRequestDispatcher("/list.jsp").forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<!-- 网页使用的语言 -->
<html lang="zh-CN">
<head>
<!-- 指定字符集 -->
<meta charset="utf-8">
<!-- 使用Edge最新的浏览器的渲染方式 -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!-- viewport视口:网页可以根据设置的宽度自动进行适配,在浏览器的内部虚拟一个容器,容器的宽度与设备的宽度相同。
width: 默认宽度与设备的宽度相同
initial-scale: 初始的缩放比,为1:1 -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<title>用户信息管理系统</title>
<!-- 1. 导入CSS的全局样式 -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- 2. jQuery导入,建议使用1.9以上的版本 -->
<script src="js/jquery-2.1.0.min.js"></script>
<!-- 3. 导入bootstrap的js文件 -->
<script src="js/bootstrap.min.js"></script>
<style type="text/css">
td, th {
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<h3 style="text-align: center">用户信息列表</h3>
<table border="1" class="table table-bordered table-hover">
<tr class="success">
<th>编号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>籍贯</th>
<th>QQ</th>
<th>邮箱</th>
<th>操作</th>
</tr>
<c:forEach items="${users}" var="user" varStatus="s">
<tr>
<td>${s.count}</td>
<td>${user.name}</td>
<td>${user.gender}</td>
<td>${user.age}</td>
<td>${user.address}</td>
<td>${user.qq}</td>
<td>${user.email}</td>
<td><a class="btn btn-default btn-sm" href="update.html">修改</a> <a class="btn btn-default btn-sm" href="">删除</a></td>
</tr>
</c:forEach>
<tr>
<td colspan="8" align="center"><a class="btn btn-primary" href="add.html">添加联系人</a></td>
</tr>
</table>
</div>
</body>
</html>
public interface ServiceUser {
List<User> findAll();
}
public class ServiceUserImpl implements ServiceUser {
@Override
public List<User> findAll() {
List<User> users = new UserDaoImpl().findAll();
return users;
}
}
public interface UserDao {
List<User> findAll();
}
public class UserDaoImpl implements UserDao {
private JdbcTemplate template=new JdbcTemplate(JDBCUtils.getDataSource());
@Override
public List<User> findAll() {
String sql="select * from user";
List<User> list = template.query(sql, new BeanPropertyRowMapper<User>(User.class));
return list;
}
} |
|