SpringMVC 框架 (简单登录实例)

ln1590 贡献于2013-03-18

作者 Think  创建于2012-08-14 13:26:00   修改者china  修改于2013-03-18 02:46:00字数6474

文档摘要:SpringMVC 框架 (简单登录实例)
关键词:

本次工程结构图 Spring 1 视图层.jsp login.jsp <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> My JSP 'index.jsp' starting page
用户名:
密码:
error.jsp <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> My JSP 'error.jsp' starting page 十分对不起,登录失败,由于:<%=request.getAttribute("msg") %> success.jsp  <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> My JSP 'success.jsp' starting page 恭喜:<%=request.getAttribute("username") %>,登录成功 Spring 2.控制层 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 model=new HashMap(); model.put("msg", message); return new ModelAndView(getErrorPage(),model); } if(!UserInfoBean.exisitUser(username)){ message=username+"不存在"; Map model=new HashMap(); model.put("msg", message); return new ModelAndView(getErrorPage(),model); } if(!UserInfoBean.confirmPassword(username,password)){ message=username+"密码不正确"; Map model=new HashMap(); model.put("msg", message); return new ModelAndView(getErrorPage(),model); } else {Map model=new HashMap(); model.put("username",username); return new ModelAndView(getSuccessPage(),model); } } // public UserInfoBean getUserInfoBean() { // return userInfoBean; // } // // public void setUserInfoBean(UserInfoBean userInfoBean) { // this.userInfoBean = userInfoBean; // } } Spring 3.模型层 package com.spring.model; import java.util.HashMap; import java.util.Map; public class UserInfoBean { private static Mapuserinfo=new HashMap(); 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 这里配置spring 的后台servlet dispatcherServlet org.springframework.web.servlet.DispatcherServlet 指定spring配置文件的路径 contextConfigLocation /WEB-INF/classes/applicationContext.xml 1 拦截所有以.do结尾的请求,可以修改 dispatcherServlet *.do login.jsp Spring applicationContext.xml login error.jsp success.jsp

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 6 金币 [ 分享文档获得金币 ] 3 人已下载

下载文档