• SSM练习——登录实现


     使用的技术:SSM框架,Jquery,Bootstrap,Jsp

    运行效果:

    项目结构:

    相关jar包,在pom.xml中配置:

    配置文件代码:

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" 
        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_3_0.xsd">
        
        <!-- 1.启动Spring的容器 -->
       <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring.xml</param-value>
        </context-param>
       
        <!--②负责启动spring容器的监听器,它将使用①处的上下文参数获得spring配置文件地址-->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
           
        
        <!-- 2.SpringMVC的前端控制器  拦截所有请求 -->
        <servlet>  
            <servlet-name>dispatch</servlet-name>  
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
            <!-- 未指定SpringMVC的配置文件,所以在与web同级文件下建立一个与servlet-name同名+servlet的xml文件 -->
            <init-param>  
                <param-name>contextConfigLocation</param-name>  
                <param-value>classpath:springmvc.xml</param-value>  
            </init-param> 
            <load-on-startup>1</load-on-startup>
        </servlet>  
        <servlet-mapping>  
                <servlet-name>dispatch</servlet-name>  
                <url-pattern>/</url-pattern>  
        </servlet-mapping>  
        
        <!-- 3.字符编码过滤器  放在所有过滤器前-->
        <filter>
            <filter-name>CharacterEncodingFilter</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <init-param>
                    <param-name>encoding</param-name>
                    <param-value>utf-8</param-value>
            </init-param>
            
            <init-param>
                    <param-name>forceRequestEncoding</param-name>
                    <param-value>true</param-value>
            </init-param>
            
            <init-param>
                    <param-name>forceResponseEncoding</param-name>
                    <param-value>true</param-value>
            </init-param>
            
        </filter>
        
        <filter-mapping>
                <filter-name>CharacterEncodingFilter</filter-name>
                <url-pattern>/*</url-pattern>
        </filter-mapping>
        
        <!-- 4.使用REST风格的URI -->
        <!-- 
              配置org.springframework.web.filter.HiddenHttpMethodFilter:可以把PUT请求转为DELETE或POST请求
       -->
       <filter>
                 <filter-name>HiddenHttpMethodFilter</filter-name>
                   <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
       
       </filter>
       <filter-mapping>
                   <filter-name>HiddenHttpMethodFilter</filter-name>
                   <!-- 过滤所有请求 -->
                   <url-pattern>/*</url-pattern>
       </filter-mapping>
       
       
                   <!-- 使页面ajax成功发put请求 
                   在Spring MVC过滤器-HiddenHttpMethodFilter中我们提到,
                   jsp或者说html中的form的method值只能为post或get,我们可以通过HiddenHttpMethodFilter获取put表单中的参数-值,
                   而在Spring3.0中获取put表单的参数-值还有另一种方法,即使用HttpPutFormContentFilter过滤器。
                   HttpPutFormContentFilter过滤器的作为就是获取put表单的值,并将之传递到Controller中标注了method为RequestMethod.put的方法中。
                   -->
       <filter>
               <filter-name>HttpPutFormContentFilter</filter-name>
               <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
       </filter>
       
       <filter-mapping>
               <filter-name>HttpPutFormContentFilter</filter-name>
               <url-pattern>/*</url-pattern>
       </filter-mapping>
       
       
        </web-app>

    spring.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    
           xmlns:tx="http://www.springframework.org/schema/tx"
     
             xmlns:aop="http://www.springframework.org/schema/aop"
     
           xmlns:mvc="http://www.springframework.org/schema/mvc"
     
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     
           xmlns:context="http://www.springframework.org/schema/context" 
     
           xmlns:p="http://www.springframework.org/schema/p"
           
           
     
      xsi:schemaLocation="
     
                  http://www.springframework.org/schema/beans    
     
                     http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
         
                  http://www.springframework.org/schema/tx      
     
                  http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
     
                   http://www.springframework.org/schema/context
     
                   http://www.springframework.org/schema/context/spring-context-4.3.xsd
                   
                   http://www.springframework.org/schema/mvc
                   
                   http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
     
                   http://www.springframework.org/schema/aop
     
                 http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
                 
                <!--Spring的配置文件 :主要配置和逻辑业务有关的-->
                
                <!-- 扫描包 -->
                <context:component-scan base-package="com.ssm">
                            <!-- 不扫描@Controller注解  -->
                        <context:exclude-filter type="annotation"  expression="org.springframework.stereotype.Controller" />
                
                </context:component-scan>
                
                            <!-- 引入外部配置文件 -->
                <context:property-placeholder  location="classpath:dbconfig.properties"/>
                
                
                <!-- ==================数据源,事务控制 ========================-->
                <bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
                        <property name="jdbcUrl" value="${jdbc.url}"></property>
                        <property name="driverClass" value="${jdbc.driver}"></property>
                        <property name="user" value="${jdbc.username}"></property>
                        <property name="password" value="${jdbc.password}"></property>
                </bean>
                
                
                <!-- ========================配置和Mybatis的整合 =============================-->
                
                <bean id="sqlSessionFactory"  class="org.mybatis.spring.SqlSessionFactoryBean">
                
                                    <!-- 指定Mybatis全局配置文件的位置 -->
                
                <!-- 加载mybatis的配置文件 -->
                <property name="configLocation" value="classpath:mybatis-config.xml"></property>
                <!-- 配置数据源 -->
                <property name="dataSource" ref="pooledDataSource"></property>
                
                <!-- 指定mybatis.mapper文件的位置 -->
                <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
                
                </bean>
                
                <!-- 配置扫描器,将mybatis接口的实现加入到ioc容器中 -->
                    <bean  class="org.mybatis.spring.mapper.MapperScannerConfigurer">
                           
                   <!-- 扫描所有dao接口的实现,加入到ioc容器 -->
                           <property name="basePackage" value="com.ssm.dao"></property>
           
                   </bean>
    
                    <!-- ==================事务控制==================== -->
                    
                    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
                            <!-- 控制数据源 -->
                            <property name="dataSource" ref="pooledDataSource"></property>
                    
                    </bean>
                
                <!-- 开启使用xml配置形式的事务 -->
                <aop:config>
                <!-- 切入点表达式 -->
                    <aop:pointcut expression="execution(* com.ssm.service.*.*(..))" id="txPoint"/>
                    <!-- 配置事务增强 -->
                    <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
                
                </aop:config>
                
                
                
                <!-- 配置事务增强,事务如何切入 -->
                <tx:advice id="txAdvice"  transaction-manager="transactionManager">
                    <tx:attributes>
                        <!-- 所有方法都是事务方法 -->
                        <tx:method name="*"></tx:method>
                        
                            <!--以get开头的所有方法-->
                        <tx:method name="get*" read-only="true"></tx:method>
                
                    </tx:attributes>
                </tx:advice>
                
                <!-- 
                        Spring配置文件的核心点:数据源,与mybatis的整合,事务控制
                
                 -->
                 
                 
                 <!-- 配置一个可以批量的执行sqlSession -->
                 <bean  id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
                         <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
                         
                         <!-- 执行器类型,批量-batch -->
                        <constructor-arg name="executorType" value="BATCH"></constructor-arg>
                
                 </bean>
                 
    
    </beans>

    springmvc.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
     
             xmlns:aop="http://www.springframework.org/schema/aop"
     
           xmlns:mvc="http://www.springframework.org/schema/mvc"
     
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     
           xmlns:context="http://www.springframework.org/schema/context" 
     
           xmlns:p="http://www.springframework.org/schema/p"
           
           xmlns:tx="http://www.springframework.org/schema/tx"
     
      xsi:schemaLocation="
     
                  http://www.springframework.org/schema/beans    
     
                     http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
         
                  http://www.springframework.org/schema/tx      
     
                  http://www.springframework.org/schema/tx/spring-mvc-4.3.xsd
     
                   http://www.springframework.org/schema/context
     
                   http://www.springframework.org/schema/context/spring-context-4.3.xsd
                   
                   http://www.springframework.org/schema/mvc
                   
                   http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
     
                   http://www.springframework.org/schema/aop
     
                 http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
                 
    <!--SpringMVC的配置文件 :主要包含网站跳转的控制,配置-->
            <context:component-scan base-package="com.ssm" use-default-filters="false">
            
            <!-- 扫描@Controller注解  不扫描用exclude -->
        <context:include-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
            
            </context:component-scan>
            
            <!-- 配置视图解析器  方便页面返回 -->
            <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
             <!-- spring中加入jstl标签库 -->
               <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
                           <!-- 前缀 -->
                           <property name="prefix" value="/WEB-INF/views/"></property>
                           <!-- 后缀 -->
                           <property name="suffix" value=".jsp"></property>
               </bean>
               
               <!-- 两个标准配置 -->
               <!-- 将SpringMVC不能处理的请求交给tomcat -->
               <mvc:default-servlet-handler></mvc:default-servlet-handler>
               <!-- 能支持SpringMVC更高级的一些功能  JSR303的校验,快捷的ajax,映射动态请求-->
               <mvc:annotation-driven>
                       <!-- <mvc:message-converters>
                         <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
                          <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
                     </mvc:message-converters>
                -->
               </mvc:annotation-driven>
            
    </beans>

    mybatis-config.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE configuration
      PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
      "http://mybatis.org/dtd/mybatis-3-config.dtd">
      
      <configuration>
                      <!-- 开启自动驼峰命名规则(camel case)映射,
                      即从经典数据库列名 A_COLUMN 到经典 Java 属性名 aColumn 的类似映射。 -->
              <settings>
                  <setting name="mapUnderscoreToCamelCase" value="true" />
              </settings>
              
              <!-- 类型别名  自定义别名name为类所在的包路径-->
              <typeAliases>
                  <package name="com.ssm.bean"></package>
              </typeAliases>
             
      </configuration>

    dbconfig.properties

    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/ssm
    jdbc.username=root
    jdbc.password=123

     其中修改了Mybatis逆向工程生成的相关文件

    EmployeeMapper.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.ssm.dao.EmployeeMapper">
      <resultMap id="BaseResultMap" type="com.ssm.bean.Employee">
        <id column="id" jdbcType="INTEGER" property="id" />
        <result column="name" jdbcType="VARCHAR" property="name" />
        <result column="password" jdbcType="VARCHAR" property="password" />
        <result column="gender" jdbcType="CHAR" property="gender" />
        <result column="email" jdbcType="VARCHAR" property="email" />
      </resultMap>
      <sql id="zd">
              id,name,password,gender,email
      </sql>
      <!-- 登录验证 -->
      <select id="login" resultMap="BaseResultMap" parameterType="com.ssm.bean.Employee">
              select <include refid="zd"></include> from Employee
              <where>
                      name=#{name} and password=#{password}
              </where>
      
      </select>
       <!-- 注册 -->
      <insert id="insert" parameterType="com.ssm.bean.Employee">
              insert into Employee(<include refid="zd"></include>) 
              values(#{name},#{password},#{gender},#{email})
      
      </insert>
    </mapper>

    EmployeeMaper.java

    package com.ssm.dao;
    
    import com.ssm.bean.Employee;
    
    public interface EmployeeMapper {
        
        public Employee login(Employee emp);
        
        String insert(Employee emp);
      
    }

    EmpService.java

    package com.ssm.service;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.ssm.bean.Employee;
    import com.ssm.dao.EmployeeMapper;
    
    @Service
    public class EmpService {
        
            @Autowired
            EmployeeMapper employeeMapper;
    
        public Employee getLogin(Employee emp) {
            
            Employee empinfo = employeeMapper.login(emp);
                
            return empinfo;
        }
    
    }

    EmpController.java

    package com.ssm.controller;
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import com.ssm.bean.Employee;
    import com.ssm.service.EmpService;
    
    @Controller
    public class EmpController {
            @Autowired
            EmpService empService;
            
            @RequestMapping("/login")
            public String elogin(HttpServletRequest request){
                String loginname = request.getParameter("name");
                String loginpass = request.getParameter("password");
                Employee emp = new Employee();
                emp.setName(loginname);
                emp.setPassword(loginpass);
                
                Employee empo = empService.getLogin(emp);
                    if(empo!=null){
                        return "success";
                    }else{
                        return "error";
                    }
                }
                
            
    }

    视图层显示页面

    登录页面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>登录页</title>
    <link rel="stylesheet" type="text/css"
        href="./static/bootstrap-3.3.7-dist/css/bootstrap.min.css">
        <style>
            .form-horizontal{
                    500px;
                    height:300px;
                    
            }
        </style>
    </head>
    
    <script src="./static/js/jquery-3.2.1.min.js" language="javascript"></script>
    <script type="text/javascript">
        // 控制onsubmit提交的方法,方法名是vform()
            function form(){
                
                var name = $("#loginname").val();
                var password = $("#loginpass").val();
                //判断上面的变量,如果为空字符串不能提交
                if(name == ""){
                    alert("请输入登录名!");
                    return false;
                }
                if(password == ""){
                    alert("请输入密码!");
                    return false;
                }
                //除以上结果的可以提交,返回true
                return true;
            }
        </script>
    
    <body>
    
        <h1>练习ssm框架之登录实现</h1>
        <hr>
    
        <form class="form-horizontal" action="login" method="post"
            onsubmit="return form()">
            <div class="form-group">
                <label class="col-sm-2 control-label">用户名</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" id="loginname"
                         name="name">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label">密码</label>
                <div class="col-sm-10">
                    <input type="password" class="form-control" id="loginpass"
                         name="password">
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <button type="submit" class="btn btn-default">登录</button>
                </div>
            </div>
        </form>
    </body>
    </html>

    登录成功或失败跳转页面

    <body>
        welcome to start studying SSM<br>
      </body>
    <body>
        帐号密码错误,<a href="index.jsp">返回</a> <br>
      </body>

                                                                                                           百度链接https://pan.baidu.com/s/1bqy18rt

  • 相关阅读:
    TextWatcher 监视EditText的输入
    【Android】SqLitOpenHelper操作数据库
    【第一个开源项目】最完整的Andoird联系人操作,完美支持联系人的各种属性操作。
    Ext.ux.grid.RowEditor更新
    『转』如何判断Store加载完毕
    MyEclipse8.6安装svn插件
    『转』Eclipse中设置编码的方式
    『转』jsonlib json 格式 Date
    Spring Security的关键组件
    Ext.grid之numbercolumn格式化输出
  • 原文地址:https://www.cnblogs.com/liurg/p/8287677.html
Copyright © 2020-2023  润新知