<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    專注成就輝煌

    專注java

    SpringMvc&Maven初級(jí)篇(二)用戶注冊(cè)(帶驗(yàn)證)

    項(xiàng)目結(jié)構(gòu)圖:


    pom.xml
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      
    <modelVersion>4.0.0</modelVersion>
      
    <groupId>userapps</groupId>
      
    <artifactId>userapps</artifactId>
      
    <version>0.0.1-SNAPSHOT</version>
      
    <packaging>war</packaging>
      
    <name>userapps</name>
      
    <description>userapps</description>
      
      
    <properties>
          
    <spring.version>3.0.5.RELEASE</spring.version>
      
    </properties>
      
      
    <dependencies>
          
    <dependency>
             
    <groupId>org.springframework</groupId>
            
    <artifactId>spring-core</artifactId>
            
    <version>${spring.version}</version>
          
    </dependency>
          
    <dependency>
             
    <groupId>org.springframework</groupId>
            
    <artifactId>spring-webmvc</artifactId>
            
    <version>${spring.version}</version>
          
    </dependency>
          
    <dependency>
             
    <groupId>org.springframework</groupId>
            
    <artifactId>spring-beans</artifactId>
            
    <version>${spring.version}</version>
          
    </dependency>
          
    <dependency>
             
    <groupId>org.springframework</groupId>
            
    <artifactId>spring-context</artifactId>
            
    <version>${spring.version}</version>
          
    </dependency>
          
    <dependency>
             
    <groupId>org.springframework</groupId>
            
    <artifactId>spring-aop</artifactId>
            
    <version>${spring.version}</version>
          
    </dependency>
          
    <dependency>
             
    <groupId>org.springframework</groupId>
            
    <artifactId>spring-tx</artifactId>
            
    <version>${spring.version}</version>
          
    </dependency>
          
          
    <dependency>
            
    <groupId>org.hibernate</groupId>
            
    <artifactId>hibernate-validator</artifactId>
            
    <version>4.0.2.GA</version>
        
    </dependency>
          
          
         
    <!-- Other dependencies -->
        
    <dependency>
            
    <groupId>commons-logging</groupId>
            
    <artifactId>commons-logging</artifactId>
            
    <version>1.1.1</version>
        
    </dependency>
        
    <dependency>
            
    <groupId>javax.servlet</groupId>
            
    <artifactId>servlet-api</artifactId>
            
    <version>2.5</version>
        
    </dependency>
        
    <dependency>
            
    <groupId>junit</groupId>
            
    <artifactId>junit</artifactId>
            
    <version>4.8.1</version>
        
    </dependency>
      
    </dependencies>
      
    </project>

    index.jsp
    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding
    ="UTF-8"
    %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <a href="toAddUserPage.html">添加用戶信息</a>
    </body>
    </html>

    web.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns
    ="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    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">
    <display-name>userapps</display-name>
    <!-- 字符過(guò)濾_防止添加到數(shù)據(jù)庫(kù)中的數(shù)據(jù)為亂碼 -->
    <filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
    <param-name>encoding</param-name>
    <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
    <param-name>forceEncoding</param-name>
    <param-value>true</param-value>
    </init-param>
    </filter>
    <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>

    <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.html</url-pattern>
    </servlet-mapping>

    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext*.xml</param-value>
    </context-param>

    <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    </web-app>

    dispatcher-servlet.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi
    ="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context
    ="http://www.springframework.org/schema/context"
    xmlns:mvc
    ="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation
    ="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    "
    >

    <!-- 搜索的控制類路徑(C) -->
    <context:component-scan base-package="com.userapps" />

    <!-- 配置視圖路徑(V) -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
    </bean>

    <!-- 異常解析器 -->
    <bean id="simpleMappingExceptionResolver"
    class
    ="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="exceptionMappings">
    <props>
    <prop
    key="org.springframework.web.multipart.MaxUploadSizeExceededException">common/fileerror</prop>
    </props>
    </property>
    </bean>
    </beans>

    add.jsp
    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding
    ="UTF-8"
    %>
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>   
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>添加用戶</title>
    </head>
    <body>
    <form:form method="Post" action="adduser.html" commandName="user">
    <table>
    <tr>
    <td>用戶名:<FONT color="red"><form:errors
    path="userName" /></FONT></td>
    </tr>
    <tr>
    <td><form:input path="userName" /></td>
    </tr>

    <tr>
    <td>密碼:<FONT color="red"><form:errors
    path="password" /></FONT></td>
    </tr>
    <tr>
    <td><form:password path="password" /></td>
    </tr>

    <tr>
    <td>確認(rèn)密碼:<FONT color="red"><form:errors
    path="confirmPassword" /></FONT></td>
    </tr>
    <tr>
    <td><form:password path="confirmPassword" /></td>
    </tr>

    <tr>
    <td>Email:<FONT color="red"><form:errors path="email" /></FONT></td>
    </tr>
    <tr>
    <td><form:input path="email" /></td>
    </tr>
    <tr>
    <td><input type="submit" value="提交" /></td>
    </tr>
    </table>
    </form:form>
    </body>
    </html>

    addSuccess.jsp
    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding
    ="UTF-8"
    %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>success page</title>
    </head>
    <body>
    Congratulate,add Success!
    </body>
    </html>

    User.java
    package com.userapps.user.form;

    import javax.validation.constraints.Size;
    import org.hibernate.validator.constraints.Email;
    import org.hibernate.validator.constraints.NotEmpty;

    public class User {
    private String userName;
    @NotEmpty
    @Size(min
    = 4, max = 20)
    private String password;
    @NotEmpty
    private String confirmPassword;
    @NotEmpty
    @Email
    private String email;

    public void setUserName(String userName) {
    this.userName = userName;
    }


    public String getUserName() {
    return userName;
    }


    public void setPassword(String password) {
    this.password = password;
    }


    public String getPassword() {
    return password;
    }


    public void setConfirmPassword(String confirmPassword) {
    this.confirmPassword = confirmPassword;
    }


    public String getConfirmPassword() {
    return confirmPassword;
    }


    public void setEmail(String email) {
    this.email = email;
    }


    public String getEmail() {
    return email;
    }

    }


    UserValidation.java
    package com.userapps.user.controllers;

    import org.springframework.stereotype.Component;
    import org.springframework.validation.Errors;
    import org.springframework.validation.ValidationUtils;

    import com.userapps.user.form.User;

    @Component(
    "userValidator")
    public class UserValidation {
    public boolean supports(Class<?> klass) {
    return User.class.isAssignableFrom(klass);
    }


    public void validate(Object target, Errors errors) {
    User registration
    = (User) target;
    ValidationUtils.rejectIfEmptyOrWhitespace(errors,
    "userName",
    "NotEmpty.registration.userName",
    "用戶名不能為空.");
    String userName
    = registration.getUserName();
    if ((userName.length()) > 50) {
    errors.rejectValue(
    "userName",
    "lengthOfUser.registration.userName",
    "User Name must not more than 50 characters.");
    }

    if (!(registration.getPassword()).equals(registration
    .getConfirmPassword()))
    {
    errors.rejectValue(
    "password",
    "matchingPassword.registration.password",
    "Password and Confirm Password Not match.");
    }

    }

    }


    UserManagerController.java
    package com.userapps.user.controllers;

    import java.util.Map;

    import javax.validation.Valid;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.validation.BindingResult;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;

    import com.userapps.user.form.User;

    @Controller
    public class UserManagerController {

    @Autowired
    private UserValidation userValidation; // 用戶自定義驗(yàn)證


    @RequestMapping(value
    ="/toAddUserPage")
    public String toAddUserPage(Map<String, User> model) {
    User user
    = new User();
    model.put(
    "user", user);
    return "user/add"; // 跳轉(zhuǎn)到添加用戶界面
    }


    @RequestMapping(value
    ="/adduser", method = RequestMethod.POST)
    public String processRegistration(@Valid User user,
    BindingResult result)
    {
    // set custom Validation by user
    userValidation.validate(user, result);
    if (result.hasErrors()) {
    return "user/add"; //驗(yàn)證不通過(guò),跳轉(zhuǎn)回添加用戶界面
    }

    return "user/addSuccess"; //驗(yàn)證通過(guò),跳轉(zhuǎn)到添加成功界面
    }

    }

    附上源碼:/Files/svygh123/userapps.rar

    posted on 2012-06-04 00:12 一江東水 閱讀(6265) 評(píng)論(5)  編輯  收藏

    評(píng)論

    # re: SpringMvc&Maven初級(jí)篇(二)用戶注冊(cè)(帶驗(yàn)證) 2015-09-29 23:54 424、、、

    切爾奇  回復(fù)  更多評(píng)論   

    # re: SpringMvc&Maven初級(jí)篇(二)用戶注冊(cè)(帶驗(yàn)證) 2015-09-29 23:55 424、、、

    不好意思,我以為是上面代碼的實(shí)例,實(shí)在是對(duì)不起了  回復(fù)  更多評(píng)論   

    # re: SpringMvc&Maven初級(jí)篇(二)用戶注冊(cè)[未登錄](méi) 2016-01-21 13:46 1

    1  回復(fù)  更多評(píng)論   

    # re: SpringMvc&Maven初級(jí)篇(二)用戶注冊(cè)[未登錄](méi) 2016-01-21 13:49 1

    你不把添加成功后的圖片 上傳到上面,這樣我們就可以看到完整的 添加注冊(cè)了。  回復(fù)  更多評(píng)論   

    # re: SpringMvc&Maven初級(jí)篇(二)用戶注冊(cè)(帶驗(yàn)證) 2016-04-06 18:30 ss

    aaaaaa  回復(fù)  更多評(píng)論   


    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 一级成人生活片免费看| 亚洲国产精品丝袜在线观看| 国产亚洲高清不卡在线观看| 日韩a毛片免费观看| 亚洲国产人成精品| 日韩免费高清一级毛片| 自拍偷自拍亚洲精品第1页| a级毛片免费观看视频| 精品日韩亚洲AV无码| 毛片基地免费视频a| 四虎精品免费永久免费视频| 国产亚洲综合色就色| 女性自慰aⅴ片高清免费| 一级中文字幕乱码免费| 亚洲天天在线日亚洲洲精| 免费看无码自慰一区二区| mm1313亚洲精品国产| 亚洲国产精品成人综合色在线| 国产高清免费观看| 国产福利在线观看永久免费| 亚洲国产精品福利片在线观看| 亚洲精品视频免费看| 亚洲AV无码专区在线电影成人| 在线亚洲精品自拍| 国内精自视频品线六区免费| 国产区图片区小说区亚洲区| 亚洲色爱图小说专区| 6080午夜一级毛片免费看 | 国产免费区在线观看十分钟| 国产啪精品视频网免费| 三级片免费观看久久| 国产精品亚洲片在线观看不卡 | 国产精品1024永久免费视频| 亚洲国产美女精品久久久| 亚洲AV无码一区二区三区DV| 午夜高清免费在线观看| 成在人线av无码免费高潮喷水| 亚洲精品无码人妻无码| 久久精品国产亚洲av麻豆小说| 亚洲AV无码乱码在线观看性色扶 | 日本一道在线日本一道高清不卡免费|