Ajax+SpringMVC+Spring+Mybatis+MySql+js用戶註冊實例

林炳文Evankaka原創作品。轉載請註明出處http://blog.csdn.net/evankaka 

       摘要:這幾天研究了下Ajax註冊的方法,通過在註冊時輸入用戶名或郵箱等,就可以判斷這個用戶是否存在,以免用戶來註冊,然後提交了,系統才提示該用戶名或郵箱不可用。使用Ajax便可實現這一功能,看了網上的都是php的,想想索性來寫一個SpringMVC+Spring+Mybatis的。文章內容用到了很多技術,包括javascript、jquery、json、e表達式等。

本文工程免費下載

先來看看最終效果:

註冊成功的樣子:


註冊過程中參數的檢驗:


下面,讓我們開始這次的編程吧!

首先,數據庫準備,新建一張用戶表,並自己插入一些數據

[sql]  view plain  copy
  1. CREATE TABLE  
  2.     t_user  
  3.     (  
  4.         USER_ID INT NOT NULL AUTO_INCREMENT,  
  5.         USER_NAME CHAR(30) NOT NULL,  
  6.         USER_PASSWORD CHAR(10) NOT NULL,  
  7.         USER_EMAIL CHAR(30) NOT NULL,  
  8.         PRIMARY KEY (USER_ID)  
  9.     )  
  10.     ENGINE=InnoDB DEFAULT CHARSET=utf8;  
好,數據庫建好了,接下來就是整個工程了。

一、工程整體

項目類型:Dynamic Web Project

開發環境:

Eclipse Java EE IDE for Web Developers.
Version: Luna Service Release 2 (4.4.2)
Build id: 20150219-0600

JDK版本:

jdk1.6.0_45

本文工程免費下載

工程的其它參數:


1、用到的JAR包

json使用的包。這裏下載

spring+sprngMvc的包。

mybatis的包

mysql連接的包

logger的包(日記 打印的)

如下:


2、需要的外部js

jquery-1.11.3.min.js

3、項目結構

這是還沒有展開的


然後把各個都展開:



其中src放置java的文件、config放置SpringMVC+Spring+Mybatis的配置文件以及日記打印的log4j.properties

web-inf下面:js用來放置調用 的js文件,lib就是最上面說的jar包的位置,view是各個jsp文件

二、編程

2.1 java代碼

這裏分了5層,按照標準的web工程來

1、domain層

這裏是使用Mybatis Generator自動生成的:User.java

[java]  view plain  copy
  1. package com.lin.domain;  
  2.   
  3. public class User {  
  4.     /** 
  5.      * This field was generated by MyBatis Generator. 
  6.      * This field corresponds to the database column t_user.USER_ID 
  7.      * 
  8.      * @mbggenerated 
  9.      */  
  10.     private Integer userId;  
  11.   
  12.     /** 
  13.      * This field was generated by MyBatis Generator. 
  14.      * This field corresponds to the database column t_user.USER_NAME 
  15.      * 
  16.      * @mbggenerated 
  17.      */  
  18.     private String userName;  
  19.   
  20.     /** 
  21.      * This field was generated by MyBatis Generator. 
  22.      * This field corresponds to the database column t_user.USER_PASSWORD 
  23.      * 
  24.      * @mbggenerated 
  25.      */  
  26.     private String userPassword;  
  27.   
  28.     /** 
  29.      * This field was generated by MyBatis Generator. 
  30.      * This field corresponds to the database column t_user.USER_EMAIL 
  31.      * 
  32.      * @mbggenerated 
  33.      */  
  34.     private String userEmail;  
  35.   
  36.     /** 
  37.      * This method was generated by MyBatis Generator. 
  38.      * This method returns the value of the database column t_user.USER_ID 
  39.      * 
  40.      * @return the value of t_user.USER_ID 
  41.      * 
  42.      * @mbggenerated 
  43.      */  
  44.     public Integer getUserId() {  
  45.         return userId;  
  46.     }  
  47.   
  48.     /** 
  49.      * This method was generated by MyBatis Generator. 
  50.      * This method sets the value of the database column t_user.USER_ID 
  51.      * 
  52.      * @param userId the value for t_user.USER_ID 
  53.      * 
  54.      * @mbggenerated 
  55.      */  
  56.     public void setUserId(Integer userId) {  
  57.         this.userId = userId;  
  58.     }  
  59.   
  60.     /** 
  61.      * This method was generated by MyBatis Generator. 
  62.      * This method returns the value of the database column t_user.USER_NAME 
  63.      * 
  64.      * @return the value of t_user.USER_NAME 
  65.      * 
  66.      * @mbggenerated 
  67.      */  
  68.     public String getUserName() {  
  69.         return userName;  
  70.     }  
  71.   
  72.     /** 
  73.      * This method was generated by MyBatis Generator. 
  74.      * This method sets the value of the database column t_user.USER_NAME 
  75.      * 
  76.      * @param userName the value for t_user.USER_NAME 
  77.      * 
  78.      * @mbggenerated 
  79.      */  
  80.     public void setUserName(String userName) {  
  81.         this.userName = userName == null ? null : userName.trim();  
  82.     }  
  83.   
  84.     /** 
  85.      * This method was generated by MyBatis Generator. 
  86.      * This method returns the value of the database column t_user.USER_PASSWORD 
  87.      * 
  88.      * @return the value of t_user.USER_PASSWORD 
  89.      * 
  90.      * @mbggenerated 
  91.      */  
  92.     public String getUserPassword() {  
  93.         return userPassword;  
  94.     }  
  95.   
  96.     /** 
  97.      * This method was generated by MyBatis Generator. 
  98.      * This method sets the value of the database column t_user.USER_PASSWORD 
  99.      * 
  100.      * @param userPassword the value for t_user.USER_PASSWORD 
  101.      * 
  102.      * @mbggenerated 
  103.      */  
  104.     public void setUserPassword(String userPassword) {  
  105.         this.userPassword = userPassword == null ? null : userPassword.trim();  
  106.     }  
  107.   
  108.     /** 
  109.      * This method was generated by MyBatis Generator. 
  110.      * This method returns the value of the database column t_user.USER_EMAIL 
  111.      * 
  112.      * @return the value of t_user.USER_EMAIL 
  113.      * 
  114.      * @mbggenerated 
  115.      */  
  116.     public String getUserEmail() {  
  117.         return userEmail;  
  118.     }  
  119.   
  120.     /** 
  121.      * This method was generated by MyBatis Generator. 
  122.      * This method sets the value of the database column t_user.USER_EMAIL 
  123.      * 
  124.      * @param userEmail the value for t_user.USER_EMAIL 
  125.      * 
  126.      * @mbggenerated 
  127.      */  
  128.     public void setUserEmail(String userEmail) {  
  129.         this.userEmail = userEmail == null ? null : userEmail.trim();  
  130.     }  
  131. }  
然後還有一個example文件:UserExample.java

