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

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

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

    專注成就輝煌

    專注java

    SpringMvc&Maven初級篇(二)用戶注冊(帶驗證)

    項目結構圖:


    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>
    <!-- 字符過濾_防止添加到數據庫中的數據為亂碼 -->
    <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>確認密碼:<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; // 用戶自定義驗證


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


    @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"; //驗證不通過,跳轉回添加用戶界面
    }

    return "user/addSuccess"; //驗證通過,跳轉到添加成功界面
    }

    }

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

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

    評論

    # re: SpringMvc&Maven初級篇(二)用戶注冊(帶驗證) 2015-09-29 23:54 424、、、

    切爾奇  回復  更多評論   

    # re: SpringMvc&Maven初級篇(二)用戶注冊(帶驗證) 2015-09-29 23:55 424、、、

    不好意思,我以為是上面代碼的實例,實在是對不起了  回復  更多評論   

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

    1  回復  更多評論   

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

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

    # re: SpringMvc&Maven初級篇(二)用戶注冊(帶驗證) 2016-04-06 18:30 ss

    aaaaaa  回復  更多評論   


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 日本视频免费观看| 4399好看日本在线电影免费| 亚洲av永久无码精品网站 | 亚洲一区二区三区成人网站| 国产在线观看免费不卡| 久久免费精品视频| 亚洲国产高清在线精品一区| 四虎永久在线精品免费观看地址 | 日韩精品视频免费观看| 中文字字幕在线高清免费电影| 亚洲熟妇无码久久精品| 免费少妇a级毛片人成网| 99精品视频在线观看免费专区 | 亚洲av无码久久忘忧草| 五月天婷亚洲天综合网精品偷| 色欲色香天天天综合网站免费| 亚洲GV天堂GV无码男同| 亚洲日本中文字幕区| 国产一级淫片免费播放| 91精品国产免费| 日韩亚洲国产综合久久久| 中文字幕免费视频一| 一区二区三区免费在线视频 | 亚洲精品福利你懂| 亚洲国产日韩在线视频| 日韩一区二区免费视频| 99精品免费观看| japanese色国产在线看免费| 国产亚洲精aa成人网站| 精品熟女少妇AV免费观看| 波多野结衣免费一区视频| 男男gvh肉在线观看免费| 亚洲国产成人精品青青草原| 亚洲真人无码永久在线| 国产成人涩涩涩视频在线观看免费| 中文字幕免费视频一| 你好老叔电影观看免费| 黄色毛片视频免费| 亚洲欧美中文日韩视频| 亚洲日韩国产成网在线观看| 在线免费一区二区|