Java进阶知识31 SpringMVC+JDBC+Oracle 注解版整合实例

本文知识点(目录):

      1、本文整合实例最终效果图展示
      2、导读
          2.1、开发技术概述
          2.2、本项目使用的jar包、项目结构图
      3、本文所有代码(注解版)
          3.1、Oracle 数据库建表脚本
          3.2、web.xml 配置文件
          3.3、odb.properties 配置文件
          3.4、JDBC 封装工具类
          3.5、User 实体类
          3.6、dao 层
          3.7、service 层
          3.8、controller 控制层/器
          3.9、SpringMVC核心配置文件
          3.10、前端页面



1、本文整合实例最终效果图展示 

2、导读                    

2.1、开发技术概述

    a.本文使用的是Spring+JDBC框架,Oracle 11g,tomcat-7.0.96,JDK-1.8,MyEclipse10.6,采用了“注解版”的方式开发的;

    b.本文只实现了用户登录(含账号查询)、新增用户(用户注册)、查询所有用户信息、根据id查询;

    c.本项目所采用的编码格式都是 UTF-8;

    d.解决了向Oracle数据库中插入含有日期的字段,报错:“ORA-00917: 缺少逗号”;可看本文 UserDao 实现类的add()方法的代码;

    e.解决了web.xml配置文件中的编码过滤器,不能过滤get请求的问题;可看本文 UserAction 控制类的register()方法的代码;

    f.SpringMVC的核心配置文件,本整合实例都是放在src目录下。

2.2、本项目使用的jar包、项目结构图

    

3、本文所有代码(注解版)      

3.1、Oracle 数据库建表脚本(oracle.sql)

 1 -- 创建数据表 user1
 2 create table user1(
 3     id            number(6) primary key,
 4     name        varchar2(20),
 5     sex            smallint,  -- 短整型interger;或者设置成number(1)也行
 6     birthday    date,  -- date:日期 ,如:2020-2-29;   datetime:日期+时间,如:2020-2-29 15:10:35
 7     account     varchar2(20),
 8     password    varchar2(32),
 9     email       varchar2(20),
10     telphone    varchar2(20)
11 );
12 
13 -- 创建序列 user1_seq
14 create sequence user1_seq
15 minvalue 1  -- 最小值
16 maxvalue 9999  -- 最大值
17 start with 1   -- 从1开始计数
18 increment by 1  -- 每次加1
19 nocycle  -- 一直累加,不循环;cycle:达到最大值后,将从头开始累加
20 nocache;  -- 不建缓冲区。

注意:此处我只是创建了一个序列user1_seq,并没有创建触发器,故:插入数据时,要带上id;对应值user1_seq.nextval

3.2、web.xml 配置文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="3.0" 
 3     xmlns="http://java.sun.com/xml/ns/javaee" 
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 6     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 7   <display-name></display-name>    
 8   <welcome-file-list>
 9     <welcome-file>index.jsp</welcome-file>