[java]  view plain  copy
  1. package com.lin.domain;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. public class UserExample {  
  7.     /** 
  8.      * This field was generated by MyBatis Generator. 
  9.      * This field corresponds to the database table t_user 
  10.      * 
  11.      * @mbggenerated 
  12.      */  
  13.     protected String orderByClause;  
  14.   
  15.     /** 
  16.      * This field was generated by MyBatis Generator. 
  17.      * This field corresponds to the database table t_user 
  18.      * 
  19.      * @mbggenerated 
  20.      */  
  21.     protected boolean distinct;  
  22.   
  23.     /** 
  24.      * This field was generated by MyBatis Generator. 
  25.      * This field corresponds to the database table t_user 
  26.      * 
  27.      * @mbggenerated 
  28.      */  
  29.     protected List<Criteria> oredCriteria;  
  30.   
  31.     /** 
  32.      * This method was generated by MyBatis Generator. 
  33.      * This method corresponds to the database table t_user 
  34.      * 
  35.      * @mbggenerated 
  36.      */  
  37.     public UserExample() {  
  38.         oredCriteria = new ArrayList<Criteria>();  
  39.     }  
  40.   
  41.     /** 
  42.      * This method was generated by MyBatis Generator. 
  43.      * This method corresponds to the database table t_user 
  44.      * 
  45.      * @mbggenerated 
  46.      */  
  47.     public void setOrderByClause(String orderByClause) {  
  48.         this.orderByClause = orderByClause;  
  49.     }  
  50.   
  51.     /** 
  52.      * This method was generated by MyBatis Generator. 
  53.      * This method corresponds to the database table t_user 
  54.      * 
  55.      * @mbggenerated 
  56.      */  
  57.     public String getOrderByClause() {  
  58.         return orderByClause;  
  59.     }  
  60.   
  61.     /** 
  62.      * This method was generated by MyBatis Generator. 
  63.      * This method corresponds to the database table t_user 
  64.      * 
  65.      * @mbggenerated 
  66.      */  
  67.     public void setDistinct(boolean distinct) {  
  68.         this.distinct = distinct;  
  69.     }  
  70.   
  71.     /** 
  72.      * This method was generated by MyBatis Generator. 
  73.      * This method corresponds to the database table t_user 
  74.      * 
  75.      * @mbggenerated 
  76.      */  
  77.     public boolean isDistinct() {  
  78.         return distinct;  
  79.     }  
  80.   
  81.     /** 
  82.      * This method was generated by MyBatis Generator. 
  83.      * This method corresponds to the database table t_user 
  84.      * 
  85.      * @mbggenerated 
  86.      */  
  87.     public List<Criteria> getOredCriteria() {  
  88.         return oredCriteria;  
  89.     }  
  90.   
  91.     /** 
  92.      * This method was generated by MyBatis Generator. 
  93.      * This method corresponds to the database table t_user 
  94.      * 
  95.      * @mbggenerated 
  96.      */  
  97.     public void or(Criteria criteria) {  
  98.         oredCriteria.add(criteria);  
  99.     }  
  100.   
  101.     /** 
  102.      * This method was generated by MyBatis Generator. 
  103.      * This method corresponds to the database table t_user 
  104.      * 
  105.      * @mbggenerated 
  106.      */  
  107.     public Criteria or() {  
  108.         Criteria criteria = createCriteriaInternal();  
  109.         oredCriteria.add(criteria);  
  110.         return criteria;  
  111.     }  
  112.   
  113.     /** 
  114.      * This method was generated by MyBatis Generator. 
  115.      * This method corresponds to the database table t_user 
  116.      * 
  117.      * @mbggenerated 
  118.      */  
  119.     public Criteria createCriteria() {  
  120.         Criteria criteria = createCriteriaInternal();  
  121.         if (oredCriteria.size() == 0) {  
  122.             oredCriteria.add(criteria);  
  123.         }  
  124.         return criteria;  
  125.     }  
  126.   
  127.     /** 
  128.      * This method was generated by MyBatis Generator. 
  129.      * This method corresponds to the database table t_user 
  130.      * 
  131.      * @mbggenerated 
  132.      */  
  133.     protected Criteria createCriteriaInternal() {  
  134.         Criteria criteria = new Criteria();  
  135.         return criteria;  
  136.     }  
  137.   
  138.     /** 
  139.      * This method was generated by MyBatis Generator. 
  140.      * This method corresponds to the database table t_user 
  141.      * 
  142.      * @mbggenerated 
  143.      */  
  144.     public void clear() {  
  145.         oredCriteria.clear();  
  146.         orderByClause = null;  
  147.         distinct = false;  
  148.     }  
  149.   
  150.     /** 
  151.      * This class was generated by MyBatis Generator. 
  152.      * This class corresponds to the database table t_user 
  153.      * 
  154.      * @mbggenerated 
  155.      */  
  156.     protected abstract static class GeneratedCriteria {  
  157.         protected List<Criterion> criteria;  
  158.   
  159.         protected GeneratedCriteria() {  
  160.             super();  
  161.             criteria = new ArrayList<Criterion>();  
  162.         }  
  163.   
  164.         public boolean isValid() {  
  165.             return criteria.size() > 0;  
  166.         }  
  167.   
  168.         public List<Criterion> getCriteria() {  
  169.             return criteria;  
  170.         }  
  171.   
  172.         protected void addCriterion(String condition) {  
  173.             if (condition == null) {  
  174.                 throw new RuntimeException("Value for condition cannot be null");  
  175.             }  
  176.             criteria.add(new Criterion(condition));  
  177.         }  
  178.   
  179.         protected void addCriterion(String condition, Object value, String property) {  
  180.             if (value == null) {  
  181.                 throw new RuntimeException("Value for " + property + " cannot be null");  
  182.             }  
  183.             criteria.add(new Criterion(condition, value));  
  184.         }  
  185.   
  186.         protected void addCriterion(String condition, Object value1, Object value2, String property) {  
  187.             if (value1 == null || value2 == null) {  
  188.                 throw new RuntimeException("Between values for " + property + " cannot be null");  
  189.             }  
  190.             criteria.add(new Criterion(condition, value1, value2));  
  191.         }  
  192.   
  193.         public Criteria andUserIdIsNull() {  
  194.             addCriterion("USER_ID is null");  
  195.             return (Criteria) this;  
  196.         }  
  197.   
  198.         public Criteria andUserIdIsNotNull() {  
  199.             addCriterion("USER_ID is not null");  
  200.             return (Criteria) this;  
  201.         }  
  202.   
  203.         public Criteria andUserIdEqualTo(Integer value) {  
  204.             addCriterion("USER_ID =", value, "userId");  
  205.             return (Criteria) this;  
  206.         }  
  207.   
  208.         public Criteria andUserIdNotEqualTo(Integer value) {  
  209.             addCriterion("USER_ID <>", value, "userId");  
  210.             return (Criteria) this;  
  211.         }  
  212.   
  213.         public Criteria andUserIdGreaterThan(Integer value) {  
  214.             addCriterion("USER_ID >", value, "userId");  
  215.             return (Criteria) this;  
  216.         }  
  217.   
  218.         public Criteria andUserIdGreaterThanOrEqualTo(Integer value) {  
  219.             addCriterion("USER_ID >=", value, "userId");  
  220.             return (Criteria) this;  
  221.         }  
  222.   
  223.         public Criteria andUserIdLessThan(Integer value) {  
  224.             addCriterion("USER_ID <", value, "userId");  
  225.             return (Criteria) this;  
  226.         }  
  227.   
  228.         public Criteria andUserIdLessThanOrEqualTo(Integer value) {  
  229.             addCriterion("USER_ID <=", value, "userId");  
  230.             return (Criteria) this;  
  231.         }  
  232.   
  233.         public Criteria andUserIdIn(List<Integer> values) {  
  234.             addCriterion("USER_ID in", values, "userId");  
  235.             return (Criteria) this;  
  236.         }  
  237.   
  238.         public Criteria andUserIdNotIn(List<Integer> values) {  
  239.             addCriterion("USER_ID not in", values, "userId");  
  240.             return (Criteria) this;  
  241.         }  
  242.   
  243.         public Criteria andUserIdBetween(Integer value1, Integer value2) {  
  244.             addCriterion("USER_ID between", value1, value2, "userId");  
  245.             return (Criteria) this;  
  246.         }  
  247.   
  248.         public Criteria andUserIdNotBetween(Integer value1, Integer value2) {  
  249.             addCriterion("USER_ID not between", value1, value2, "userId");  
  250.             return (Criteria) this;  
  251.         }  
  252.   
  253.         public Criteria andUserNameIsNull() {  
  254.             addCriterion("USER_NAME is null");  
  255.             return (Criteria) this;  
  256.         }  
  257.   
  258.         public Criteria andUserNameIsNotNull() {  
  259.             addCriterion("USER_NAME is not null");  
  260.             return (Criteria) this;  
  261.         }  
  262.   
  263.         public Criteria andUserNameEqualTo(String value) {  
  264.             addCriterion("USER_NAME =", value, "userName");  
  265.             return (Criteria) this;  
  266.         }  
  267.   
  268.         public Criteria andUserNameNotEqualTo(String value) {  
  269.             addCriterion("USER_NAME <>", value, "userName");  
  270.             return (Criteria) this;  
  271.         }  
  272.   
  273.         public Criteria andUserNameGreaterThan(String value) {  
  274.             addCriterion("USER_NAME >", value, "userName");  
  275.             return (Criteria) this;  
  276.         }  
  277.   
  278.         public Criteria andUserNameGreaterThanOrEqualTo(String value) {  
  279.             addCriterion("USER_NAME >=", value, "userName");  
  280.             return (Criteria) this;  
  281.         }  
  282.   
  283.         public Criteria andUserNameLessThan(String value) {  
  284.             addCriterion("USER_NAME <", value, "userName");  
  285.             return (Criteria) this;  
  286.         }  
  287.   
  288.         public Criteria andUserNameLessThanOrEqualTo(String value) {  
  289.             addCriterion("USER_NAME <=", value, "userName");  
  290.             return (Criteria) this;  
  291.         }  
  292.   
  293.         public Criteria andUserNameLike(String value) {  
  294.             addCriterion("USER_NAME like", value, "userName");  
  295.             return (Criteria) this;  
  296.         }  
  297.   
  298.         public Criteria andUserNameNotLike(String value) {  
  299.             addCriterion("USER_NAME not like", value, "userName");  
  300.             return (Criteria) this;  
  301.         }  
  302.   
  303.         public Criteria andUserNameIn(List<String> values) {  
  304.             addCriterion("USER_NAME in", values, "userName");  
  305.             return (Criteria) this;  
  306.         }  
  307.   
  308.         public Criteria andUserNameNotIn(List<String> values) {  
  309.             addCriterion("USER_NAME not in", values, "userName");  
  310.             return (Criteria) this;  
  311.         }  
  312.   
  313.         public Criteria andUserNameBetween(String value1, String value2) {  
  314.             addCriterion("USER_NAME between", value1, value2, "userName");  
  315.             return (Criteria) this;  
  316.         }  
  317.   
  318.         public Criteria andUserNameNotBetween(String value1, String value2) {  
  319.             addCriterion("USER_NAME not between", value1, value2, "userName");  
  320.             return (Criteria) this;  
  321.         }  
  322.   
  323.         public Criteria andUserPasswordIsNull() {  
  324.             addCriterion("USER_PASSWORD is null");  
  325.             return (Criteria) this;  
  326.         }  
  327.   
  328.         public Criteria andUserPasswordIsNotNull() {  
  329.             addCriterion("USER_PASSWORD is not null");  
  330.             return (Criteria) this;  
  331.         }  
  332.   
  333.         public Criteria andUserPasswordEqualTo(String value) {  
  334.             addCriterion("USER_PASSWORD =", value, "userPassword");  
  335.             return (Criteria) this;  
  336.         }  
  337.   
  338.         public Criteria andUserPasswordNotEqualTo(String value) {  
  339.             addCriterion("USER_PASSWORD <>", value, "userPassword");  
  340.             return (Criteria) this;  
  341.         }  
  342.   
  343.         public Criteria andUserPasswordGreaterThan(String value) {  
  344.             addCriterion("USER_PASSWORD >", value, "userPassword");  
  345.             return (Criteria) this;  
  346.         }  
  347.   
  348.         public Criteria andUserPasswordGreaterThanOrEqualTo(String value) {  
  349.             addCriterion("USER_PASSWORD >=", value, "userPassword");  
  350.             return (Criteria) this;  
  351.         }  
  352.   
  353.         public Criteria andUserPasswordLessThan(String value) {  
  354.             addCriterion("USER_PASSWORD <", value, "userPassword");  
  355.             return (Criteria) this;  
  356.         }  
  357.   
  358.         public Criteria andUserPasswordLessThanOrEqualTo(String value) {  
  359.             addCriterion("USER_PASSWORD <=", value, "userPassword");  
  360.             return (Criteria) this;  
  361.         }  
  362.   
  363.         public Criteria andUserPasswordLike(String value) {  
  364.             addCriterion("USER_PASSWORD like", value, "userPassword");  
  365.             return (Criteria) this;  
  366.         }  
  367.   
  368.         public Criteria andUserPasswordNotLike(String value) {  
  369.             addCriterion("USER_PASSWORD not like", value, "userPassword");  
  370.             return (Criteria) this;  
  371.         }  
  372.   
  373.         public Criteria andUserPasswordIn(List<String> values) {  
  374.             addCriterion("USER_PASSWORD in", values, "userPassword");  
  375.             return (Criteria) this;  
  376.         }  
  377.   
  378.         public Criteria andUserPasswordNotIn(List<String> values) {  
  379.             addCriterion("USER_PASSWORD not in", values, "userPassword");  
  380.             return (Criteria) this;  
  381.         }  
  382.   
  383.         public Criteria andUserPasswordBetween(String value1, String value2) {  
  384.             addCriterion("USER_PASSWORD between", value1, value2, "userPassword");  
  385.             return (Criteria) this;  
  386.         }  
  387.   
  388.         public Criteria andUserPasswordNotBetween(String value1, String value2) {  
  389.             addCriterion("USER_PASSWORD not between", value1, value2, "userPassword");  
  390.             return (Criteria) this;  
  391.         }  
  392.   
  393.         public Criteria andUserEmailIsNull() {  
  394.             addCriterion("USER_EMAIL is null");  
  395.             return (Criteria) this;  
  396.         }  
  397.   
  398.         public Criteria andUserEmailIsNotNull() {  
  399.             addCriterion("USER_EMAIL is not null");  
  400.             return (Criteria) this;  
  401.         }  
  402.   
  403.         public Criteria andUserEmailEqualTo(String value) {  
  404.             addCriterion("USER_EMAIL =", value, "userEmail");  
  405.             return (Criteria) this;  
  406.         }  
  407.   
  408.         public Criteria andUserEmailNotEqualTo(String value) {  
  409.             addCriterion("USER_EMAIL <>", value, "userEmail");  
  410.             return (Criteria) this;  
  411.         }  
  412.   
  413.         public Criteria andUserEmailGreaterThan(String value) {  
  414.             addCriterion("USER_EMAIL >", value, "userEmail");  
  415.             return (Criteria) this;  
  416.         }  
  417.   
  418.         public Criteria andUserEmailGreaterThanOrEqualTo(String value) {  
  419.             addCriterion("USER_EMAIL >=", value, "userEmail");  
  420.             return (Criteria) this;  
  421.         }  
  422.   
  423.         public Criteria andUserEmailLessThan(String value) {  
  424.             addCriterion("USER_EMAIL <", value, "userEmail");  
  425.             return (Criteria) this;  
  426.         }  
  427.   
  428.         public Criteria andUserEmailLessThanOrEqualTo(String value) {  
  429.             addCriterion("USER_EMAIL <=", value, "userEmail");  
  430.             return (Criteria) this;  
  431.         }  
  432.   
  433.         public Criteria andUserEmailLike(String value) {  
  434.             addCriterion("USER_EMAIL like", value, "userEmail");  
  435.             return (Criteria) this;  
  436.         }  
  437.   
  438.         public Criteria andUserEmailNotLike(String value) {  
  439.             addCriterion("USER_EMAIL not like", value, "userEmail");  
  440.             return (Criteria) this;  
  441.         }  
  442.   
  443.         public Criteria andUserEmailIn(List<String> values) {  
  444.             addCriterion("USER_EMAIL in", values, "userEmail");  
  445.             return (Criteria) this;  
  446.         }  
  447.   
  448.         public Criteria andUserEmailNotIn(List<String> values) {  
  449.             addCriterion("USER_EMAIL not in", values, "userEmail");  
  450.             return (Criteria) this;  
  451.         }  
  452.   
  453.         public Criteria andUserEmailBetween(String value1, String value2) {  
  454.             addCriterion("USER_EMAIL between", value1, value2, "userEmail");  
  455.             return (Criteria) this;  
  456.         }  
  457.   
  458.         public Criteria andUserEmailNotBetween(String value1, String value2) {  
  459.             addCriterion("USER_EMAIL not between", value1, value2, "userEmail");  
  460.             return (Criteria) this;  
  461.         }  
  462.     }  
  463.   
  464.     /** 
  465.      * This class was generated by MyBatis Generator. 
  466.      * This class corresponds to the database table t_user 
  467.      * 
  468.      * @mbggenerated do_not_delete_during_merge 
  469.      */  
  470.     public static class Criteria extends GeneratedCriteria {  
  471.   
  472.         protected Criteria() {  
  473.             super();  
  474.         }  
  475.     }  
  476.   
  477.     /** 
  478.      * This class was generated by MyBatis Generator. 
  479.      * This class corresponds to the database table t_user 
  480.      * 
  481.      * @mbggenerated 
  482.      */  
  483.     public static class Criterion {  
  484.         private String condition;  
  485.   
  486.         private Object value;  
  487.   
  488.         private Object secondValue;  
  489.   
  490.         private boolean noValue;  
  491.   
  492.         private boolean singleValue;  
  493.   
  494.         private boolean betweenValue;  
  495.   
  496.         private boolean listValue;  
  497.   
  498.         public String getCondition() {  
  499.             return condition;  
  500.         }  
  501.   
  502.         public Object getValue() {  
  503.             return value;  
  504.         }  
  505.   
  506.         public Object getSecondValue() {  
  507.             return secondValue;  
  508.         }  
  509.   
  510.         public boolean isNoValue() {  
  511.             return noValue;  
  512.         }  
  513.   
  514.         public boolean isSingleValue() {  
  515.             return singleValue;  
  516.         }  
  517.   
  518.         public boolean isBetweenValue() {  
  519.             return betweenValue;  
  520.         }  
  521.   
  522.         public boolean isListValue() {  
  523.             return listValue;  
  524.         }  
  525.   
  526.         protected Criterion(String condition) {  
  527.             super();  
  528.             this.condition = condition;  
  529.             this.noValue = true;  
  530.         }  
  531.   
  532.         protected Criterion(String condition, Object value) {  
  533.             super();  
  534.             this.condition = condition;  
  535.             this.value = value;  
  536.             if (value instanceof List<?>) {  
  537.                 this.listValue = true;  
  538.             } else {  
  539.                 this.singleValue = true;  
  540.             }  
  541.         }  
  542.   
  543.         protected Criterion(String condition, Object value, Object secondValue) {  
  544.             super();  
  545.             this.condition = condition;  
  546.             this.value = value;  
  547.             this.secondValue = secondValue;  
  548.             this.betweenValue = true;  
  549.         }  
  550.     }  
  551. }  

