国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

Spring MVC

2019-11-15 00:34:19
字體:
供稿:網(wǎng)友
SPRing MVC

登錄界面:

登錄界面:  Spring 視圖層_viewindex.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="descrCSS" 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 控制層_controlLoginController.javapackage 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 模型層_modelUserInfoBean.javapackage 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);}//判斷一個(gè)用戶名是否存在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結(jié)尾的請求,可以修改<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"?><beansxmlns="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>

  


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 五华县| 黄陵县| 桃园县| 隆化县| 昌平区| 华蓥市| 新建县| 拉孜县| 桦川县| 古丈县| 隆林| 福泉市| 马鞍山市| 鹰潭市| 襄樊市| 巴东县| 宣恩县| 苏尼特右旗| 庆元县| 中阳县| 托里县| 松滋市| 密山市| 阳春市| 义乌市| 察隅县| 东光县| 武夷山市| 行唐县| 霍山县| 石门县| 托克托县| 东港市| 德安县| 司法| 江川县| 金堂县| 洪湖市| 岑溪市| 兴宁市| 拜城县|