10   </welcome-file-list>
11   
12   <!-- springmvc核心控制器 begin -->
13     <servlet>
14         <servlet-name>DispatcherServlet</servlet-name>
15         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
16         <init-param>
17             <param-name>contextConfigLocation</param-name>
18             <param-value>classpath:conf/springmvc.xml</param-value>
19             <!-- classpath: 等价于WEB-INF/classes/ -->
20         </init-param>
21     </servlet>
22     <servlet-mapping>
23         <servlet-name>DispatcherServlet</servlet-name>
24         <url-pattern>*.action</url-pattern>
25     </servlet-mapping>
26     <!-- springmvc核心控制器 end -->
27 
28     <!-- 编码过滤器  begin -->
29     <!-- 经过测试,这个编码过滤器,只能过滤post请求,get请求 还是出现乱码;本文UserAction类的register()方法中,有解决方法 -->
30     <filter>
31         <filter-name>CharacterEncodingFilter</filter-name>
32         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
33         <init-param>
34             <param-name>encoding</param-name>
35             <param-value>UTF-8</param-value>
36         </init-param>
37     </filter>
38     <filter-mapping>
39         <filter-name>CharacterEncodingFilter</filter-name>
40         <url-pattern>/*</url-pattern>
41     </filter-mapping>
42     <!-- 编码过滤器  end -->    
43 </web-app>

3.3、odb.properties 配置文件

1 jdbcDriver=oracle.jdbc.driver.OracleDriver
2 url=jdbc:oracle:thin:@localhost:1521:shoreid
3 userName=zhangsan
4 password=123456

3.4、JDBC 封装工具类(DBUtils.java)

  1 package com.shore.utils;
  2 
  3 import java.io.IOException;
  4 import java.io.InputStream;
  5 import java.sql.Connection;
  6 import java.sql.DriverManager;
  7 import java.sql.PreparedStatement;
  8 import java.sql.ResultSet;
  9 import java.sql.SQLException;
 10 import java.sql.Statement;
 11 import java.util.Properties;
 12 
 13 /**
 14  * @author DSHORE/2020-2-29
 15  * 连接数据库--->工具类:JDBC
 16  */
 17 public class DBUtils {
 18     
 19     private DBUtils() {
 20     }
 21 
 22     private static String jdbcDriver = "";
 23     private static String url = "";
 24     private static String userName = "";
 25     private static String password = "";
 26 
 27     static {
 28         Properties properties = new Properties();
 29         try {
 30             //读取配置文件(Oracle数据库)
 31             InputStream inputStream = DBUtils.class.getResourceAsStream("/odb.properties");
 32             properties.load(inputStream); //加载配置文件
 33             inputStream.close();//关闭输入流
 34             
 35             //获取到对应参数的值
 36             jdbcDriver = properties.getProperty("jdbcDriver");
 37             url = properties.getProperty("url");
 38             userName = properties.getProperty("userName");
 39             password = properties.getProperty("password");
 40         } catch (IOException e1) {
 41             e1.printStackTrace();
 42         } finally {
 43             try {
 44                 Class.forName(jdbcDriver);
 45             } catch (ClassNotFoundException e) {
 46                 throw new ExceptionInInitializerError(e);
 47             }
 48         }
 49     }
 50 
 51     //连接数据库
 52     public static Connection getConnection() throws SQLException {
 53         Connection connection = null;
 54         try {
 55             connection = DriverManager.getConnection(url, userName, password);
 56         } catch (Exception e) {
 57             System.out.println(e.getMessage());//如果出现异常,则 把异常信息打印到控台上
 58         }
 59         return connection;
 60     }
 61 
 62     // 释放资源。   顺序:resultSet、statement、connection
 63     public static void close(ResultSet resultSet, Statement statement, Connection connection) {
 64         if (resultSet != null) {
 65             try {
 66                 resultSet.close();
 67             } catch (SQLException e) {
 68                 e.printStackTrace();
 69             } finally {
 70                 if (statement != null) {
 71                     try {
 72                         statement.close();
 73                     } catch (SQLException e) {
 74                         e.printStackTrace();
 75                     } finally {
 76                         if (connection != null) {
 77                             try {
 78                                 connection.close();
 79                             } catch (SQLException e) {
 80                                 e.printStackTrace();
 81                             }
 82                         }
 83                     }
 84                 }
 85             }
 86         }
 87     }
 88 
 89     // 释放资源。  顺序:resultSet、preparedStatement、connection
 90     public static void close(ResultSet resultSet, PreparedStatement preparedStatement, Connection connection) {
 91         if (resultSet != null) {
 92             try {
 93                 resultSet.close();
 94             } catch (SQLException e) {
 95                 e.printStackTrace();
 96             } finally {
 97                 if (preparedStatement != null) {
 98                     try {
 99                         preparedStatement.close();
100                     } catch (SQLException e) {
101                         e.printStackTrace();
102                     } finally {
103                         if (connection != null) {
104                             try {
105                                 connection.close();
106                             } catch (SQLException e) {
107                                 e.printStackTrace();
108                             }
109                         }
110                     }
111                 }
112             }
113         }
114     }
115 }

