spring mvc 简单登录例子

 

 

------------------------------loginaction

package com.gc.action;

import java.net.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 {
 //复写 onSubmit
 @SuppressWarnings("unchecked")
 public ModelAndView onSubmit(Object command,BindException errors)
 throws Exception{
  User 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 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.password = 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 Validator {

 public boolean supports(Class arg0) {
  // TODO Auto-generated method stub
  return 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()))
        {
         errors.rejectValue("password", "",null,"密码不正确");
        }
        if(!user.getPassword().equals(user.getPassword2()))
        {
         errors.rejectValue("password2", "",null,"2次输入的密码不一致");
        }
       
       
 
 }

}

 


-----------------------------dispatcherServlet-servlet.xml


 "http://www.springframework.org/dtd/spring-beans.dtd">

   
            class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
       
       
           
                login
           

       

       
      
       
   

             class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       
            org.springframework.web.servlet.view.InternalResourceView
       

       
        
            /WEB-INF/jsp/
        

        
        
            .jsp
        

        
   

   
   
       
            com.gc.action.User
       

       
           
       

       
       
             login
       

       
       
             success
       

   

   
   
  

----------------------------login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="spring" uri="/spring" %>

SpringMVC登录实例


  

      
            输入用户名 :
      ${status.errorMessage}

      

      
            密码:
      ${status.errorMessage}

      

    
            确认密码:
      ${status.errorMessage}

      

        
   


---------------------------------------------------------------

你可能感兴趣的:(spring,mvc,string,class,user,bean)