<%--
  Created by IntelliJ IDEA.
  User: 呆萌老师:QQ:2398779723
  Date: 2019/12/6
  Time: 15:55
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String baseurl=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath();
    pageContext.setAttribute("baseurl",baseurl);
%>
< html> < basehref = " ${baseurl}" /> < head> < title> </ title> < scriptsrc = " ${baseurl}/static/js/jquery1.11.3.min.js" > </ script> < scriptsrc = " ${baseurl}/static/js/reg.js" > </ script> </ head> < body> < fromaction = " ${baseurl}/users/reg" method = " post" > < pre> < inputtype = " text" name = " uname" /> < spanid = " s1" > </ span> < inputtype = " password" name = " pwd" /> < inputtype = " password" name = " repwd" /> < selectid = " major" > </ select> < inputtype = " submit" name = " sub" value = " 注册" /> </ pre> </ from> </ body> </ html> 
$ ( function ( ) { 
    
    $ ( ":text[name='uname']" ) . blur ( function ( ) { 
          
        var  uname= $ ( this ) . val ( ) ; 
        
        $. get ( "http://localhost:8080/TestSpringMVC4/users/checkUname?uname=" + uname, function ( msg ) { 
            
            if ( msg== "exists" ) 
                $ ( "#s1" ) . html ( "已存在" ) ; 
            else 
                $ ( "#s1" ) . html ( "可以使用" ) ; 
        } ) 
    } ) 
    
    $ ( "#major" ) . click ( function ( ) { 
        
        $. getJSON ( "http://localhost:8080/TestSpringMVC4/users/getMajorList2" , function ( arr ) { 
            
            $ ( "#major" ) . empty ( ) ; 
            $. each ( arr, function ( k, v ) { 
                
                var  option= $ ( "<option></option>" ) ; 
                option. val ( v. id) ; 
                option. html ( v. name) ; 
                $ ( "#major" ) . append ( option) ; 
            } ) 
        } ) 
    } ) 
} ) 
package  com. test. controller ; 
import  com. alibaba. fastjson.  JSON ; 
import  com. test. pojo.  Major ; 
import  com. test. pojo.  Users ; 
import  org. springframework. stereotype.  Controller ; 
import  org. springframework. web. bind. annotation.  RequestMapping ; 
import  org. springframework. web. bind. annotation.  ResponseBody ; 
import  javax. servlet. http.  HttpServletRequest ; 
import  javax. servlet. http.  HttpSession ; 
import  java. util.  ArrayList ; 
import  java. util.  List ; 
@Controller 
@RequestMapping ( "/users" ) 
public  class  UsersController  { 
    @RequestMapping ( "/loginUi" ) 
    public  String  loginUi ( ) 
    { 
        return  "login" ; 
    } 
    @RequestMapping ( "/checkLogin" ) 
    public  String  checkLogin ( Users  user,  HttpServletRequest  request) 
    { 
        if ( user. getUname ( ) . equals ( "daimenglaoshi" )  &&  user. getPwd ( ) . equals ( "123" ) ) 
        { 
            
            HttpSession  session=  request. getSession ( ) ; 
            session. setAttribute ( "loginUser" , user) ; 
            
            return  "redirect:/index.jsp" ; 
        } 
        else 
            return  "login" ; 
    } 
    @RequestMapping ( "/regUi" ) 
    public  String  regUi ( ) 
    { 
        return  "reg" ; 
    } 
    @RequestMapping ( "/checkUname" ) 
    
    @ResponseBody 
    public  String  checkUname ( String  uname) 
    { 
        if ( uname. equals ( "daimenglaoshi" ) ) 
            return  "exists" ; 
        else 
            return  "not exists" ; 
    } 
    @RequestMapping ( "/getMajorList" ) 
    @ResponseBody 
    
    public  List < Major > getMajorList ( ) 
    { 
        List < Major > = new  ArrayList < Major > ( ) ; 
        System . out. println ( "2222" ) ; 
        majorList. add ( new  Major ( 1 , "aaa" ) ) ; 
        majorList. add ( new  Major ( 2 , "bb" ) ) ; 
        return  majorList; 
    } 
    @RequestMapping ( value= "/getMajorList2" , produces =  "text/html;charset=utf-8" ) 
    @ResponseBody 
    public  String  getMajorList2 ( ) 
    { 
        List < Major > = new  ArrayList < Major > ( ) ; 
        majorList. add ( new  Major ( 1 , "计算机" ) ) ; 
        majorList. add ( new  Major ( 2 , "英语" ) ) ; 
        return  JSON . toJSONString ( majorList) ; 
    } 
}