3.5、User 实体类

 1 package com.shore.entity;
 2 
 3 import java.util.Date;
 4 
 5 /**
 6  * @author DSHORE/2020-2-29
 7  * 
 8  */
 9 public class User {
10     private Integer id;
11     private String name;
12     private Integer sex;
13     private Date birthday;
14     private String account;
15     private String password;
16     private String email;
17     private String telphone;
18 
19     public Integer getId() {
20         return id;
21     }
22 
23     public void setId(Integer id) {
24         this.id = id;
25     }
26 
27     public String getName() {
28         return name;
29     }
30 
31     public void setName(String name) {
32         this.name = name;
33     }
34 
35     public Integer getSex() {
36         return sex;
37     }
38 
39     public void setSex(Integer sex) {
40         this.sex = sex;
41     }
42 
43     public Date getBirthday() {
44         return birthday;
45     }
46 
47     public void setBirthday(Date birthday) {
48         this.birthday = birthday;
49     }
50 
51     public String getAccount() {
52         return account;
53     }
54 
55     public void setAccount(String account) {
56         this.account = account;
57     }
58 
59     public String getPassword() {
60         return password;
61     }
62 
63     public void setPassword(String password) {
64         this.password = password;
65     }
66 
67     public String getEmail() {
68         return email;
69     }
70 
71     public void setEmail(String email) {
72         this.email = email;
73     }
74 
75     public String getTelphone() {
76         return telphone;
77     }
78 
79     public void setTelphone(String telphone) {
80         this.telphone = telphone;
81     }
82 }

3.6、dao 层

接口 IUserDao

 1 package com.shore.dao;
 2 
 3 import java.util.List;
 4 
 5 import com.shore.entity.User;
 6 
 7 /**
 8  * @author DSHORE/2020-2-29
 9  *
10  */
11 public interface IUserDao {
12     
13     public void add(User user); //新增
14     
15     public List<User> listAll(); //查询所有
16 
17     public User findByAccount(String account); //根据账号查询
18 }

