SpringMVC Demo
<一>编写日记文件放在myMVC/WEB-INF/src下
#指定日志输入文件的大小
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=[%-5p]%d{yyyy-MM-ddHH:mm:ss}%c-%m%n
#定义log4j的显示方式
log4j.appender.A1=org.apache.log4j.RollingFileAppender
#指定日志输入文件的名称
log4j.appender.A1.File=org.log
#指定日志输入文件的大小
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
#定义log4j的显示方式
log4j.appender.A1=org.apache.log4j.RollingFileAppender
#指定日志输入文件的名称
log4j.appender.A1.File=gc.log
#指定日志输入文件的大小
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
<二>配置web.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appxmlns="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>
<!--初始化参数-->
<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>
<三>编写登录页面myMVC/gc/jsp目录下login.jsp
<%@taglibprefix="spring"uri="/spring"%>
<%@pagecontentType="text/html;charset=GBK"%>
<html>
<head><title><spring:messagecode="title"/></title></head>
<body>
<formaction="/myMVC/login.do"name="User"method="post">
<spring:bindpath="command.username">
<spring:messagecode="username"></spring:message>
<inputtype="text"name="${status.expression}"value="${status.value}"/>
<br>
<fontcolor="red"><b>${status.errorMessage}</b></font><br>
</spring:bind>
<spring:bindpath="command.password">
<spring:messagecode="password"></spring:message>
<inputtype="password"name="${status.expression}"value="${status.value}"/>
<br>
<fontcolor="red"><b>${status.errorMessage}</b></font><br>
</spring:bind>
<spring:bindpath="command.password2">
<spring:messagecode="password2"></spring:message>
<inputtype="password"name="${status.expression}"value="${status.value}"/>
<br>
<fontcolor="red"><b>${status.errorMessage}</b></font><br>
</spring:bind>
<inputtype="submit"value="<spring:messagecode='submit'/>"/>
</form>
</body>
</html>
<四>编写显示成功的页面myMVC/gc/jsp目录下success.jsp
<%@taglibprefix="spring"uri="/spring"%>
<%@pagecontentType="text/html;charset=GBK"%>
<html>
<head><title><spring:messagecode="title"></spring:message></title></head>
<body>
<spring:bindpath="command.username">
<h3><spring:messagecode="welcome"></spring:message>{status.value}<spring:messagecode="loginSuccess"/></h3><br>
</spring:bind>
</body>
</html>
<五>编写存放用户登录信息的Bean,在com.gc.action包中建立Java文件User.java
packagecom.gc.action;
publicclassUser{
privateStringusername=null;
privateStringpassword=null;
privateStringpassword2=null;
publicStringgetPassword(){
returnpassword;
}
publicvoidsetPassword(Stringpassword){
this.password=password;
}
publicStringgetPassword2(){
returnpassword2;
}
publicvoidsetPassword2(Stringpassword2){
this.password2=password2;
}
publicStringgetUsername(){
returnusername;
}
publicvoidsetUsername(Stringusername){
this.username=username;
}
}
<六>在com.gc.action包下编写用户验证类UserValidator.java
packagecom.gc.action;
importcom.gc.action.User;
importorg.springframework.validation.Validator;
importorg.springframework.validation.Errors;
publicclassUserValidatorimplementsValidator{
publicbooleansupports(Classclazz){
returnclazz.equals(User.class);
}
publicvoidvalidate(Objectobj,Errorserrors){
Useruser=(User)obj;
if(!"tang".equals(user.getUsername())){
errors.rejectValue("username","usernameerror",null,"用户名不正确");
}
if(!"123456".equals(user.getPassword())){
errors.rejectValue("password","passworderror",null,"密码不正确");
}
if(!user.getPassword().equals(user.getPassword2())){
errors.rejectValue("password2","password2error",null,"2次输入密码不一致");
}
}
}
<七>在com.gc.action包下编写用户登录逻辑Login.java
packagecom.gc.action;
importjava.util.Map;
importorg.apache.log4j.Logger;
importorg.springframework.validation.BindException;
importorg.springframework.web.servlet.ModelAndView;
importorg.springframework.web.servlet.mvc.SimpleFormController;
publicclassLoginextendsSimpleFormController{
privateLoggerlogger=Logger.getLogger(this.getClass().getName());
publicModelAndViewonSubmit(Objectcommand,BindExceptionerrors)throwsException{
Useruser=(User)command;
Mapmodle=errors.getModel();
modle.put("user",user);
returnnewModelAndView(getSuccessView(),modle);
}
}
<八>myMVC/WEB-INF目录下编写配置文件dispatcherServlet-servlet.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN"
"http:www.springframework.org/dtd/spring-bean.dtd">
<beans>
<!--定义国际化消息-->
<beanid="messageSource"class="org.springframework.context.support.ResourceBundleMessageSource">
<propertyname="basename">
<value>messages</value>
</property>
</bean>
<!--定义映射-->
<beanid="urlMapping"class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<propertyname="mapping">
<props>
<propkey="login.do">login</prop>
</props>
</property>
</bean>
<!--定义视图及路径-->
<beanid="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<propertyname="viewClass">
<value>org.springframework.web.servlet.view.InternalResourceView</value>
</property>
<!--JSP都放在WEB-INF/jsp目录下-->
<propertyname="prefix">
<value>/WEB-INF/jsp</value>
</property>
<!--JSP页面的后缀都四.JSP-->
<propertyname="suffix">
<value>.jsp</value>
</property>
</bean>
<!--定义控制器-->
<beanid="login"class="com.gc.action.Login">
<propertyname="commandClass">
<value>com.gc.action.User</value>
</property>
<!--指定验证类-->
<propertyname="validator">
<refbean="userValidator"/>
</property>
<!--指定失败要返回的页面-->
<propertyname="formView">
<value>login</value>
</property>
<propertyname="successView">
<value>success</value>
</property>
</bean>
<!--定义验证类-->
<beanid="userValidator"class="com.gc.action.UserValidator"></bean>
</beans>
<九>myMVC/WEB-INF/src目录下编写资源文件messages.properties及messages_en_US.properties
messages.properties
title=SpringMVC实例
username=输入用户名:
password=输入密码:
password2=确认密码:
submit=提交
welcome=欢迎
loginSuccess=登录成功
usernameerror=用户名不正确
passworderror=密码不正确
doublepassword=2次密码不一致
native2ascii工具转码后
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
转自网络<script type="text/javascript"></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>