初学SSM实现简单登录注册实例

初学SSM试着做了个登录注册实例检验一下,顺便把代码搬上来了
废话不多说直接撸
数据库版本:mysql-8.0.13(版本较高,jar包和一些语法会有出入)
开发工具:Eclipse

整体结构

在这里插入图片描述

libs

不是全部的包都要用到,当时图省事就全弄进来了
在这里插入图片描述

数据表

俩字段,很简单,只是方便测试

CREATE TABLE test (
  name varchar(20) NOT NULL,
  pwd varchar(20) DEFAULT NULL,
  PRIMARY KEY (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>b8_c301</display-name>
	<!-- 加载Spring -->
    <context-param>
       <param-name>
           contextConfigLocation
       </param-name>
       <param-value>
           classpath:applicationContext.xml
       </param-value>
   </context-param>
   <!-- Listener监听  -->
   <listener>
       <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    
    <!-- 加载SpringMvc -->
    
    <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.action</url-pattern>
    </servlet-mapping>
    <!-- 配置SpringMVC编码过滤器 -->
  	<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>
  	</filter>
  	<filter-mapping>
  		<filter-name>CharacterEncodingFilter</filter-name>
  		<url-pattern>/*</url-pattern>
  	</filter-mapping>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
</web-app>

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:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd">
	<!-- 开启IOC扫描  -->
	<context:component-scan
		base-package="com.cn.service">
	</context:component-scan>
	<!-- 配置数据源 -->
	<bean id="dataSource"
		class="org.apache.commons.dbcp2.BasicDataSource">
		<property name="driverClassName"
			value="com.mysql.cj.jdbc.Driver" />//注意数据库版本
		<property name="url"
			value="jdbc:mysql://localhost:3306/library_sys?useSSL=false&amp;serverTimezone=UTC" />//注意数据库版本,8.0版本中url要设置时区,并且xml配置文件中要使用“&amp;”来代替“&”
		<property name="username" value="root" />
		<property name="password" value="fknoobq3q" />
		<!-- Max连接数 -->
		<property name="maxTotal" value="30"></property>
		<!-- Max空闲连接数 -->
		<property name="maxIdle" value="10"></property>
		<!-- 初始化连接数 -->
		<property name="initialSize" value="5"></property>
	</bean>
	<!-- sqlSessionFactory -->
	<bean id="sqlSessionFactory"
		class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 关联连接池 -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- 加载sql映射文件 -->
		<property name="mapperLocations"
			value="classpath:com/cn/dao/UserMapper.xml"></property>//直接加载的sql映射文件,省去配置mybatis-config.xml的步骤
	</bean>
	<!-- Mapper接口扫描 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.cn.dao"></property>
	</bean>

	<!-- 开启Spring事务 -->
	<bean id="txManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<!-- 开启事务注解 -->
	<tx:annotation-driven
		transaction-manager="txManager" />
</beans>

springmvc-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

	<!-- 扫描Controller -->
	<context:component-scan
		base-package="
	com.cn.controller ">
    </context:component-scan>
	<!-- 注解驱动 -->
	<mvc:annotation-driven></mvc:annotation-driven>
	<!-- 视图解析器:简化在Controller类编写的视图路径 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 前缀 -->
		<property name="prefix" value="/WEB-INF/jsp/" />
		<!-- 后缀 -->
		<property name="suffix" value=".jsp" />
	</bean>
</beans>

UserController

package com.cn.controller;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.cn.entiy.User;
import com.cn.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService;

	@RequestMapping("/golog")
	public String golog() {
		return "login";
	}
	@RequestMapping("/goreg")
	public String goreg() {
		return "register";
	}
	@RequestMapping("/register")
	public String login(User user) {
		userService.saveUser(user);
		return "succ";
	}
	@RequestMapping("/login")
	public String register(String name,String pwd) {
		User user=userService.findUser(name, pwd);
		if(user!=null) {
			return "succ";
		}else {
			return "fail";
		}
	}
}

UserMapper

package com.cn.dao;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import com.cn.entiy.User;

@Repository
@Mapper
public interface UserMapper {
	void saveUser(User user);

	User findUser(@Param("name")String name,@Param("pwd")String pwd);
}

UserMapepr.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.cn.dao.UserMapper">
	<!-- saveUser()方法实现 -->
	<insert id="saveUser" parameterType="com.cn.entiy.User">
		insert into test (name,pwd) values (#{name},#{pwd})
	</insert>
	<!-- findUser()方法实现 -->
	<select id="findUser" resultType="com.cn.entiy.User">
	select  * from test where name=#{name} and pwd=#{pwd}
	</select>
</mapper>

User

package com.cn.entiy;

public class User {
	private String name;
	private String pwd;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	@Override
	public String toString() {
		return "User [name=" + name + ", pwd=" + pwd + "]";
	}
	
}

UserService

package com.cn.service;

import org.springframework.stereotype.Service;
import com.cn.entiy.User;

public interface UserService {
	void saveUser(User user);
	User findUser(String name, String pwd);
}

UserServiceImpl

package com.cn.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cn.dao.UserMapper;
import com.cn.entiy.User;
@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {
	@Autowired
	private UserMapper userMapepr;
	@Override
	public void saveUser(User user) {
		// TODO Auto-generated method stub
		userMapepr.saveUser(user);
	}
	@Override
	public User findUser(String name, String pwd) {
		// TODO Auto-generated method stub
		return userMapepr.findUser(name, pwd);
	}
}

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
	<form action="${pageContext.request.contextPath}/user/login.action"
		method="post">
		用户名:<input type="text" name="name"> 密码:<input type="password"
			name="pwd"> <input type="submit" value="提交">
	</form>
</body>
</html>

register.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
	<form
		action="${pageContext.request.contextPath}/user/register.action"
		method="post">
	用户名:<input type="text" name="name">
	密码:<input type="text" name="pwd">
	<input type="submit" value="提交">
	</form>
</body>
</html>

succ.jsp和fail.jsp随便写点测试文字就行了,我就不沾上来了

将项目部署到tomcat上发布,然后在浏览器中输入以下地址,会有以下注册界面(密码框的格式为text,所以是明文)出现,点击提交会返回succ.jsp
在这里插入图片描述
在浏览器中输入以下地址,会有如下登录界面,点击提交如果匹配数据库返回succ.jsp,未匹配则返回fail.jsp
在这里插入图片描述

最后贴一个为什么要在spring中扫描service,在springmvc中扫描controller的贴

2019年11月22日更新

此版本为初学时搭建的,源码已经丢失,附上新的重构版本

  • 3
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
SSM指的是Spring、SpringMVC、MyBatis三大框架的集成,是目前最为流行的Java Web开发框架之一。在Eclipse中利用SSM框架实现登录需要进行以下步骤: 1. 搭建SSM框架环境 首先需要在Eclipse中搭建好SSM框架的环境,包括Spring、SpringMVC、MyBatis等组件,以及相关依赖包的导入。 2. 设计数据库 登录功能需要用到数据库存储用户信息,因此需要在MySQL等数据库软件中创建相关表,并将表结构导入到Eclipse中。 3. 创建登录页面 在Eclipse中创建登录页面,包括用户名、密码的输入框,以及登录按钮等控件。同时还需要在页面中添加相关的CSS和JavaScript文件,实现页面的美化和交互效果。 4. 编写控制器代码 创建Java类作为登录页面的控制器,利用SpringMVC框架中的注解将其注册为控制器。在控制器中编写相关代码,实现登录功能。具体内容包括获取用户输入的用户名和密码,与数据库中存储的信息进行比对,验证用户身份并进行页面跳转等操作。 5. 配置MyBatis 在MyBatis中配置Mapper接口,实现数据库操作。这样就能够方便地从数据库中获取用户信息,以及将用户信息存储到数据库中。 6. 实现登录验证 利用Spring框架中的AOP机制,实现登录验证的拦截器,并在控制器中使用拦截器进行登录验证。这样可以避免非法用户未经授权进行操作。 以上就是在Eclipse中使用SSM框架实现登录的基本步骤,具体实现过程可能还需根据项目需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值