IUserDao 接口的实现类 UserDao

  1 package com.shore.dao.impl;
  2 
  3 import java.sql.Connection;
  4 import java.sql.PreparedStatement;
  5 import java.sql.ResultSet;
  6 import java.sql.SQLException;
  7 import java.sql.Statement;
  8 import java.text.SimpleDateFormat;
  9 import java.util.ArrayList;
 10 import java.util.List;
 11 
 12 import org.springframework.stereotype.Repository;
 13 
 14 import com.shore.dao.IUserDao;
 15 import com.shore.entity.User;
 16 import com.shore.utils.DBUtils;
 17 
 18 /**
 19  * @author DSHORE/2020-2-29
 20  *
 21  */
 22 @Repository
 23 public class UserDao implements IUserDao {
 24 
 25     //全局变量
 26     Connection connection = null;
 27     Statement statement = null;
 28     PreparedStatement preparedStatement = null;
 29     ResultSet resultSet = null;
 30     
 31     @Override //新增
 32     public void add(User user) {
 33         //日期转换成字符串
 34         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 35         String birthday = sdf.format(user.getBirthday());
 36         try {
 37             connection = DBUtils.getConnection();
 38             String sql = "insert into user1 values(user1_seq.nextval,'"
 39                                             +user.getName()+"'," 
 40                                             +user.getSex()+",to_date('"
 41                                             +birthday+"','yyyy-MM-dd'),'" 
 42                                             +user.getAccount()+"','"
 43                                             +user.getPassword()+"','"
 44                                             +user.getEmail()+"','"
 45                                             +user.getTelphone()+"')";
 46             //此处也可以用PreparedStatement,sql改一下即可
 47             statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
 48             statement.executeUpdate(sql);//执行添加操作
 49         } catch (Exception e) {
 50             e.printStackTrace();
 51         } finally{
 52             DBUtils.close(null, statement, connection);
 53         }
 54     }
 55 
 56     @Override //查询所有
 57     public List<User> listAll() {
 58         List<User> userList = new ArrayList<User>();
 59         String sql = "";
 60         try {
 61             connection = DBUtils.getConnection();
 62             sql = "select * from user1 order by id";
 63             statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
 64             resultSet = statement.executeQuery(sql);
 65             while (resultSet.next()) {
 66                 User user = new User();
 67                 user.setId(resultSet.getInt("id"));
 68                 user.setName(resultSet.getString("name"));
 69                 user.setSex(resultSet.getInt("sex"));
 70                 user.setBirthday(resultSet.getDate("birthday"));
 71                 user.setAccount(resultSet.getString("account"));
 72                 user.setPassword(resultSet.getString("password"));
 73                 user.setEmail(resultSet.getString("email"));
 74                 user.setTelphone(resultSet.getString("telphone"));
 75                 userList.add(user);
 76             }
 77         } catch (SQLException e) {
 78             e.printStackTrace();
 79         }finally {
 80             DBUtils.close(resultSet, statement, connection);
 81         }
 82         return userList;
 83     }
 84 
 85     @Override //根据账号查询
 86     public User findByAccount(String account) {
 87         String sql = null;
 88         User user = null;
 89         try {
 90             connection = DBUtils.getConnection();
 91             sql = "select * from user1 where account=?";
 92             preparedStatement = connection.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
 93             preparedStatement.setString(1, account);
 94             resultSet = preparedStatement.executeQuery();
 95             if (resultSet.first()) {
 96                 user = new User();
 97                 user.setId(resultSet.getInt("id"));
 98                 user.setName(resultSet.getString("name"));
 99                 user.setSex(resultSet.getInt("sex"));
100                 user.setBirthday(resultSet.getDate("birthday"));
101                 user.setAccount(resultSet.getString("account"));
102                 user.setPassword(resultSet.getString("password"));
103                 user.setEmail(resultSet.getString("email"));
104                 user.setTelphone(resultSet.getString("telphone"));
105             }
106         }catch (Exception e) {
107             e.printStackTrace();
108         }finally{
109             DBUtils.close(resultSet, preparedStatement, connection);
110         }
111         return user;
112     }
113 }

3.7、service 层

IUserService 接口

 1 package com.shore.service;
 2 
 3 import java.util.List;
 4 
 5 import com.shore.entity.User;
 6 
 7 /**
 8  * @author DSHORE/2020-2-29
 9  *
10  */
11 public interface IUserService {
12     
13     public void add(User user); //新增
14     
15     public List<User> listAll(); //查询所有
16 
17     public User findByAccount(String account);//根据账号查询
18 }

IUserService 接口的实现类 UserService

 1 package com.shore.service.impl;
 2 
 3 import java.util.List;
 4 
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Service;
 7 
 8 import com.shore.dao.IUserDao;
 9 import com.shore.dao.impl.UserDao;
10 import com.shore.entity.User;
11 import com.shore.service.IUserService;
12 
13 /**
14  * @author DSHORE/2020-2-29
15  *
16  */
17 @Service
18 public class UserService implements IUserService {
19     
20     @Autowired
21     private IUserDao userDao = new UserDao();
22 
23     @Override //新增
24     public void add(User user) {
25         userDao.add(user);
26     }
27 
28     @Override //查询所有
29     public List<User> listAll() {
30         return userDao.listAll();
31     }
32 
33     @Override //根据账号查询
34     public User findByAccount(String account) {
35         return userDao.findByAccount(account);
36     }
37 }

