登录注册样例(附带输入信息检验) springmvc


1 注册界面

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<html>
<head>
<meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>买家注册title>
  
  <meta name="viewport" content="width=device-width, initial-scale=1">

  
  <link rel="stylesheet" href="${pageContext.request.contextPath}/conf1/css/font-awesome.min.css">
  
  <link rel="stylesheet" href="${pageContext.request.contextPath}/conf1/css/ionicons.min.css">
  
  <link rel="stylesheet" href="${pageContext.request.contextPath}/conf1/dist/css/adminlte.min.css">
  
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700" >
head>

<script type="text/javascript"> function checkForm(){ if($.trim($("#name").val())==""){ alert("请填写名字"); $("#name").focus(); return false; } if($("#name").val().length>50){ alert("名字长度不得大于50个字符"); $("#name").focus(); return false; } if($.trim($("#username").val())==""){ alert("请填写用户名"); $("#username").focus(); return false; } if($("#username").val().length>50){ alert("用户名长度不得大于50个字符"); $("#username").focus(); return false; } if($.trim($("#password").val())==""){ alert("请填写密码"); $("#password").focus(); return false; } if($("#password").val().length>50){ alert("密码长度不得大于50个字符"); $("#password").focus(); return false; } if($.trim($("#studentId").val())==""){ alert("请填写学号"); $("#studentId").focus(); return false; } if($("#studentId").val().length>50){ alert("学号长度不得大于50个字符"); $("#studentId").focus(); return false; } if($.trim($("#className").val())==""){ alert("请填写班级名字"); $("#className").focus(); return false; } if($("#className").val().length>50){ alert("班级名字长度不得大于50个字符"); $("#className").focus(); return false; } if($.trim($("#dormitoryNumber").val())==""){ alert("请填写宿舍号"); $("#dormitoryNumber").focus(); return false; } if($("#dormitoryNumber").val().length>50){ alert("宿舍号长度不得大于50个字符"); $("#dormitoryNumber").focus(); return false; } if($.trim($("#signature").val())==""){ alert("请填写个性签名"); $("#signature").focus(); return false; } if($("#signature").val().length>50){ alert("个性签名长度不得大于50个字符"); $("#signature").focus(); return false; } if($.trim($("#introduction").val())==""){ alert("请填写自我介绍"); $("#introduction").focus(); return false; } if($("#introduction").val().length>5000){ alert("自我介绍长度不得大于5000个字符"); $("#introduction").focus(); return false; } if($.trim($("#pic").val())==""){ alert("请上传校园卡照片"); $("#pic").focus(); return false; } return true; } script>
<% if ((String)request.getAttribute("status") == "success") { %>
    <script> alert("${info}"); script>

<%}%>



<body>
<div class="wrapper">
  <jsp:include page="side_buyer.jsp">jsp:include>

  
  <div class="content-wrapper">
    
    <section class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1>买家注册h1>
          div>

        div>
      div>
    section>

    
    <section class="content">


       <div class="container-fluid">
        <div class="row">
          
          <div class="col-12">
            
            <div class="card card-primary">
              <div class="card-header">
                <h3 class="card-title">自身信息h3>
              div>
              
              
              <form role="form" method="post" enctype="multipart/form-data" onSubmit="return checkForm()" action="${pageContext.request.contextPath}/buyer/register.do">
                <div class="card-body">
                  <div class="form-group">
                    <label for="exampleInputEmail1">名字label>
                    <input type="text" class="form-control" id="name" name="name" placeholder="Enter ...">
                  div>
                  <div class="form-group">
                    <label for="exampleInputEmail1">用户名label>
                    <input type="text" class="form-control" id="username" name="username" placeholder="Enter ...">
                  div>
                  <div class="form-group">
                    <label for="exampleInputEmail1">密码label>
                    <input type="text" class="form-control" id="password" name="password" placeholder="Enter ...">
                  div>
                  <div class="form-group">
                    <label>性别label>
                    <select class="form-control" id="sex" name="sex">
                      <option value="男">option>
                      <option value="女">option>                     
                    select>
                  div>
                  <div class="form-group">
                    <label for="exampleInputEmail1">学号label>
                    <input type="text" class="form-control" id="studentId" name="studentId" placeholder="Enter ...">
                  div>
                  <div class="form-group">
                    <label for="exampleInputEmail1">班级label>
                    <input type="text" class="form-control" id="className" name="className" placeholder="Enter ...">
                  div>
                  <div class="form-group">
                    <label for="exampleInputEmail1">宿舍号label>
                    <input type="text" class="form-control" id="dormitoryNumber" name="dormitoryNumber" placeholder="Enter ...">
                  div>
                   <div class="form-group">
                    <label for="exampleInputEmail1">个性签名label>
                    <input type="text" class="form-control" id="signature" name="signature" placeholder="Enter ...">
                  div>
                  <div class="form-group">
                    <label>自我介绍label>
                    <textarea class="form-control" id="introduction" name="introduction" rows="10" placeholder="Enter ...">textarea>
                  div>
                <div class="form-group">
                    <label for="pic">校园卡图片label>
                    <div class="input-group">
                      <div class="custom-file">                       
                        <input type="file" class="" size="80" id="pic" name="pic"/>
                      div>                    
                    div>
                div>                  
                div>
                <div class="card-footer">
                  <button type="submit" class="btn btn-primary">提交button>
                div>
              form>
            div>
            

        div>
        
      div>







    section>
    
  div>
  
  <footer class="main-footer">
    <div class="float-right d-none d-sm-block">
      <b>b> 
    div>
    <strong>Copyright © 2018 <a href="#">中央财经大学a>.strong> All rights
    reserved.
  footer>

  
  <aside class="control-sidebar control-sidebar-dark">
    
  aside>
  
