使用SpringMVC实现用户注册和登录的小例子

使用SpringMVC实现用户注册和登录的小例子

1.新建Dynamic Web Project,名称RequestMappingTest
2.导入jar包
使用SpringMVC实现用户注册和登录的小例子_第1张图片
3.添加相关文件
使用SpringMVC实现用户注册和登录的小例子_第2张图片
4.文件代码
loginForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




登录页面


	

登录页面


registerForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




注册页面


	

注册页面


welcom.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




登陆成功页面


	

欢迎${requestScope.user.username }登录


springmvc-config.xml



   	
   	
   		
   			/WEB-INF/context/
   		
   		
   			.jsp
   		
   	
   

web.xml



  RequestMappingTest
  
  	log4jConfiguration
  	classpath:log4j2.xml
  
  
  	springmvc
  	org.springframework.web.servlet.DispatcherServlet
  	
  		contextConfigLocation
  		/WEB-INF/springmvc-config.xml
  	
  	1
  
  
  
  	springmvc
	/
  
    
        characterEncodingFilter  
        org.springframework.web.filter.CharacterEncodingFilter  
          
            encoding  
            UTF-8  
          
          
            forceEncoding  
            true  
          
      
      
        characterEncodingFilter  
        /*  
      
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  

UserController.java

package org.fkit.controller;
 
import java.util.ArrayList;
import java.util.List;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.fkit.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
 
 
@Controller
@RequestMapping(value="user")
public class UserController {
	//静态List集合,此处代替数据库用来保存注册的用户信息
	private static List userList;
	//UserController类的构造器,初始化List集合
	public UserController(){
		super();
		userList = new ArrayList();
	}
	//静态日之类loggerMaggent
	private static final Logger logger = LogManager.getLogger(UserController.class.getName());
	@RequestMapping(value="/register",method=RequestMethod.GET)
	public String registerForm(){
		logger.info("register GET 方法被调用……");
		return "registerForm";
	}
	
	@RequestMapping(value="/register",method=RequestMethod.POST)
	public String register(@RequestParam("loginname")String loginname,
			@RequestParam("password")String password,
			@RequestParam("username")String username){
		logger.info("register POST 方法被调用……");
		//创建user对象
		User user = new User();
		user.setLoginname(loginname);
		user.setPassword(password);
		user.setUsername(username);
		//模拟数据库存储User信息
		userList.add(user);
		return "loginForm";		
	}
	
	//登录
	@RequestMapping("/login")
	public String login(@RequestParam("loginname")String loginname,
			@RequestParam("password")String password,Model model){
		logger.info("登录名:"+loginname+" 密码:"+password);
		//到集合中查找用户是否存在,此处用来模拟数据库验证
		for(User user:userList){
			if(user.getLoginname().equals(loginname) && user.getPassword().equals(password)){
				model.addAttribute("user", user);
				return "welcome";
			}
		}
		return "loginForm";
	}
	
}

User.java

package org.fkit.domain;
 
public class User {
	private String loginname;
	private String password;
	private String username;
	public String getLoginname() {
		return loginname;
	}
	public void setLoginname(String loginname) {
		this.loginname = loginname;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
}


log4j2.xml



	
		${sys:catalina.base}/var/logs
		${LOG_HOME}/requestMappingTest
	
 
	
		
			
			
			
			
		
		
			
			
				%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M --
					%msg%xEx%n
			
			
				
			
			
			
			
				
					
					
				
			
		
	
 
	
		
			
			
		
		
			
			
		
		
		
			
			
		
		
			
			
		
	
 


5.界面
使用SpringMVC实现用户注册和登录的小例子_第3张图片
使用SpringMVC实现用户注册和登录的小例子_第4张图片
使用SpringMVC实现用户注册和登录的小例子_第5张图片

你可能感兴趣的:(SpringMVC)