现在的位置: 首页 > 综合 > 正文

springMVC框架(简单登录实例)

2013年06月22日 ⁄ 综合 ⁄ 共 7002字 ⁄ 字号 评论关闭

本次工程结构图

 

 

 

 

 

 

 

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+"/";

%>

 

<!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>

    十分对不起,登录失败,由于:<%=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 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;

 

publicclass LoginController
implements Controller {

  private String
successPage;

  private String
errorPage;

  //private UserInfoBean userInfoBean;

 

  public String getSuccessPage() {

    returnsuccessPage;

  }

 

  publicvoid setSuccessPage(String successPage) {

    this.successPage = successPage;

  }

 

  publicvoid setErrorPage(String errorPage) {

    this.errorPage = errorPage;

  }

private String getErrorPage() {

   

    returnerrorPage;

  }

 

  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);

       

        returnnew ModelAndView(getErrorPage(),model);

       

       

      }

         

   

   

   

 

 

 

  if(!UserInfoBean.exisitUser(username)){

    message=username+"不存在";

     Map<String,String> model=new HashMap<String,String>();

      model.put("msg", message);

     

      returnnew ModelAndView(getErrorPage(),model);

    }

if(!UserInfoBean.confirmPassword(username,password)){

    message=username+"密码不正确";

    Map<String,String> model=new HashMap<String,String>();

      model.put("msg", message);

     

      returnnew ModelAndView(getErrorPage(),model);

   

   

  }

  else

  {Map<String, String> model=new HashMap<String,String>();

  model.put("username",username);

  returnnew 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;

 

publicclass UserInfoBean {

  privatestatic 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);

   

  }//判断一个用户名是否存在

  publicstaticboolean exisitUser(String username){

    returnuserinfo.containsKey(username);

  }

  publicstaticboolean confirmPassword(String username,String
password)

  {returnuserinfo.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>

 

 

Spring 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>

抱歉!评论已关闭.