天天看点

spring mvc 模拟数据库实现注册 登录

model层代码,用户

package com.entity;
import java.io.Serializable;

public class User implements Serializable {
	//私有字段;
	private String username;
	private String pwd;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	
	
}
           

view层,几个简单的注册 登录 欢迎页面,分别如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'zhuce.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
  </head>
  
  <body>
  注册页面
    <form action="reg" method="post">
    	账户:<input type="text" name="username"/><br/>
    	密码:<input type="password" name="pwd"/><br/>
    	<input type="submit" value="注册"/>
    </form>
  </body>
</html>
           
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'login.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
  </head>
  
  <body>
  用户登录
    <form action="login" method="post">
    	账户:<input type="text" name="username"/><br/>
    	密码:<input type="password" name="pwd"/><br/>
    	<input type="submit" value="登录"/>
    </form>
  </body>
</html>
           
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page isELIgnored="false" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'welcome.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
  </head>
  
  <body>
  用户:  ${user.username }<br/>
  密码:${user.pwd }
  </body>
</html>
           

控制器代码,其中很多代码还可以继续简化

package com.controller;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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;
import org.springframework.web.servlet.ModelAndView;

import com.entity.User;
//注解该类是一个控制器,可以同时接受多个请求动作;
@Controller
public class UserController {
	//静态List<User>集合,用来代替数据库来保存注册的用户信息;
	private static List<User>userList;
	//UserController类的构造器,初始化List<User>集合
	public UserController() {
		super();
		userList=new ArrayList<User>();
	}
	//静态的日志类;logFactory
	//该方法,请求.../reg,该方法支持Post请求
	@RequestMapping(value="/reg",method=RequestMethod.GET)
	public String regForm(){
		//跳转到注册页面;
		return "zhuce";
	}
	//该方法映射的请求为.../reg,支持Post请求
	@RequestMapping(value="/reg",method=RequestMethod.POST)
	public String register(
			@RequestParam("username")String username,
			@RequestParam("pwd")String pwd	){
		//创建User对象;
		User u=new User();
		u.setUsername(username);
		u.setPwd(pwd);
		//模拟数据存储到数据库user里面
		userList.add(u);
		//跳转到登录页面
		return "login";
	}
	//该方法映射的请求是.../login
	@RequestMapping("/login")
	public ModelAndView login(@RequestParam("username")String username,
									@RequestParam("pwd")String pwd,ModelAndView mv){
		//RequestParam将请求中的username和pwd参数赋值给username,pwd变量。
		//到集合中去查找下用户是否存在,此处用来模拟数据库验证。
		for(User user:userList){
			if(user.getUsername().equals(username)&&user.getPwd().equals(pwd)){
				mv.addObject("user",user);
				mv.setViewName("welcome");
				//mv.addAttribute("user",user);model传递的是requestScope
				return mv;
			}
		}
		return mv;
	}

}           

配置文件,web.xml代码

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
  	<servlet-name>hello</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>WEB-INF/spring-servlet.xml</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>hello</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
  
  <filter>  
        <filter-name>encodingFilter</filter-name>  
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
        <init-param>  
            <param-name>encoding</param-name>  
            <param-value>UTF-8</param-value>  
        </init-param>  
    </filter>  
    <filter-mapping>  
        <filter-name>encodingFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>  
</web-app>           

spring-servlet.xml代码

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.0.xsd
	http://www.springframework.org/schema/mvc
	http://www.springframework.org/schema/mvc/spring-mvc.xsd">
	<context:annotation-config></context:annotation-config>
	<context:component-scan base-package="com"></context:component-scan>
	<!-- ViewResolver -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
		<property name="prefix" value="/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
<!--	<bean name="/hello" class="com.controller.HelloController"></bean>-->
</beans>           

在后续讲课中添加了删除 修改 查询功能,针对删除报错如下:java.util.ConcurrentModificationException异常.

对Vector、ArrayList在迭代的时候如果同时对其进行修改就会抛出java.util.ConcurrentModificationException异常。下面我们就来讨论以下这个异常出现的原因以及解决办法。

单线程环境下的解决办法

public

class

Test {

public

static

void

main(String[] args)  {

ArrayList<Integer> list =

new

ArrayList<Integer>();

list.add(

2

);

Iterator<Integer> iterator = list.iterator();

while

(iterator.hasNext()){

Integer integer = iterator.next();

if

(integer==

2

)

iterator.remove();  

//注意这个地方

}

}

}

在多线程环境下的解决方法