1、spring mvc 简单登录例子 -loginactionpackage com.gc.action;import .BindException;import java.util.HashMap;import java.util.Map;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.SimpleFormController;public class Login extends SimpleFormController /复写 onSubmitSupp
2、ressWarnings(“unchecked“)public ModelAndView onSubmit(Object command,BindException errors)throws ExceptionUser user=(User)command;/Map model=() errors).getModel();Map model=new HashMap();model.put(“user“,user);return new ModelAndView(getSuccessView(),model);-User 类package com.gc.action;public class
3、User private String username=null;private String password=null;private String password2=null;public String getUsername() return username;public void setUsername(String username) this.username = username;public String getPassword() return password;public void setPassword(String password) this.passwor
4、d = password;public String getPassword2() return password2;public void setPassword2(String password2) this.password2 = password2;-UserValidator 类package com.gc.action;import org.springframework.validation.Errors;import org.springframework.validation.Validator;public class UserValidator implements Va
5、lidator public boolean supports(Class arg0) / TODO Auto-generated method stubreturn arg0.equals(User.class);public void validate(Object obj, Errors errors) User user=(User)obj;if(!“gf“.equals(user.getUsername()errors.rejectValue(“username“, “,null,“用户名不正确“);if(!“123456“.equals(user.getPassword()erro
6、rs.rejectValue(“password“, “,null,“密码不正确“);if(!user.getPassword().equals(user.getPassword2()errors.rejectValue(“password2“, “,null,“2 次输入的密码不一致 “);-dispatcherServlet-servlet.xmlloginorg.springframework.web.servlet.view.InternalResourceView/WEB-INF/jsp/.jspcom.gc.action.Userloginsuccess-login.jspSpringMVC 登录实例输入用户名 :$status.errorMessage 密码:$status.errorMessage 确认密码:$status.errorMessage