Spring MVC

 

登录界面:



登录界面:
 
 
Spring 视图层_view
index.jsp

<%@ 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 'index.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">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
<form action="login.do" method="post">
用户名:<input type=text name="username"/><br>
密码:<input type=text name="password"/><br>
<input type=submit value="登录"/>
</form>
  </body>
</html>

error.jsp

<%@ 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 'error.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">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
    <body>
<form action="login.do" method="post">
用户名:<input type=text name="username"/><br>
密码:<input type=text name="password"/><br>
<input type=submit value="登录"/>
</form>
      登录失败  , <%=request.getAttribute("msg") %>
  </body></html>

success.jsp
 

<%@ 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 'success.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">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
 恭喜:<%=request.getAttribute("username") %>,登录成功
  </body>
</html>


Spring 控制层_control
LoginController.java
package com.spring.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.spring.model.UserInfoBean;

public class LoginController implements Controller {
	private String successPage;
	private String errorPage;
	//private UserInfoBean userInfoBean;

	public String getSuccessPage() {
		return successPage;
	}

	public void setSuccessPage(String successPage) {
		this.successPage = successPage;
	}

	public void setErrorPage(String errorPage) {
		this.errorPage = errorPage;
	}
private String getErrorPage() {
		
		return errorPage;
	}

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		  String username=request.getParameter("username");
		  String password=request.getParameter("password");
		  String message=null;
		  if(username==null||password==null||username.trim().equals("")||password.trim().equals(""))
		  {
			  message=" 用户名或者密码为空";
			  Map<String,String> model=new HashMap<String,String>();
			  model.put("msg", message);
			  
			  return new ModelAndView(getErrorPage(),model);
			 }

	if(!UserInfoBean.exisitUser(username)){
		message=username+"不存在";
		 Map<String,String> model=new HashMap<String,String>();
		  model.put("msg", message);
		  
		  return new ModelAndView(getErrorPage(),model);
		}
	if(!UserInfoBean.confirmPassword(username,password)){
		message=username+"密码不正确";
		Map<String,String> model=new HashMap<String,String>();
		  model.put("msg", message);
		  
		  return new ModelAndView(getErrorPage(),model);
		
	}
	else
	{Map<String, String> model=new HashMap<String,String>();
	model.put("username",username);
	return new ModelAndView(getSuccessPage(),model);
		}
	}
}
Spring 模型层_model
UserInfoBean.java
package com.spring.model;

import java.util.HashMap;
import java.util.Map;

public class UserInfoBean {
	private static Map<String,String>userinfo=new HashMap<String,String>();
	static
	{
		String numberOneUser="zhangsan";
		String numberOnePassword="123";
		String numberTwoUser="lisi";
		String numberTwoPassword="456";
		userinfo.put(numberTwoUser, numberTwoPassword);
		userinfo.put(numberOneUser, numberOnePassword);
		
	}//判断一个用户名是否存在
	public static boolean exisitUser(String username){
		return userinfo.containsKey(username);
	}
	public static boolean confirmPassword(String username,String password)
	{
return userinfo.get(username).equals(password);	
	}
}

Spring 配置文件:
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">
这里配置spring 的后台servlet
	<servlet>
	<servlet-name>dispatcherServlet</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
指定spring配置文件的路径
	<init-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
	</init-param>
	<load-on-startup>1</load-on-startup>
		
	</servlet>
拦截所有以.do结尾的请求,可以修改
	<servlet-mapping>
	<servlet-name>dispatcherServlet</servlet-name>
	<url-pattern>*.do</url-pattern>
	
	
	</servlet-mapping>
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
</web-app>


applicationContext.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="login.do">login</prop>

</props>

</property>

</bean>
<bean id="login" class="com.spring.controller.LoginController">
<property name="errorPage">
<value>error.jsp</value>
</property>
<property name="successPage">
<value>success.jsp</value>
</property>
<!--  <property name="userInfoBean" ref="userInfoBean"></property>-->

</bean>
<!--  <bean id="UserInfoBean" class="com.spring.model.UserInfoBean"> </bean>-->
</beans>

  

posted @ 2015-06-12 15:39  依旧若尘、  阅读(375)  评论(0编辑  收藏  举报