2、dao層

這裏是使用Mybatis Generator自動生成的:UserDao.java

[java]  view plain  copy
  1. package com.lin.dao;  
  2.   
  3. import com.lin.domain.User;  
  4. import com.lin.domain.UserExample;  
  5. import java.util.List;  
  6. import org.apache.ibatis.annotations.Param;  
  7.   
  8. public interface UserDao {  
  9.     /** 
  10.      * This method was generated by MyBatis Generator. 
  11.      * This method corresponds to the database table t_user 
  12.      * 
  13.      * @mbggenerated 
  14.      */  
  15.     int countByExample(UserExample example);  
  16.   
  17.     /** 
  18.      * This method was generated by MyBatis Generator. 
  19.      * This method corresponds to the database table t_user 
  20.      * 
  21.      * @mbggenerated 
  22.      */  
  23.     int deleteByExample(UserExample example);  
  24.   
  25.     /** 
  26.      * This method was generated by MyBatis Generator. 
  27.      * This method corresponds to the database table t_user 
  28.      * 
  29.      * @mbggenerated 
  30.      */  
  31.     int deleteByPrimaryKey(Integer userId);  
  32.   
  33.     /** 
  34.      * This method was generated by MyBatis Generator. 
  35.      * This method corresponds to the database table t_user 
  36.      * 
  37.      * @mbggenerated 
  38.      */  
  39.     int insert(User record);  
  40.   
  41.     /** 
  42.      * This method was generated by MyBatis Generator. 
  43.      * This method corresponds to the database table t_user 
  44.      * 
  45.      * @mbggenerated 
  46.      */  
  47.     int insertSelective(User record);  
  48.   
  49.     /** 
  50.      * This method was generated by MyBatis Generator. 
  51.      * This method corresponds to the database table t_user 
  52.      * 
  53.      * @mbggenerated 
  54.      */  
  55.     List<User> selectByExample(UserExample example);  
  56.   
  57.     /** 
  58.      * This method was generated by MyBatis Generator. 
  59.      * This method corresponds to the database table t_user 
  60.      * 
  61.      * @mbggenerated 
  62.      */  
  63.     User selectByPrimaryKey(Integer userId);  
  64.   
  65.     /** 
  66.      * This method was generated by MyBatis Generator. 
  67.      * This method corresponds to the database table t_user 
  68.      * 
  69.      * @mbggenerated 
  70.      */  
  71.     int updateByExampleSelective(@Param("record") User record, @Param("example") UserExample example);  
  72.   
  73.     /** 
  74.      * This method was generated by MyBatis Generator. 
  75.      * This method corresponds to the database table t_user 
  76.      * 
  77.      * @mbggenerated 
  78.      */  
  79.     int updateByExample(@Param("record") User record, @Param("example") UserExample example);  
  80.   
  81.     /** 
  82.      * This method was generated by MyBatis Generator. 
  83.      * This method corresponds to the database table t_user 
  84.      * 
  85.      * @mbggenerated 
  86.      */  
  87.     int updateByPrimaryKeySelective(User record);  
  88.   
  89.     /** 
  90.      * This method was generated by MyBatis Generator. 
  91.      * This method corresponds to the database table t_user 
  92.      * 
  93.      * @mbggenerated 
  94.      */  
  95.     int updateByPrimaryKey(User record);  
  96. }  

