jsp
<%--Created by IntelliJ IDEA.User: 呆萌老师:QQ:2398779723Date: 2019/12/6Time: 15:55To 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>
< base href = " ${baseurl}" />
< head> < title> Title</ title> < script src = " ${baseurl}/static/js/jquery1.11.3.min.js" > </ script> < script src = " ${baseurl}/static/js/reg.js" > </ script>
</ head>
< body>
< from action = " ${baseurl}/users/reg" method = " post" > < pre> 用户名:< input type = " text" name = " uname" /> < span id = " s1" > </ span> 密码:< input type = " password" name = " pwd" /> 确认密码:< input type = " password" name = " repwd" /> 专业:< select id = " major" > </ select> < input type = " submit" name = " sub" value = " 注册" /> </ pre>
</ from>
</ body>
</ html>
js
$ ( 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) ; } ) } ) } ) } )
java
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 > majorList= 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 > majorList= new ArrayList < Major > ( ) ; majorList. add ( new Major ( 1 , "计算机" ) ) ; majorList. add ( new Major ( 2 , "英语" ) ) ; return JSON . toJSONString ( majorList) ; } }
项目结构