第二篇——MVC初体验
1、bean类
package com.l.bean;
public class Product {
private int id;
private String name;
private String addr;
private double price;
public Product() {
super();
}
public Product(int id, String name, String addr, double price) {
this.id = id;
this.name = name;
this.addr = addr;
this.price = price;
}
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 getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Product [id=" + id + ", name=" + name + ", addr=" + addr + ", price=" + price + "]";
}
}
2、接口
public interface IProductDao {
List<Product> query();
}
3、操作数据库
public class ProductDaoImpl implements IProductDao {
private PreparedStatement ps;
@Override
public List<Product> query() {
String sql="select * from product";
List<Product> list=new ArrayList();
try {
ps=ConnectionDatabase.getConnection().prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while(rs.next()){
Product p=new Product();
p.setId(rs.getInt("id"));
p.setName(rs.getString("name"));
p.setAddr(rs.getString("addr"));
p.setPrice(rs.getDouble("price"));
list.add(p);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
}
4、工厂模式
public class DaoFactory {
public static IProductDao getProductDao() {
return new ProductDaoImpl();
}
}
5、测试
public class T {
@Test
public void t() {
IProductDao dao = DaoFactory.getProductDao();
List<Product> list = dao.query();
for(Product p:list) {
System.out.println(p);
}
}
}
6、事务处理
- 继承servelt后默认的代码
response.getWriter().append("Served at: ").append(request.getContextPath());//必须删掉此处不然会有乱码
private static final long serialVersionUID = 1L;//建议删掉,自行斟酌,序列化操作不多解释
@WebServlet("/Oprate")//此处通过注释配置servlet,我们把它删掉,因为要练习通过最基本的XML来配置
public class OpratorServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());//必须删掉此处不然会有乱码
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
- 删掉注释,精简代码
public class OpratorServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
- 编写代码
public class OperatorServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String op=request.getParameter("op");
if("query".equals(op)) {
queryService(request,response);
}
}
private void queryService(HttpServletRequest request, HttpServletResponse response) {
List<Product> list = DaoFactory.getProductDao().query();
try {
if (list != null) {
request.setAttribute("list", list);
request.getRequestDispatcher("product.jsp").forward(request, response);
} else {
response.sendRedirect("main.jsp");
}
} catch (Exception e) {
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
7、配制servlet及首页
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>productTest</display-name>
<welcome-file-list>
<welcome-file>main.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>OperatorServlet</servlet-name>
<servlet-class>com.l.servlet.OperatorServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>OperatorServlet</servlet-name>
<url-pattern>/OperatorServlet</url-pattern>
</servlet-mapping>
</web-app>
8、测试
- 建立首页
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>在此处插入标题</title>
</head>
<body>
main
</body>
</html>
- 启动项目查看是否跳转到首页
- 建立商品显示页面
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>在此处插入标题</title>
</head>
<body>
product
</body>
</html>
- 手动输入url查看是否跳转到prodect.jsp
http://localhost:8081/ProductTest/OperatorServlet?op=query
9、修改product.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@page import="com.l.bean.Product"%>
<%@ page import="java.util.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>在此处插入标题</title>
</head>
<body>
<center>
<table border="1">
<tr>
<th width="50">编号</th>
<th width="50">名称</th>
<th width="50">产地</th>
<th width="50">价格</th>
</tr>
<%
List<Product> list = (List) request.getAttribute("list");
for (Product p : list) {
%>
<tr>
<td><%=p.getId()%></td>
<td><%=p.getName()%></td>
<td><%=p.getAddr()%></td>
<td><%=p.getPrice()%></td>
<td></td>
</tr>
<%
}
%>
</table>
</center>
</body>
</html>
- 同样在搜索栏手动输入url
http://localhost:8081/ProductTest/OperatorServlet?op=query
10、修改首页
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>在此处插入标题</title>
</head>
<body>
<center>
<h2>
<a href="OperatorServlet?op=query" target="show">商品列表</a>
</h2>
<iframe name="show" height="800" width="800" frameborder="0"
scrolling="no"></iframe>
</center>
</body>
</html>
- 运行项目测试效果