div>



<script src="${pageContext.request.contextPath}/conf1/plugins/jquery/jquery.min.js">script>

<script src="${pageContext.request.contextPath}/conf1/plugins/bootstrap/js/bootstrap.bundle.min.js">script>

<script src="${pageContext.request.contextPath}/conf1/plugins/fastclick/fastclick.js">script>

<script src="${pageContext.request.contextPath}/conf1/dist/js/adminlte.min.js">script>

<script src="${pageContext.request.contextPath}/conf1/dist/js/demo.js">script>
body>
html>

2 登录界面

<%@ 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>
<link href="${pageContext.request.contextPath}/conf2/css/login.css" rel="stylesheet" rev="stylesheet" type="text/css" media="all" />
<script type="text/javascript" src="${pageContext.request.contextPath}/conf2/js/jQuery1.7.js">script>
<script type="text/javascript" src="${pageContext.request.contextPath}/conf2/js/jquery-1.8.2.min.js">script>
<script type="text/javascript" src="${pageContext.request.contextPath}/conf2/js/jquery1.42.min.js">script>
<script type="text/javascript" src="${pageContext.request.contextPath}/conf2/js/jquery.SuperSlide.js">script>
<script type="text/javascript" src="${pageContext.request.contextPath}/conf2/js/Validform_v5.3.2_min.js">script>
<script type="text/javascript"> $(document).ready(function(){ var $tab_li = $('#tab ul li'); $tab_li.hover(function(){ $(this).addClass('selected').siblings().removeClass('selected'); var index = $tab_li.index(this); $('div.tab_box > div').eq(index).show().siblings().hide(); }); }); script>