3.8、controller 控制层/器(UserAction)

 1 package com.shore.controller;
 2 
 3 import java.text.SimpleDateFormat;
 4 import java.util.Date;
 5 import java.util.List;
 6 
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 
10 import org.springframework.beans.propertyeditors.CustomDateEditor;
11 import org.springframework.stereotype.Controller;
12 import org.springframework.web.bind.ServletRequestDataBinder;
13 import org.springframework.web.bind.annotation.InitBinder;
14 import org.springframework.web.bind.annotation.RequestMapping;
15 import org.springframework.web.bind.annotation.RequestMethod;
16 
17 import com.shore.entity.User;
18 import com.shore.service.IUserService;
19 import com.shore.service.impl.UserService;
20 
21 /**
22  * @author DSHORE/2020-2-29
23  * 控制器:UserAction;有的人喜欢这样写:UserController
24  */
25 @Controller
26 @RequestMapping(value="/user")
27 public class UserAction {
28     private IUserService userService = new UserService();
29 
30     @InitBinder //转换时间类型(自动)
31     protected void initBinder(HttpServletRequest request,
32             ServletRequestDataBinder binder) throws Exception {
33         binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true)); //true:允许输入空值,false:不能为空值
34     }
35     
36     //用户登录
37     @RequestMapping(value="/login",method=RequestMethod.POST)
38     public void login(HttpServletRequest request,HttpServletResponse response) throws Exception{
39         String account = request.getParameter("account").trim();
40         String password = request.getParameter("password").trim();
41         // 根据用户账号名称查询User对象
42         User dbUser = userService.findByAccount(account);
43         if (dbUser == null) { // 用户不存在
44             request.getSession().setAttribute("message", "账号不存在,请重新输入!");
45             response.sendRedirect(request.getContextPath());//重定向到登录页面
46         } else if (!password.equals(dbUser.getPassword())) {
47             request.getSession().setAttribute("message", "密码错误,请重新输入!");
48             response.sendRedirect(request.getContextPath());
49         }else {
50             // 把该用户(数据库中查到的)保存到session中
51             request.getSession().setAttribute("currentUser", dbUser);
52             //重定向到本UserAction类的listAll方法
53             response.sendRedirect(request.getContextPath()+"/user/listAll.action");
54         }
55     }
56     
57     //查询所有用户详细信息
58     @RequestMapping(value="/listAll",method=RequestMethod.GET)
59     public String listAll(HttpServletRequest request) throws Exception {
60         List<User> users = userService.listAll();
61         request.setAttribute("users", users);
62         request.getSession().removeAttribute("message");//登录成功后,移除session中的message提示信息
63         return "user-list";//跳转到user-list.jsp页面
64     }
65         
66     //注册账号(新增用户)
67     @RequestMapping(value="/register",method=RequestMethod.GET)
68     public void register(User user,HttpServletRequest request,
69             HttpServletResponse response) throws Exception{
70         //get方式,防止乱码
71         String userName = new String(request.getParameter("name").getBytes("ISO-8859-1"),"utf-8");
72         user.setName(userName);
73         //调用service进行注册(保存)
74         userService.add(user);
75         //重定向到本UserAction类的listAll方法
76         response.sendRedirect(request.getContextPath()+"/user/listAll.action");
77     }
78 }

3.9、SpringMVC核心配置文件(springmvc.xml)

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:mvc="http://www.springframework.org/schema/mvc"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xmlns:aop="http://www.springframework.org/schema/aop"
 6     xmlns:tx="http://www.springframework.org/schema/tx"
 7     xmlns:context="http://www.springframework.org/schema/context"
 8     xsi:schemaLocation="
 9        http://www.springframework.org/schema/beans
10        http://www.springframework.org/schema/beans/spring-beans.xsd
11        http://www.springframework.org/schema/mvc
12        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
13        http://www.springframework.org/schema/context
14        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
15     
16     <!-- 1、映射器(可选:可写,可不写,如果一条连接有多个入口,则需要配置) -->
17     <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
18     
19     <!-- 2、适配器(可选) -->
20     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
21     
22     <!-- 3、控制器 -->
23     <context:component-scan base-package="com.shore.controller"/>
24     
25     <!-- 4、视图解析器(可选,如果写了视图逻辑名,就必须要配置,下面配置了两个property) -->
26     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
27         <property name="prefix" value="/jsp/user/"></property>
28         <property name="suffix" value=".jsp"></property>
29     </bean>
30 </beans>

