Use spring MVC to implement a complete instance for user login. In this example, create a tomcat project in eclipse to explain the whole process of spring MVC. The instance code is as follows:
<I> write a diary file under mymvc/WEB-INF/src
# Specify the size of the log input file
Log4j. appender. stdout. maxfilesize = 500kb
Log4j. appender. stdout. maxbackupindex = 50
Log4j. appender. stdout. append = true
Log4j. appender. stdout. layout = org. Apache. log4j. patternlayout
Log4j. appender. stdout. layout. conversionpattern = [%-5 p] % d {yyyy-mm-DDHH: mm: SS} % C-% m % N
# Define how log4j is displayed
Log4j. appender. A1 = org. Apache. log4j. rollingfileappender
# Specify the name of the log input file
Log4j. appender. a1.file = org. Log
# Specify the size of the log input file
Log4j. appender. a1.maxfilesize = 500kb
Log4j. appender. a1.maxbackupindex = 50
Log4j. appender. a1.append = true
Log4j. appender. a1.layout = org. Apache. log4j. patternlayout
Log4j. appender. a1.layout. conversionpattern = % d {iso8601}-[% P] [% c {1}]-% m % N
# Define how log4j is displayed
Log4j. appender. A1 = org. Apache. log4j. rollingfileappender
# Specify the name of the log input file
Log4j. appender. a1.file = GC. Log
# Specify the size of the log input file
Log4j. appender. gc. maxfilesize = 500kb
Log4j. appender. gc. maxbackupindex = 50
Log4j. appender. gc. append = true
Log4j. appender. gc. layout = org. Apache. log4j. patternlayout
Log4j. appender. gc. layout. conversionpattern = % d {iso8601}-[% P] [% c {1}]-% m % N
<2> Configure web. xml
<? XML version = "1.0" encoding = "UTF-8"?>
<Web-app xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Version = "2.4"
Xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee
Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd>
<Servlet>
<Servlet-Name> dispatcherservlet </servlet-Name>
<Servlet-class> org. springframework. Web. servlet. dispatcherservlet </servlet-class>
<! -- Initialize parameters -->
<Init-param>
<Param-Name> contextconfiglocation </param-Name>
<Param-value>/WEB-INF/dispatcherServlet-servlet.xml </param-value>
</Init-param>
</Servlet>
<Servlet-mapping>
<Servlet-Name> dispatcherservlet </servlet-Name>
<URL-pattern> *. DO </url-pattern>
</Servlet-mapping>
<Taglib>
<Taglib-Uri>/spring </taglib-Uri>
<Taglib-location>/WEB-INF/spring. TLD </taglib-location>
</Taglib>
<Welcome-file-List>
<Welcome-File> index. jsp </welcome-File>
</Welcome-file-List>
</Web-app>
<3> compile login. jsp in the mymvc/GC/jsp directory on the logon page.
<% @ Taglib prefix = "Spring" uri = "/Spring" %>
<% @ Page contenttype = "text/html; charset = GBK" %>
<HTML>
<Head> <title> <spring: Message code = "title"/> </title>
<Body>
<Form action = "/mymvc/login. Do" name = "user" method = "Post">
<Spring: bind Path = "command. username">
<Spring: Message code = "username"> </spring: Message>
<Input type = "text" name = "$ {status. Expression}" value = "$ {status. Value}"/>
<Br>
<Font color = "red"> <B >$ {status. errormessage} </B> </font> <br>
</Spring: bind>
<Spring: bind Path = "command. Password">
<Spring: Message code = "password"> </spring: Message>
<Input type = "password" name = "$ {status. Expression}" value = "$ {status. Value}"/>
<Br>
<Font color = "red"> <B >$ {status. errormessage} </B> </font> <br>
</Spring: bind>
<Spring: bind Path = "command. password2">
<Spring: Message code = "password2"> </spring: Message>
<Input type = "password" name = "$ {status. Expression}" value = "$ {status. Value}"/>
<Br>
<Font color = "red"> <B >$ {status. errormessage} </B> </font> <br>
</Spring: bind>
<Input type = "Submit" value = "<spring: messagecode = 'submit '/>"/>
</Form>
</Body>
</Html>
<4> write success. jsp in mymvc/GC/jsp
<% @ Taglib prefix = "Spring" uri = "/Spring" %>
<% @ Page contenttype = "text/html; charset = GBK" %>
<HTML>
<Head> <title> <spring: Message code = "title"> </spring: Message> </title>
<Body>
<Spring: bind Path = "command. username">
<H3> <spring: Message code = "welcome"> </spring: Message> {status. value} <spring: Message code = "loginsuccess"/>
</Spring: bind>
</Body>
</Html>
<5> compile the bean that stores user login information and create the Java file user. Java in the com. gc. action package.
Package com. gc. Action;
Publicclass user {
Private string username = NULL;
Private string Password = NULL;
Private string password2 = NULL;
Public String GetPassword (){
Returnpassword;
}
Publicvoid setpassword (string password ){
This. Password = password;
}
Public String getpassword2 (){
Returnpassword2;
}
Publicvoid setpassword2 (string password2 ){
This. password2 = password2;
}
Public String GetUserName (){
Returnusername;
}
Publicvoid setusername (string username ){
This. Username = username;
}
}
<6> write user verification class uservalidator. Java under the com. gc. action package
Package com. gc. Action;
Import com. gc. Action. user;
Import org. springframework. validation. validator;
Import org. springframework. validation. errors;
Publicclass uservalidator implements validator {
Publicboolean supports (class clazz ){
Return clazz. Equals (user. Class );
}
Publicvoid validate (Object OBJ, errors ){
User user = (User) OBJ;
If (! "Tang". Equals (user. GetUserName ())){
Errors. rejectvalue ("username", "usernameerror", null, "incorrect user name ");
}
If (! & Quot; 123456 & quot;. Equals (user. GetPassword ())){
Errors. rejectvalue ("password", "passworderror", null, "Incorrect password ");
}
If (! User. GetPassword (). Equals (user. getpassword2 ())){
Errors. rejectvalue ("password2", "password2error", null, "two different passwords ");
}
}
}
<7> write the user logon logic login. Java in the com. gc. action package.
Package com. gc. Action;
Import java. util. Map;
Import org. Apache. log4j. Logger;
Import org. springframework. validation. bindexception;
Import org. springframework. Web. servlet. modelandview;
Import org. springframework. Web. servlet. MVC. simpleformcontroller;
Publicclass login extends simpleformcontroller {
Private logger = logger. getlogger (this. getclass (). getname ());
Public modelandview onsubmit (Object command, bindexception errors) throws exception {
User user = (User) command;
Map modle = errors. GetModel ();
Modle. Put ("user", user );
Returnnew modelandview (getsuccessview (), modle );
}
}
<8> write configuration file WEB-INF under mymvc/dispatcherServlet-servlet.xml directory
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype beans public "-// spring // DTD bean // en"
"Http: www.springframework.org/dtd/spring-bean.dtd">
<Beans>
<! -- Define international messages -->
<Bean id = "messagesource" class = "org. springframework. Context. Support. resourcebundlemessagesource">
<Property name = "basename">
<Value> messages </value>
</Property>
</Bean>
<! -- Define ing -->
<Bean id = "urlmapping" class = "org. springframework. Web. servlet. handler. simpleurlhandlermapping">
<Property name = "mapping">
<Props>
<Prop key = "login. Do"> login </prop>
</Props>
</Property>
</Bean>
<! -- Define view and path -->
<Bean id = "viewresolver" class = "org. springframework. Web. servlet. View. internalresourceviewresolver">
<Property name = "viewclass">
<Value> org. springframework. Web. servlet. View. internalresourceview </value>
</Property>
<! -- JSP are placed in the WEB-INF/jsp directory -->
<Property name = "prefix">
<Value>/WEB-INF/jsp </value>
</Property>
<! -- JSP page suffixes are all four. jsp -->
<Property name = "suffix">
<Value>. jsp </value>
</Property>
</Bean>
<! -- Define Controller -->
<Bean id = "login" class = "com. gc. Action. login">
<Property name = "commandclass">
<Value> com. gc. Action. User </value>
</Property>
<! -- Specify the verification class -->
<Property name = "validator">
<Ref bean = "uservalidator"/>
</Property>
<! -- Specify the page to be returned for failure -->
<Property name = "formview">
<Value> login </value>
</Property>
<Property name = "successview">
<Value> success </value>
</Property>
</Bean>
<! -- Define the verification class -->
<Bean id = "uservalidator" class = "com. gc. Action. uservalidator"> </bean>
</Beans>
<9> write the resource file messages. properties and messages_en_us.properties in the mymvc/WEB-INF/src directory.
Messages. Properties
Title = springmvc instance
Username = enter the User Name:
Password = enter the password:
Password2 = Confirm Password:
Submit = submit
Welcome = welcome
Loginsuccess = logon successful
Usernameerror = incorrect user name
Passworderror = incorrect password
Doublepassword = two different passwords
After native2ascii tool Transcoding
Title = springmvc/u5b9e/u4f8b
Username =/u8f93/u5165/u7528/u6237/u540d:
Password =/u8f93/u5165/u5bc6/u7801:
Password2 =/u786e/u8ba4/u5bc6/u7801:
Submit =/u63d0/u4ea4
Welcome =/u6b22/u8fce
Loginsuccess =/u767b/u5f55/u6210/u529f
Usernameerror =/u7528/u6237/u540d/u4e0d/u6b63/u786e
Passworderror =/u5bc6/u7801/u4e0d/u6b63/u786e
Doublepassword = 2/u6b21/u5bc6/u7801/u4e0d/u4e00/u81f4
Messages_en_us.properties
Title = springmvc
Username = Username
Password = Password
Password2 = password2
Submit = submit
Welcome = welcome
Loginsuccess = loginsuccess
Usernameerror = usernameerror
Passworderror = passworderror
Doublepassword = doublepassworderror
This article from: http://fangyong2006.javaeye.com/blog/117563