3、service層

這裏就設計了兩個方法,一個查找和一個插入

接口類:

[java]  view plain  copy
  1. package com.lin.service;  
  2.   
  3. import com.lin.domain.User;  
  4. import com.lin.domain.UserExample;  
  5.   
  6. public interface IRegisterService {  
  7.       
  8.     public int insert(User record);  
  9.       
  10.     public int countByExample(UserExample example);  
  11.   
  12. }  

實現類:

[java]  view plain  copy
  1. package com.lin.service.impl;  
  2.   
  3. import javax.annotation.Resource;  
  4.   
  5. import org.apache.log4j.Logger;  
  6. import org.springframework.stereotype.Service;  
  7.   
  8. import com.lin.dao.UserDao;  
  9. import com.lin.domain.User;  
  10. import com.lin.domain.UserExample;  
  11. import com.lin.service.IRegisterService;  
  12. @Service("registerService")  
  13. public class RegisterServiceImpl implements IRegisterService{  
  14.     private static Logger logger = Logger.getLogger(RegisterServiceImpl.class);    
  15.     @Resource  
  16.     private UserDao userDao;  
  17.   
  18.     @Override  
  19.     public int insert(User record) {      
  20.         try {  
  21.             return userDao.insert(record);  
  22.         } catch (Exception e) {  
  23.             e.printStackTrace();  
  24.         }  
  25.         return 0;  
  26.     }  
  27.   
  28.     @Override  
  29.     public int countByExample(UserExample example) {  
  30.         try {  
  31.             return userDao.countByExample(example);  
  32.         } catch (Exception e) {  
  33.             e.printStackTrace();  
  34.         }  
  35.         return 0;  
  36.     }  
  37.   
  38. }  

