springMVC框架简单登录实例.docx
- 文档编号:23228833
- 上传时间:2023-05-15
- 格式:DOCX
- 页数:12
- 大小:130.19KB
springMVC框架简单登录实例.docx
《springMVC框架简单登录实例.docx》由会员分享,可在线阅读,更多相关《springMVC框架简单登录实例.docx(12页珍藏版)》请在冰豆网上搜索。
springMVC框架简单登录实例
本次工程结构图
Spring1视图层.jsp
login.jsp
<%@pagelanguage="java"import="java.util.*"pageEncoding="utf-8"%>
<%
Stringpath=request.getContextPath();
StringbasePath=request.getScheme()+":
//"+request.getServerName()+":
"+request.getServerPort()+path+"/";
%>
DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
--
-->
用户名:
密码:
error.jsp
<%@pagelanguage="java"import="java.util.*"pageEncoding="utf-8"%>
<%
Stringpath=request.getContextPath();
StringbasePath=request.getScheme()+":
//"+request.getServerName()+":
"+request.getServerPort()+path+"/";
%>
DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
--
-->
十分对不起,登录失败,由于:
<%=request.getAttribute("msg")%>
success.jsp
<%@pagelanguage="java"import="java.util.*"pageEncoding="utf-8"%>
<%
Stringpath=request.getContextPath();
StringbasePath=request.getScheme()+":
//"+request.getServerName()+":
"+request.getServerPort()+path+"/";
%>
DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
--
-->
恭喜:
<%=request.getAttribute("username")%>,登录成功
Spring2.控制层
packagecom.spring.controller;
importjava.util.HashMap;
importjava.util.Map;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importorg.springframework.web.servlet.ModelAndView;
importorg.springframework.web.servlet.mvc.Controller;
importcom.spring.model.UserInfoBean;
publicclassLoginControllerimplementsController{
privateStringsuccessPage;
privateStringerrorPage;
//privateUserInfoBeanuserInfoBean;
publicStringgetSuccessPage(){
returnsuccessPage;
}
publicvoidsetSuccessPage(StringsuccessPage){
this.successPage=successPage;
}
publicvoidsetErrorPage(StringerrorPage){
this.errorPage=errorPage;
}
privateStringgetErrorPage(){
returnerrorPage;
}
publicModelAndViewhandleRequest(HttpServletRequestrequest,
HttpServletResponseresponse)throwsException{
Stringusername=request.getParameter("username");
Stringpassword=request.getParameter("password");
Stringmessage=null;
if(username==null||password==null||username.trim().equals("")||password.trim().equals(""))
{
message="用户名或者密码为空";
Map
model.put("msg",message);
returnnewModelAndView(getErrorPage(),model);
}
if(!
UserInfoBean.exisitUser(username)){
message=username+"不存在";
Map
model.put("msg",message);
returnnewModelAndView(getErrorPage(),model);
}
if(!
UserInfoBean.confirmPassword(username,password)){
message=username+"密码不正确";
Map
model.put("msg",message);
returnnewModelAndView(getErrorPage(),model);
}
else
{Map
model.put("username",username);
returnnewModelAndView(getSuccessPage(),model);
}
}
//publicUserInfoBeangetUserInfoBean(){
//returnuserInfoBean;
//}
//
//publicvoidsetUserInfoBean(UserInfoBeanuserInfoBean){
//this.userInfoBean=userInfoBean;
//}
}
Spring3.模型层
packagecom.spring.model;
importjava.util.HashMap;
importjava.util.Map;
publicclassUserInfoBean{
privatestaticMap
static
{
StringnumberOneUser="zhangsan";
StringnumberOnePassword="123";
StringnumberTwoUser="lisi";
StringnumberTwoPassword="456";
userinfo.put(numberTwoUser,numberTwoPassword);
userinfo.put(numberOneUser,numberOnePassword);
}//判断一个用户名是否存在
publicstaticbooleanexisitUser(Stringusername){
returnuserinfo.containsKey(username);
}
publicstaticbooleanconfirmPassword(Stringusername,Stringpassword)
{returnuserinfo.get(username).equals(password);
}
}
Spring编写配置文件:
Web.xml
xmlversion="1.0"encoding="UTF-8"?
>
xmlns=" xmlns: xsi="http: //www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation=" 这里配置spring的后台servlet 指定spring配置文件的路径 拦截所有以.do结尾的请求,可以修改 SpringapplicationContext.xml xmlversion="1.0"encoding="UTF-8"? > 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/beanshttp: //www.springframework.org/schema/beans/spring-beans-2.0.xsd"> -- --
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- springMVC 框架 简单 登录 实例
![提示](https://static.bdocx.com/images/bang_tan.gif)