4月18日 RIA 周三

mvc代码+注释

MVC设计模式
M 模型层 model
V 视图层 view
C 控制层 control
分层
1.控制层 servlet/Structs2/springMVC 控制页面跳转
2.业务逻辑层 service 编写业务逻辑
3.数据持久层 dao 连接数据库
作用:
1.便于进行分工
2.便于维护
3.提高软件的重用性
4.便于替换某种产品
效果图
这里写图片描述
代码
list.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'list.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
  <center>
  <table border="1px">
        <tr>
            <th>id</th>
            <th>name</th>
            <th>age</th>
            <th>sex</th>
        </tr>
    <c:forEach var="li" items="${list00}">
        <tr>
            <th>${li.id}</th>
            <th>${li.name}</th>
            <th>${li.age}</th>
            <th>${li.sex}</th>
        </tr>
    </c:forEach>
  </table>
  </center>
  </body>
</html>

User

package com.www.entity;

public class User {
    int id;
    String name;
    String age;
    String sex;
    //构造方法
    public User(int id, String name, String age, String sex) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
    //get and set
    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 getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }



}

UserDao

package com.www.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.www.entity.User;
//数据持久层dao (用于连接数据库)
public class UserDao {
    //JDBC查询类
    //返回值为      集合  泛型User类
    public List<User> listUser() throws Exception{//将JDBC的异常抛出给调用它的上一级
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql:///login", "root", "root");
        PreparedStatement pst = con.prepareStatement("select * from user04");
        ResultSet rs = pst.executeQuery();
        //创建集合 list对象
        ArrayList<User> list = new ArrayList();
        //将User对象加入集合list对象中
        while(rs.next()){
            User u = new User(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4));
            list.add(u);
        }
        rs.close();
        pst.close();
        con.close();
        //返回list对象
        return list;//传送集合类型的值
    }
}

UserService

package com.www.service;

import java.util.List;

import com.www.dao.UserDao;
import com.www.entity.User;
//业务逻辑层service(编写业务逻辑)调用数据持久层dao
public class UserService {
    //创建UserDao类的对象dao
    UserDao dao = new UserDao();
    public List<User> showUser() throws Exception{//继续抛出异常给调用它的上一级
        //调用dao对象中的listUser方法 并抛出异常
        //获取到数据库里面的集合类型返回值list进行传值
        //从数据持久层里面取到集合类型的返回值
        List<User> list = dao.listUser();
        return list;//返回一个对象(传值)集合类型
    }
}

UserServlet

package com.www.servlet;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.www.entity.User;
import com.www.service.UserService;
//控制层servlet(控制页面跳转) 调用业务逻辑层
public class UserServlet extends HttpServlet {
    //调用UserService创建service对象
    UserService service = new UserService();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //接收网页数据并判断跳转对应的方法
        String m = request.getParameter("m");
        if(m.equals("listUser")){
            listUser(request,response);
        }
    }
    private void listUser(HttpServletRequest request,
            HttpServletResponse response) {
        // TODO Auto-generated method stub
        try {
            //从业务逻辑层取到集合类型的返回值
            List<User> list = service.showUser();
            //将集合类型的值进行转发 在页面进行展示
            request.setAttribute("list00", list);
            request.getRequestDispatcher("list.jsp").forward(request, response);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

面具戴太久,就会长到脸上,再想揭下来,除非伤筋动骨扒皮。
——鲁迅

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值