2.2 配置文件


1、mappp文件配置

這裏是使用Mybatis Generator自動生成的:UserMapper.xml。放在src下面的com.lin.mapper包下

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  3. <mapper namespace="com.lin.dao.UserDao">  
  4.   <resultMap id="BaseResultMap" type="com.lin.domain.User">  
  5.     <!--  
  6.       WARNING - @mbggenerated  
  7.       This element is automatically generated by MyBatis Generator, do not modify.  
  8.     -->  
  9.     <id column="USER_ID" jdbcType="INTEGER" property="userId" />  
  10.     <result column="USER_NAME" jdbcType="CHAR" property="userName" />  
  11.     <result column="USER_PASSWORD" jdbcType="CHAR" property="userPassword" />  
  12.     <result column="USER_EMAIL" jdbcType="CHAR" property="userEmail" />  
  13.   </resultMap>  
  14.   <sql id="Example_Where_Clause">  
  15.     <!--  
  16.       WARNING - @mbggenerated  
  17.       This element is automatically generated by MyBatis Generator, do not modify.  
  18.     -->  
  19.     <where>  
  20.       <foreach collection="oredCriteria" item="criteria" separator="or">  
  21.         <if test="criteria.valid">  
  22.           <trim prefix="(" prefixOverrides="and" suffix=")">  
  23.             <foreach collection="criteria.criteria" item="criterion">  
  24.               <choose>  
  25.                 <when test="criterion.noValue">  
  26.                   and ${criterion.condition}  
  27.                 </when>  
  28.                 <when test="criterion.singleValue">  
  29.                   and ${criterion.condition} #{criterion.value}  
  30.                 </when>  
  31.                 <when test="criterion.betweenValue">  
  32.                   and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}  
  33.                 </when>  
  34.                 <when test="criterion.listValue">  
  35.                   and ${criterion.condition}  
  36.                   <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">  
  37.                     #{listItem}  
  38.                   </foreach>  
  39.                 </when>  
  40.               </choose>  
  41.             </foreach>  
  42.           </trim>  
  43.         </if>  
  44.       </foreach>  
  45.     </where>  
  46.   </sql>  
  47.   <sql id="Update_By_Example_Where_Clause">  
  48.     <!--  
  49.       WARNING - @mbggenerated  
  50.       This element is automatically generated by MyBatis Generator, do not modify.  
  51.     -->  
  52.     <where>  
  53.       <foreach collection="example.oredCriteria" item="criteria" separator="or">  
  54.         <if test="criteria.valid">  
  55.           <trim prefix="(" prefixOverrides="and" suffix=")">  
  56.             <foreach collection="criteria.criteria" item="criterion">  
  57.               <choose>  
  58.                 <when test="criterion.noValue">  
  59.                   and ${criterion.condition}  
  60.                 </when>  
  61.                 <when test="criterion.singleValue">  
  62.                   and ${criterion.condition} #{criterion.value}  
  63.                 </when>  
  64.                 <when test="criterion.betweenValue">  
  65.                   and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}  
  66.                 </when>  
  67.                 <when test="criterion.listValue">  
  68.                   and ${criterion.condition}  
  69.                   <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">  
  70.                     #{listItem}  
  71.                   </foreach>  
  72.                 </when>  
  73.               </choose>  
  74.             </foreach>  
  75.           </trim>  
  76.         </if>  
  77.       </foreach>  
  78.     </where>  
  79.   </sql>  
  80.   <sql id="Base_Column_List">  
  81.     <!--  
  82.       WARNING - @mbggenerated  
  83.       This element is automatically generated by MyBatis Generator, do not modify.  
  84.     -->  
  85.     USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL  
  86.   </sql>  
  87.   <select id="selectByExample" parameterType="com.lin.domain.UserExample" resultMap="BaseResultMap">  
  88.     <!--  
  89.       WARNING - @mbggenerated  
  90.       This element is automatically generated by MyBatis Generator, do not modify.  
  91.     -->  
  92.     select  
  93.     <if test="distinct">  
  94.       distinct  
  95.     </if>  
  96.     <include refid="Base_Column_List" />  
  97.     from t_user  
  98.     <if test="_parameter != null">  
  99.       <include refid="Example_Where_Clause" />  
  100.     </if>  
  101.     <if test="orderByClause != null">  
  102.       order by ${orderByClause}  
  103.     </if>  
  104.   </select>  
  105.   <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">  
  106.     <!--  
  107.       WARNING - @mbggenerated  
  108.       This element is automatically generated by MyBatis Generator, do not modify.  
  109.     -->  
  110.     select   
  111.     <include refid="Base_Column_List" />  
  112.     from t_user  
  113.     where USER_ID = #{userId,jdbcType=INTEGER}  
  114.   </select>  
  115.   <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">  
  116.     <!--  
  117.       WARNING - @mbggenerated  
  118.       This element is automatically generated by MyBatis Generator, do not modify.  
  119.     -->  
  120.     delete from t_user  
  121.     where USER_ID = #{userId,jdbcType=INTEGER}  
  122.   </delete>  
  123.   <delete id="deleteByExample" parameterType="com.lin.domain.UserExample">  
  124.     <!--  
  125.       WARNING - @mbggenerated  
  126.       This element is automatically generated by MyBatis Generator, do not modify.  
  127.     -->  
  128.     delete from t_user  
  129.     <if test="_parameter != null">  
  130.       <include refid="Example_Where_Clause" />  
  131.     </if>  
  132.   </delete>  
  133.   <insert id="insert" parameterType="com.lin.domain.User">  
  134.     <!--  
  135.       WARNING - @mbggenerated  
  136.       This element is automatically generated by MyBatis Generator, do not modify.  
  137.     -->  
  138.     insert into t_user (USER_ID, USER_NAME, USER_PASSWORD,   
  139.       USER_EMAIL)  
  140.     values (#{userId,jdbcType=INTEGER}, #{userName,jdbcType=CHAR}, #{userPassword,jdbcType=CHAR},   
  141.       #{userEmail,jdbcType=CHAR})  
  142.   </insert>  
  143.   <insert id="insertSelective" parameterType="com.lin.domain.User">  
  144.     <!--  
  145.       WARNING - @mbggenerated  
  146.       This element is automatically generated by MyBatis Generator, do not modify.  
  147.     -->  
  148.     insert into t_user  
  149.     <trim prefix="(" suffix=")" suffixOverrides=",">  
  150.       <if test="userId != null">  
  151.         USER_ID,  
  152.       </if>  
  153.       <if test="userName != null">  
  154.         USER_NAME,  
  155.       </if>  
  156.       <if test="userPassword != null">  
  157.         USER_PASSWORD,  
  158.       </if>  
  159.       <if test="userEmail != null">  
  160.         USER_EMAIL,  
  161.       </if>  
  162.     </trim>  
  163.     <trim prefix="values (" suffix=")" suffixOverrides=",">  
  164.       <if test="userId != null">  
  165.         #{userId,jdbcType=INTEGER},  
  166.       </if>  
  167.       <if test="userName != null">  
  168.         #{userName,jdbcType=CHAR},  
  169.       </if>  
  170.       <if test="userPassword != null">  
  171.         #{userPassword,jdbcType=CHAR},  
  172.       </if>  
  173.       <if test="userEmail != null">  
  174.         #{userEmail,jdbcType=CHAR},  
  175.       </if>  
  176.     </trim>  
  177.   </insert>  
  178.   <select id="countByExample" parameterType="com.lin.domain.UserExample" resultType="java.lang.Integer">  
  179.     <!--  
  180.       WARNING - @mbggenerated  
  181.       This element is automatically generated by MyBatis Generator, do not modify.  
  182.     -->  
  183.     select count(*) from t_user  
  184.     <if test="_parameter != null">  
  185.       <include refid="Example_Where_Clause" />  
  186.     </if>  
  187.   </select>  
  188.   <update id="updateByExampleSelective" parameterType="map">  
  189.     <!--  
  190.       WARNING - @mbggenerated  
  191.       This element is automatically generated by MyBatis Generator, do not modify.  
  192.     -->  
  193.     update t_user  
  194.     <set>  
  195.       <if test="record.userId != null">  
  196.         USER_ID = #{record.userId,jdbcType=INTEGER},  
  197.       </if>  
  198.       <if test="record.userName != null">  
  199.         USER_NAME = #{record.userName,jdbcType=CHAR},  
  200.       </if>  
  201.       <if test="record.userPassword != null">  
  202.         USER_PASSWORD = #{record.userPassword,jdbcType=CHAR},  
  203.       </if>  
  204.       <if test="record.userEmail != null">  
  205.         USER_EMAIL = #{record.userEmail,jdbcType=CHAR},  
  206.       </if>  
  207.     </set>  
  208.     <if test="_parameter != null">  
  209.       <include refid="Update_By_Example_Where_Clause" />  
  210.     </if>  
  211.   </update>  
  212.   <update id="updateByExample" parameterType="map">  
  213.     <!--  
  214.       WARNING - @mbggenerated  
  215.       This element is automatically generated by MyBatis Generator, do not modify.  
  216.     -->  
  217.     update t_user  
  218.     set USER_ID = #{record.userId,jdbcType=INTEGER},  
  219.       USER_NAME = #{record.userName,jdbcType=CHAR},  
  220.       USER_PASSWORD = #{record.userPassword,jdbcType=CHAR},  
  221.       USER_EMAIL = #{record.userEmail,jdbcType=CHAR}  
  222.     <if test="_parameter != null">  
  223.       <include refid="Update_By_Example_Where_Clause" />  
  224.     </if>  
  225.   </update>  
  226.   <update id="updateByPrimaryKeySelective" parameterType="com.lin.domain.User">  
  227.     <!--  
  228.       WARNING - @mbggenerated  
  229.       This element is automatically generated by MyBatis Generator, do not modify.  
  230.     -->  
  231.     update t_user  
  232.     <set>  
  233.       <if test="userName != null">  
  234.         USER_NAME = #{userName,jdbcType=CHAR},  
  235.       </if>  
  236.       <if test="userPassword != null">  
  237.         USER_PASSWORD = #{userPassword,jdbcType=CHAR},  
  238.       </if>  
  239.       <if test="userEmail != null">  
  240.         USER_EMAIL = #{userEmail,jdbcType=CHAR},  
  241.       </if>  
  242.     </set>  
  243.     where USER_ID = #{userId,jdbcType=INTEGER}  
  244.   </update>  
  245.   <update id="updateByPrimaryKey" parameterType="com.lin.domain.User">  
  246.     <!--  
  247.       WARNING - @mbggenerated  
  248.       This element is automatically generated by MyBatis Generator, do not modify.  
  249.     -->  
  250.     update t_user  
  251.     set USER_NAME = #{userName,jdbcType=CHAR},  
  252.       USER_PASSWORD = #{userPassword,jdbcType=CHAR},  
  253.       USER_EMAIL = #{userEmail,jdbcType=CHAR}  
  254.     where USER_ID = #{userId,jdbcType=INTEGER}  
  255.   </update>  
  256. </mapper>  

2、mybatis配置,沒有其實也行,因爲都 在spring中進行配置了,但是保留一下,以免要用,mybatis-config.xml放在config下面

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  
  3. "http://mybatis.org/dtd/mybatis-3-config.dtd">  
  4. <configuration>  
  5. </configuration>  

3、spingMVC配置

[plain]  view plain  copy
  1. <beans xmlns="http://www.springframework.org/schema/beans"    
  2.     xmlns:context="http://www.springframework.org/schema/context"    
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"    
  4.      xmlns:mvc="http://www.springframework.org/schema/mvc"      
  5.     xsi:schemaLocation="      
  6.         http://www.springframework.org/schema/mvc     
  7.         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd    
  8.         http://www.springframework.org/schema/beans           
  9.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd      
  10.         http://www.springframework.org/schema/mvc        
  11.         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd    
  12.         http://www.springframework.org/schema/context       
  13.         http://www.springframework.org/schema/context/spring-context-3.0.xsd">    
  14.     <!-- 把標記了@Controller註解的類轉換爲bean -->    
  15.     <context:component-scan base-package="com.lin.controller" />    
  16.     <!-- 啓動Spring MVC的註解功能,完成請求和註解POJO的映射 -->    
  17.     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />    
  18.    <!-- 靜態資源訪問(不攔截此目錄下的東西的訪問) -->      
  19.     <mvc:annotation-driven />      
  20.     <mvc:resources location="/WEB-INF//js/"  mapping="/js/**" />   
  21.     <!-- 對模型視圖名稱的解析,即在模型視圖名稱添加前後綴 -->    
  22.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"    
  23.         p:prefix="/WEB-INF/views/" p:suffix=".jsp"/>    
  24.   
  25.     
  26. </beans>  

4、spring配置:application.xml放在config下面

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xsi:schemaLocation="    
  6.            http://www.springframework.org/schema/beans    
  7.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
  8.            http://www.springframework.org/schema/aop    
  9.            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
  10.            http://www.springframework.org/schema/context    
  11.            http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  12.     <!-- 配置數據源 -->  
  13.     <bean id="dataSource"  
  14.         class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  15.         <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
  16.         <property name="url" value="jdbc:mysql://localhost:3306/learning" />  
  17.         <property name="username" value="root" />  
  18.         <property name="password" value="[email protected]" />  
  19.     </bean>  
  20.   
  21.     <!-- 自動掃描了所有的XxxxMapper.xml對應的mapper接口文件,這樣就不用一個一個手動配置Mpper的映射了,只要Mapper接口類和Mapper映射文件對應起來就可以了。 -->  
  22.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  23.         <property name="basePackage"  
  24.             value="com.lin.dao" />  
  25.     </bean>  
  26.   
  27. <!-- 配置Mybatis的文件 ,mapperLocations配置**Mapper.xml文件位置,configLocation配置mybatis-config文件位置-->  
  28.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  29.         <property name="dataSource" ref="dataSource" />  
  30.        <property name="mapperLocations" value="classpath*:com/lin/mapper/**/*.xml"/>    
  31.         <property name="configLocation" value="classpath:mybatis-config.xml" />  
  32.         <!-- <property name="typeAliasesPackage" value="com.tiantian.ckeditor.model"   
  33.             /> -->  
  34.     </bean>  
  35.   
  36.     <!-- 自動掃描註解的bean -->  
  37.     <context:component-scan base-package="com.lin.controller" />  
  38.         <context:component-scan base-package="com.lin.service.impl" />  
  39.   
  40. </beans>  

5、日誌打印配置:log4j.properties放在config下面

[plain]  view plain  copy
  1. log4j.rootLogger =DEBEG,stdout,debug  
  2.     
  3.     
  4. log4j.appender.stdout = org.apache.log4j.ConsoleAppender    
  5. log4j.appender.stdout.Target = System.out    
  6. log4j.appender.stdout.layout = org.apache.log4j.PatternLayout    
  7. log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n    
  8.     
  9. log4j.logger.java.sql.ResultSet=INFO  
  10. log4j.logger.org.apache=INFO  
  11. log4j.logger.java.sql.Connection=DEBUG  
  12. log4j.logger.java.sql.Statement=DEBUG  
  13. log4j.logger.java.sql.PreparedStatement=DEBUG   

6、web文件配置,放在WebContent下面

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  
  3.   <display-name>JsLearning3</display-name>  
  4.   <welcome-file-list>  
  5.     <welcome-file>index.html</welcome-file>  
  6.     <welcome-file>index.htm</welcome-file>  
  7.     <welcome-file>index.jsp</welcome-file>  
  8.     <welcome-file>default.html</welcome-file>  
  9.     <welcome-file>default.htm</welcome-file>  
  10.     <welcome-file>default.jsp</welcome-file>  
  11.   </welcome-file-list>  
  12.     
  13.         <!-- Spring 容器加載 -->    
  14.     <listener>    
  15.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    
  16.     </listener>    
  17.     <context-param>    
  18.         <param-name>contextConfigLocation</param-name>    
  19.         <param-value>classpath:application.xml</param-value>    
  20.     </context-param>   
  21.       
  22.     <!-- 統一設置編碼,防止出現中文亂碼 -->    
  23.     <filter>  
  24.         <filter-name>Set Character Encoding</filter-name>  
  25.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  26.         <init-param>  
  27.             <param-name>encoding</param-name>  
  28.             <param-value>UTF-8</param-value>  
  29.         </init-param>  
  30.     </filter>  
  31.     <filter-mapping>  
  32.         <filter-name>Set Character Encoding</filter-name>  
  33.         <url-pattern>/*</url-pattern>  
  34.     </filter-mapping>  
  35.     
  36.    <!-- SpringMVC的前端控制器 -->    
  37.     <servlet>    
  38.         <servlet-name>dispatcherServlet</servlet-name>    
  39.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    
  40.         <!-- 設置自己定義的控制器xml文件 -->    
  41.         <init-param>    
  42.             <param-name>contextConfigLocation</param-name>    
  43.             <param-value>classpath*:spring-servlet.xml</param-value>    
  44.         </init-param>    
  45.         <load-on-startup>1</load-on-startup>    
  46.     </servlet>    
  47.     <!-- Spring MVC配置文件結束 -->    
  48.     
  49.     <!-- 攔截設置 -->    
  50.     <servlet-mapping>    
  51.         <servlet-name>dispatcherServlet</servlet-name>    
  52.         <!-- 由SpringMVC攔截所有請求 -->    
  53.         <url-pattern>/</url-pattern>    
  54.     </servlet-mapping>    
  55.       
  56.     <!-- webAppRootKey:值缺省爲webapp.root,當tomcat下部署多個應用時(每個都用到了log4j), 每個應用的web.xml中都要配置該參數,該參數與Log4j.xml文件中的${webapp.root}  
  57.         否則每個應用的webAppRootKey值都相同,就會引起衝突  
  58.      -->  
  59.     <context-param>  
  60.         <param-name>webAppRootKey</param-name>  
  61.         <param-value>webApp.root</param-value>  
  62.     </context-param>  
  63.     <!-- log4jConfigLocation:log4j配置文件存放路徑 -->  
  64.     <context-param>  
  65.         <param-name>log4jConfigLocation</param-name>  
  66.         <param-value>classpath:log4j.properties</param-value>  
  67.     </context-param>  
  68.     <!-- 3000表示 開一條watchdog線程每60秒掃描一下配置文件的變化;這樣便於日誌存放位置的改變 -->    
  69.     <context-param>      
  70.          <param-name>log4jRefreshInterval</param-name>      
  71.          <param-value>3000</param-value>      
  72.     </context-param>     
  73.     <listener>  
  74.         <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
  75.     </listener>  
  76. </web-app>  

三、web頁面編寫



1、使用到的js編程

[javascript]  view plain  copy
  1. var process_request = "<img src='loading.gif' width='16' height='16' border='0' align='absmiddle'>正在數據處理中...";    
  2. var username_empty = "<span style='COLOR:#ff0000'>  × 用戶名不能爲空!</span>";    
  3. var username_shorter = "<span style='COLOR:#ff0000'> × 用戶名長度不能少於 3 個字符。</span>";    
  4. var username_longer = "<span style='COLOR:#ff0000'> × 用戶名長度不能大於 30個字符。</span>";    
  5. var username_invalid = "- 用戶名只能是由字母數字以及下劃線組成。";    
  6. var username_have_register = "<span style='COLOR:#ff0000'> × 用戶名已經存在,請重新輸入!</span>";    
  7. var username_can_register="<span style='COLOR:#006600'> √ 恭喜您!該用戶名可以註冊!</span>";    
  8. var password_empty = "<span style='COLOR:#ff0000'> × 登錄密碼不能爲空。</span>";    
  9. var password_shorter_s = "<span style='COLOR:#ff0000'> × 登錄密碼不能少於 6 個字符。</span>";    
  10. var password_shorter_m = "<span style='COLOR:#ff0000'> × 登錄密碼不能多於 30 個字符。</span>";    
  11. var confirm_password_invalid = "<span style='COLOR:#ff0000'> × 兩次輸入密碼不一致!</span>";    
  12. var email_empty = "<span style='COLOR:#ff0000'> × 郵箱不能爲空!</span>";    
  13. var email_invalid = "<span style='COLOR:#ff0000'> × 郵箱格式出錯!</span>";    
  14. var email_have_register = "<span style='COLOR:#ff0000'> × 該郵箱已被註冊! </span>";    
  15. var email_can_register = "<span style='COLOR:#006600'> √ 郵箱可以註冊!</span>";    
  16. var agreement_no = "<span style='COLOR:#ff0000'> × 您沒有接受協議</span>";    
  17. var agreement_yes= "<span style='COLOR:#006600'> √ 已經接受協議</span>";    
  18. var info_can="<span style='COLOR:#006600'> √ 可以註冊!</span>";    
  19. var info_right="<span style='COLOR:#006600'> √ 填寫正確!</span>";   
  20. var name_flag=false;  
  21. var email_flag=false;  
  22. var password_flag=false;  
  23. var accept_flag=false;  
  24.   
  25. $(function(){  
  26.     change_submit();      
  27.     if(document.getElementById("agreement").checked){  
  28.         alert("checkbox is checked");  
  29.     }  
  30. });   
  31.   
  32. /* 
  33.  * 獲取工程的路徑 
  34.  */  
  35. function getRootPath() {  
  36.     var pathName = window.location.pathname.substring(1);  
  37.     var webName = pathName == '' ? '' : pathName.substring(0, pathName  
  38.             .indexOf('/'));  
  39.     return window.location.protocol + '//' + window.location.host + '/'  
  40.             + webName + '/';  
  41. }  
  42. /* 
  43.  * 用戶名檢測 
  44.  */  
  45. function checkUserName(obj) {  
  46.     if (checks(obj.value) == false) {  
  47.         showInfo("username_notice", username_invalid);  
  48.     } else if (obj.value.length < 1) {  
  49.         showInfo("username_notice", username_empty);  
  50.     }else if (obj.value.length < 3) {  
  51.         showInfo("username_notice", username_shorter);  
  52.     } else if(obj.value.length>30){  
  53.         showInfo("username_notice", username_longer);  
  54.     }else {       
  55.         // 調用Ajax函數,向服務器端發送查詢  
  56.         $.ajax({ //一個Ajax過程  
  57.             type: "post"//以post方式與後臺溝通  
  58.             url :getRootPath()+"/register/checkUserName"//與此頁面溝通  
  59.             dataType:'json',//返回的值以 JSON方式 解釋  
  60.             data: 'userName='+obj.value, //發給的數據  
  61.             success: function(json){//如果調用成功  
  62.                 if(json.flag){  
  63.                     showInfo("username_notice", username_have_register);  
  64.                 }else {  
  65.                     showInfo("username_notice", username_can_register);  
  66.                     name_flag=true;  
  67.                     change_submit();  
  68.                     return;  
  69.                 }  
  70.             }     
  71.     });   
  72.     }  
  73.     name_flag=false;  
  74.     change_submit();  
  75. }    
  76. /* 
  77.  * 用戶名檢測是否包含非法字符 
  78.  */  
  79. function checks(t) {  
  80.     szMsg = "[#%&\'\"\\,;:=!^@]"  
  81.     for (i = 1; i < szMsg.length + 1; i++) {  
  82.         if (t.indexOf(szMsg.substring(i - 1, i)) > -1) {  
  83.             return false;  
  84.         }  
  85.     }  
  86.     return true;  
  87. }  
  88. /* 
  89.  * 郵箱檢測 
  90.  */  
  91.  function checkEmail(email) {  
  92.     var re = /^(\w-*\.*)[email protected](\w-?)+(\.\w{2,})+$/  
  93.     if (email.value.length < 1) {  
  94.         showInfo("email_notice", email_empty);  
  95.     } else if (!re.test(email.value)) {  
  96.         email.className = "FrameDivWarn";  
  97.         showInfo("email_notice", email_invalid);          
  98.     } else {  
  99.         // 調用Ajax函數,向服務器端發送查詢  
  100.        $.ajax({ //一個Ajax過程  
  101.             type: "post"//以post方式與後臺溝通  
  102.             url :getRootPath()+"/register/checkEmail"//與此頁面溝通  
  103.             dataType:'json',//返回的值以 JSON方式 解釋  
  104.             data: 'email='+email.value, //發給的數據  
  105.             success: function(json){//如果調用成功  
  106.                 if(json.flag){  
相關文章
相關標籤/搜索