3.10、前端页面

index.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP 'index.jsp' starting page</title>
13     <meta http-equiv="pragma" content="no-cache">
14     <meta http-equiv="cache-control" content="no-cache">
15     <meta http-equiv="expires" content="0">    
16     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
17     <meta http-equiv="description" content="This is my page">
18     <!--
19     <link rel="stylesheet" type="text/css" href="styles.css">
20     -->
21   </head>
22   
23   <body>
24       <jsp:forward page="jsp/user/login.jsp"></jsp:forward>
25   </body>
26 </html>

login.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP 'login.jsp' starting page</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22 
23   </head>
24   
25  <style> 
26     table tr th{ border:1px solid #C1C1C1; font-size: 16px;}
27     table,table tr td { border:1px solid #C1C1C1; }
28     table { width: 30%; min-height: 25px; line-height: 25px; border-collapse: collapse; padding:2px; margin:auto;}
29     a{text-decoration: none;font-weight: bold;}
30   </style> 
31   
32   <body>
33        <form name="form" action="user/login.action" method="post">
34            <table style="align: center;">
35                <tr>
36                    <td style="text-align: center;">账号:</td>
37                    <td>
38                        <input type="text" name="account" />
39                        <span style="color: red">${message}</span> 
40                    </td>
41                </tr>
42                <tr>
43                    <td style="text-align: center;">密码:</td>
44                    <td><input type="password" name="password" /></td>
45                </tr>
46            </table>
47            <br/>
48            <div style="text-align: center;">
49             <input type="submit" value="登录" />&nbsp;&nbsp;&nbsp;
50             <input type="reset" value="重置" />
51         </div>
52        </form>
53   </body>
54 </html>

user-list.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
 4 <%
 5 String path = request.getContextPath();
 6 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 7 %>
 8 
 9 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
10 <html>
11   <head>
12     <base href="<%=basePath%>">
13     
14     <title>用户列表</title>
15     
16     <meta http-equiv="pragma" content="no-cache">
17     <meta http-equiv="cache-control" content="no-cache">
18     <meta http-equiv="expires" content="0">    
19     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
20     <meta http-equiv="description" content="This is my page">
21     <!--
22     <link rel="stylesheet" type="text/css" href="styles.css">
23     -->
24 
25   </head>
26   
27   <script type="text/javascript">
28     //全选、全反选
29     function doSelectAll() {
30         $("input[name='selectedRow']").prop("checked", $("#selAll").is(":checked"));
31     }
32   </script>
33   
34   <style> 
35     table tr th{ border:1px solid #C1C1C1; font-size: 16px;}
36     table,table tr td { border:1px solid #C1C1C1; }
37     table { width: 80%; min-height: 25px; line-height: 25px; border-collapse: collapse; padding:2px; margin:auto;text-align: center;}
38     a{text-decoration: none;font-weight: bold;}
39   </style> 
40   
41   <body>
42       <h3 align="center">用户信息列表</h3>
43     <table>
44         <tr style=" background-color: #EBEBEB">
45             <th><input type="checkbox" id="selAll" onclick="doSelectAll()" /></th>
46             <th>序号</th>
47             <th>姓名</th>
48             <th>性别</th>
49             <th>生日</th>
50             <th>账号</th>
51             <th>邮箱</th>
52             <th>电话</th>
53             <th>操作</th>
54         </tr>
55         <c:forEach items="${users}" var="user" varStatus="st">
56             <tr>
57                  <td><input type="checkbox" name="selectedRow" value="<s:property value='id'/>"/></td>
58                  <td>${st.count }</td>
59                  <td>${user.name }</td>
60                  <td>
61                      <c:if test="${user.sex==1}"></c:if>
62                     <c:if test="${user.sex==0}"></c:if> 
63                  </td>
64                  <td>${user.birthday }</td>
65                  <td>${user.account }</td>
66                  <td>${user.email }</td>
67                  <td>${user.telphone }</td>
68                  <td>
69                      <a href="user/listAll.action">编辑</a><!-- 此功能未实现,留空,简单演示SSH框架(xml版) -->
70                      <a href="user/listAll.action">删除</a><!-- 此功能未实现,留空,简单演示SSH框架(xml版) -->
71                  </td>
72              </tr>
73            </c:forEach>
74     </table>
75     <br/>
76     <div style="text-align: center;">
77         <a href="jsp/user/user-add.jsp">新增用户</a>&nbsp;&nbsp;&nbsp;&nbsp;
78         <a href="user/listAll.action">批量删除</a><!-- 此功能未实现,留空,简单演示SSH框架(xml版) -->
79     </div>
80   </body>
81 </html>

user-add.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 3 <%
 4 String path = request.getContextPath();
 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
 7 
 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 9 <html>
10   <head>
11     <base href="<%=basePath%>">
12     
13     <title>新增用户</title>
14     
15     <meta http-equiv="pragma" content="no-cache">
16     <meta http-equiv="cache-control" content="no-cache">
17     <meta http-equiv="expires" content="0">    
18     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
19     <meta http-equiv="description" content="This is my page">
20     <!--
21     <link rel="stylesheet" type="text/css" href="styles.css">
22     -->
23 
24   </head>
25   
26   <style> 
27     table tr th{ border:1px solid #C1C1C1; font-size: 16px;}
28     table,table tr td { border:1px solid #C1C1C1; }
29     th{background-color: #EBEBEB; text-align: center;}
30     table { width: 50%; min-height: 25px; line-height: 25px; border-collapse: collapse; padding:2px; margin:auto;}
31     a{text-decoration: none;font-weight: bold;}
32   </style> 
33   
34   <body>
35       <form action="<%=basePath%>user/register.action" method="get">
36           <h3 align="center">新增用户</h3>
37         <table>
38             <tr>
39                 <th>姓名</th>
40                 <td><input name="name" value="" style="width:45%;"/></td>
41             </tr>
42             <tr>
43                 <th>性别</th>
44                 <td>
45                     <input type="radio" name="sex" value="1" checked="checked"/>46                     <input type="radio" name="sex" value="0"/>47                 </td>
48             </tr>
49             <tr>
50                 <th>生日</th><!-- 此处,可加个日期控件,单击选择日期(省略不做);这里我用手写输入的形式:yyyy-MM-dd -->
51                 <td><input type="text" name="birthday" value="" style="width:45%;"/></td>
52             </tr>
53             <tr>
54                 <th>账号</th>
55                 <td><input name="account" value="" style="width:45%;"/></td>
56             </tr>
57             <tr>
58                 <th>密码</th>
59                 <td><input name="password" style="width:45%;"/></td>
60             </tr>
61             <tr>
62                 <th>电话</th>
63                  <td><input id="telphone" name="telphone" value="" onkeyup="value=value.replace(/[^\d]/g,'')"  style="width:45%;"/></td>
64             </tr>
65             <tr>
66                 <th>邮箱</th>
67                 <td><input id="email" name="email" value=""  style="width:45%;"/></td>
68             </tr>
69         </table>
70         <br/>
71         <div style="text-align: center;">
72             <input type="submit" value="保存" />&nbsp;&nbsp;&nbsp;&nbsp;
73             <input type="reset" value="重置" />&nbsp;&nbsp;&nbsp;&nbsp;
74             <input type="button"  onclick="javascript:history.go(-1)" value="返回" />
75         </div>
76     </form>
77   </body>
78 </html>

 

到此已完结!有任何问题,可留言。

 

 

 

 

 

 

 

原创作者:DSHORE

作者主页:http://www.cnblogs.com/dshore123/

原文出自:https://www.cnblogs.com/dshore123/p/12396023.html

版权声明:欢迎转载,转载务必说明出处。(如果本文对您有帮助,可以点击一下右下角的 推荐,或评论,谢谢!

posted @ 2020-03-02 15:52  DSHORE  阅读(766)  评论(0编辑  收藏  举报