<script type="text/javascript"> $(function(){ $(".screenbg ul li").each(function(){ $(this).css("opacity","0"); }); $(".screenbg ul li:first").css("opacity","1"); var index = 0; var t; var li = $(".screenbg ul li"); var number = li.size(); function change(index){ li.css("visibility","visible"); li.eq(index).siblings().animate({opacity:0},3000); li.eq(index).animate({opacity:1},3000); } function show(){ index = index + 1; if(index<=number-1){ change(index); }else{ index = 0; change(index); } } t = setInterval(show,8000); //根据窗口宽度生成图片宽度 var width = $(window).width(); $(".screenbg ul img").css("width",width+"px"); }); script>
head>
script>
<% if ((String)request.getAttribute("status") == "success") { %>
    <script> alert("${info}"); script>

<%}%>

<body>
<div id="tab">
  <ul class="tab_menu">
    <li class="selected">买家登录li>
    <li>卖家登录li>
    <li>管理员登录li>
  ul>
  <div class="tab_box"> 
    <div>
      <div class="stu_error_box">div>
      <form action="${pageContext.request.contextPath}/buyer/login.do" method="post" class="stu_login_error">
        <div id="username">
          <label>用户名:label>
          <input type="text" id="stu_username_hide" name="username" placeholder="输入用户名" />
        div>
        <div id="password">
          <label>密   码:label>
          <input type="password" id="stu_password_hide" name="password" placeholder="输入密码" />
        div>
        <div id="username">
            <a href="${pageContext.request.contextPath}/buyer/index.do" >
            <span style="float:right;font-weight: bold;color: #3d96c9;">
                返回首页        
            span>a>
          <a href="${pageContext.request.contextPath}/views/buyer/register.jsp" >
            <span style="float:right;font-weight: bold;color: #3d96c9;">
                买家注册        
            span>a>
        div>

        <div id="login">
          <button type="submit">登录button>
        div>
      form>
    div>

    <div class="hide">
     <div class="tea_error_box">div>
      <form action="${pageContext.request.contextPath}/seller/login.do" method="post" class="tea_login_error">
        <div id="username">
          <label>用户名:label>
          <input type="text" id="tea_username_hide" name="username" placeholder="输入用户名" />
        div>
        <div id="password">
          <label>密   码:label>
          <input type="password" id="tea_password_hide" name="password" placeholder="输入密码" />
        div>
        <div id="username">
        <a href="${pageContext.request.contextPath}/buyer/index.do" >
            <span style="float:right;font-weight: bold;color: #3d96c9;">
                返回首页        
            span>a>
          <a href="${pageContext.request.contextPath}/views/seller/register.jsp" >
            <span style="float:right;font-weight: bold;color: #3d96c9;">
                卖家注册        
            span>a>
        div>
        <div id="login">
          <button type="submit">登录button>
        div>
      form>
    div>

    <div class="hide">
    <div class="sec_error_box">div>
      <form action="${pageContext.request.contextPath}/admin/login.do" method="post" class="sec_login_error">
        <div id="username">
          <label>用户名:label>
          <input type="text" id="sec_username_hide" name="username" placeholder="输入用户名" />
        div>
        <div id="password">
          <label>密   码:label>
          <input type="password" id="sec_password_hide" name="password" placeholder="输入密码" />
        div> 
        <div id="username">
        <a href="${pageContext.request.contextPath}/buyer/index.do" >
            <span style="float:right;font-weight: bold;color: #3d96c9;">
                返回首页        
            span>a>

        div>    
        <div id="login">
          <button type="submit">登录button>
        div>
      form>
    div>
  div>
div>
<div class="bottom">©2018 中央财经大学div>
<div class="screenbg">
  <ul>
    <li><a href="javascript:;"><img src="${pageContext.request.contextPath}/conf2/images/0.jpg">a>li>
    <li><a href="javascript:;"><img src="${pageContext.request.contextPath}/conf2/images/1.jpg">a>li>
    <li><a href="javascript:;"><img src="${pageContext.request.contextPath}/conf2/images/2.jpg">a>li>
  ul>
div>
body>
html>

3 登录注册的controller,附带退出登录

package com.ssh.controller;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.ssh.po.Activity;
import com.ssh.po.Buyer;
import com.ssh.po.Cart;
import com.ssh.po.Category;
import com.ssh.po.Collection;
import com.ssh.po.DetailedOrder;
import com.ssh.po.Order;
import com.ssh.po.Product;
import com.ssh.po.Seller;
import com.ssh.service.BuyerService;

@Controller
@RequestMapping("/buyer")
public class BuyerController
{

    @Autowired
    private BuyerService buyerService;


    public BuyerService getBuyerService()
    {
        return buyerService;
    }

    public void setBuyerService(BuyerService buyerService)
    {
        this.buyerService = buyerService;
    }

    @RequestMapping(value="/login") 
    public ModelAndView login(String username,String password,HttpServletRequest request){
        Buyer buyer=buyerService.login(username,password);
        if(buyer==null){
            ModelAndView modelAndView = new ModelAndView("/login"); 
            modelAndView.addObject("status", "success");
            modelAndView.addObject("info", "买家用户名和密码不正确"); 
            return modelAndView; 
        }
        request.getSession().setAttribute("buyer", buyer);
        ModelAndView modelAndView = new ModelAndView("/buyer/info_show"); 
        modelAndView.addObject("status", "success");
        modelAndView.addObject("info", "用户"+buyer.getName()+"登录成功");
        return modelAndView; 
    }

     @RequestMapping(value = "/logout")
     public String logout(HttpServletRequest request){
         HttpSession session=request.getSession();
         session.invalidate();
         return "/login";
     }

    @RequestMapping(value="/register") 
    public ModelAndView register(Buyer buyer, MultipartFile pic,HttpServletRequest request) throws Exception{
        String username=buyer.getUsername();
        Buyer now=buyerService.hasUsername(username);
        if(now!=null){
            ModelAndView modelAndView = new ModelAndView("/buyer/register"); 
            modelAndView.addObject("status", "success"); 
            modelAndView.
            addObject("info", "用户名"+username+"已经存在,请重新注册"); 
            return modelAndView;
        }
        String originalFilename = pic.getOriginalFilename();
        if (pic != null && originalFilename != null && originalFilename.length() > 0)
        {   
            String pic_path = "C:\\Pic\\Buyer\\";
            String newFileName = "买家_" + buyer.getUsername()+"_"
                    + originalFilename;
                    //+ originalFilename.substring(originalFilename.lastIndexOf("."));
            File newFile = new File(pic_path + newFileName);
            pic.transferTo(newFile);
            buyer.setPicPath(newFileName);
        }
        buyer.setMoney(10000);
        buyerService.register(buyer);
        request.getSession().setAttribute("buyer", buyer);
        ModelAndView modelAndView = new ModelAndView("/buyer/register"); 

        modelAndView.addObject("status", "success"); 
        modelAndView.addObject("info", "注册成功"); 
        return modelAndView; 
    }

4 对应的service

@Service
public class BuyerServiceImpl implements BuyerService {
    @Autowired
    private BaseDao baseDao;

    public BaseDao getBaseDao() {
        return baseDao;
    }

    public void setBaseDao(BaseDao baseDao) {
        this.baseDao = baseDao;
    }

    @Override
    public Buyer hasUsername(String username){
        return (Buyer)baseDao.
                uniqueQuery("from Buyer where username=?0", username);
    }

    @Override
    public void register(Buyer buyer){
        baseDao.save(buyer);
    }
    @Override
    public Buyer login(String username,String password){
        return (Buyer)baseDao.uniqueQuery("From Buyer Where username=?0 And password =?1", username,password);
    }