文档章节

javaweb登录 SSM框架

o
 osc_4nmshwhm
发布于 2018/08/06 22:19
字数 1156
阅读 19
收藏 0

精选30+云产品,助力企业轻松上云!>>>

 

 

 

同样数据库中先建表

DROP TABLE IF EXISTS `admin`;
CREATE TABLE `admin` (
  `adminPassword` varchar(20) DEFAULT NULL,
  `adminName` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

下图为整体结构:

 

要使用的包请自行百度

package controller;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import pojo.Admin;
import service.AdminService;@Controller
@RequestMapping(value="/admin")
public class AdminController {
 @Autowired
 private AdminService adminService;
 @RequestMapping(value="/login.do")
 public ModelAndView login(Admin admin, HttpSession session) {
  ModelAndView mv = new ModelAndView();
  
  
  //登录
  Admin a = adminService.login(admin);
  //登录验证
  if(a != null) { //成功
   session.setAttribute("admin", a);
   mv.setViewName("/success.jsp");
  } else { //失败
    String msg = "用户名或密码错误";
    mv.addObject("msg",msg);
    mv.setViewName("/login.jsp");
  }
  return mv;
 }
 
 @RequestMapping(value="/register.do")
 public ModelAndView register(Admin admin, HttpSession session) {
  ModelAndView mv = new ModelAndView();
  
  //登录
  
  int a = adminService.register(admin);
 
  if(a==1){
   session.setAttribute("admin",admin);
   mv.setViewName("/regsucce.jsp");
  }else {
   String msg = "注册失败,请重试!";
    mv.addObject("msg",msg);
    mv.setViewName("/reg.jsp");
  }
  return mv;
 
 }
 
 @RequestMapping(value="/logout.do")
 public ModelAndView logout(HttpSession session) {
  ModelAndView mv = new ModelAndView();
   session.removeAttribute("admin");
   mv.setViewName("/login.jsp");
  return mv;
 
 }
 
 
}

 

 

package mapper;
import pojo.Admin;
public interface AdminMapper {
 //登录
 Admin findAdminByNameAndPwd(Admin admin);
 //注册
 int register(Admin admin);
}
 
<?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要和Mapper接口的全类名一致
 -->
<mapper namespace="mapper.AdminMapper">
   <select id="findAdminByNameAndPwd" parameterType="pojo.Admin" resultType="admin">
    select * from admin where adminName=#{adminName} and adminPassword=#{adminPassword}
   </select>
   <insert id="register" parameterType="pojo.Admin">
      <!--
    keyProperty:返回的主键存放在pojo对象的什么位置
    order="AFTER" 在插入操作运行完成之后执行
    resultType:返回的主键的类型
    -->
   <selectKey keyProperty="id" order="AFTER" resultType="int">
    select LAST_INSERT_ID()
   </selectKey>
   insert into admin(adminName,adminPassword) values(#{adminName},#{adminPassword})
   </insert>
</mapper>
 
package service;
import pojo.Admin;
public interface AdminService {
 Admin login(Admin admin);
 //注册
 int register(Admin admin);
}
 
 
package service;
import mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.Admin;
import service.AdminService;
@Service(value="adminService")
public class AdminServiceImpl implements AdminService{
 @Autowired
 private AdminMapper adminMapper;
 @Override
 public Admin login(Admin admin) {
  return adminMapper.findAdminByNameAndPwd(admin);
 }
 @Override
 public int register(Admin admin) {
  return adminMapper.register(admin);
 }
 
}
<?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:aop="http://www.springframework.org/schema/aop"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
 <!-- 加载配置文件 -->
 <context:property-placeholder location="classpath:db.properties"/>
 <!-- 配置数据源 -->
 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
  <property name="driverClass" value="${jdbc.driverName}"></property>  
  <property name="jdbcUrl" value="${jdbc.url}"></property>  
  <property name="user" value="${jdbc.username}"></property>  
  <property name="password" value="${jdbc.password}"></property>  
  <property name="acquireIncrement" value="${acquireIncrement}"></property>  
  <property name="initialPoolSize" value="${initialPoolSize}"></property>  
  <property name="minPoolSize" value="${minPoolSize}"></property>  
  <property name="maxPoolSize" value="${maxPoolSize}"></property>
 </bean>
 
 <!-- 配置sqlSessionFactory -->
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
  <property name="dataSource" ref="dataSource"></property>
 </bean>
 
 <!--
  使用包扫描的方式批量引入Mapper,扫描后引用的时候可以使用类名,首字母小写
  -->
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <!-- 指定要扫描的包的全路径名称,如果有多个包用英文状态下的逗号分隔 -->
  <property name="basePackage" value="mapper"></property>
 </bean>
 
 <!--
  开启注解扫描,扫描所有service包下的注解
  -->
 <context:component-scan base-package="service"></context:component-scan>
</beans>jdbc.driverName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/admin
jdbc.username=用户名
jdbc.password=密码
acquireIncrement=3
initialPoolSize=10
minPoolSize=2
maxPoolSize=10

log4j.rootLogger=DEBUG,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

 

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    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"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    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">
   <!-- 配置@Controller注解扫描
 
   -->
   <context:component-scan base-package="controller"></context:component-scan>
       
</beans>
 
<?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>
 <typeAliases>
  <package name="pojo"/>
 </typeAliases>
</configuration>
 
 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login</title>
</head>
<body>
 ${msg}
 <form action="${pageContext.request.contextPath}/admin/login.do" method="post">
  用户名<input type="text" name="adminName" /><br/>
  密码<input type="password" name="adminPassword" /><br/>
  <input type="submit" value="登录" />
   <a href="${pageContext.request.contextPath}/reg.jsp">注册</a>
 </form>
</body>
</html>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>register</title>
<script language="javascript" type="">
  function RegSubmit(){
   var user = document.reg.adminName.value;
   var pwd = document.reg.adminPassword.value;
   if(user==null||user==""){
    alert("请填写用户名");
document.getElementById("adminName").focus;
   }else if(pwd==null||pwd==""){
    alert("密码不能为空");
   
   }else document.reg.submit();
   
  }
</script>
</head>
<body>
  ${msg}
 <form name="reg" action="${pageContext.request.contextPath}/admin/register.do" method="post">
  用户名<input type="text" name="adminName" /><br/>
  密码<input type="password" name="adminPassword" /><br/>
  <input type="button" value="注册" onclick="RegSubmit()"/>
  
 </form>
</body>
</html>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
   恭喜您,注册成功!<br/>
   <a href="${pageContext.request.contextPath}/login.jsp">登录</a>
</body>
</html>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
 恭喜您,登录成功!
 欢迎${sessionScope.admin.adminName}<br/>
 当前您的用户信息为:<br/>
 用户名:${sessionScope.admin.adminName}
 密码:${sessionScope.admin.adminPassword}
 <form action="${pageContext.request.contextPath}/admin/logout.do" method="post">
 <input type="submit" value="注销" />
 </form>
</body>
</html>

 

o
粉丝 0
博文 500
码字总数 0
作品 0
私信 提问
加载中
请先登录后再评论。

暂无文章

格式编号始终显示2个小数位 - Format number to always show 2 decimal places

问题: I would like to format my numbers to always display 2 decimal places, rounding where applicable. 我想将数字格式化为始终显示2个小数位,并在适用的情况下四舍五入。 Examples...

富含淀粉
今天
22
0
Docker可视化工具Portainer

1 前言 从没想到Docker也有可视化的工具,因为它的命令还是非常清晰简单的。无聊搜了一下,原来已经有很多Docker可视化工具了。如DockerUI、Shipyard、Rancher、Portainer等。查看对比了一番...

南瓜慢说
今天
20
0
日志系统新贵 Loki,真香!!

最近,在对公司容器云的日志方案进行设计的时候,发现主流的ELK或者EFK比较重,再加上现阶段对于ES复杂的搜索功能很多都用不上最终选择了Grafana开源的Loki日志系统,下面介绍下Loki的背景。...

庞陆阳
今天
14
0
jQuery获取select onChange的值 - jQuery get value of select onChange

问题: I was under the impression that I could get the value of a select input by doing this $(this).val(); 我的印象是我可以通过执行$(this).val();来获取选择输入的值$(this).val()......

javail
今天
13
0
道翰天琼解密宇宙信息大脑三者最核心奥秘,破解认知智能基础理论(群聊形式)

三体论是探索研究宇宙,信息和人类大脑三者关系的理论体系。是认知智能的奠基理论体系之一。宇宙和信息,信息和人类大脑,人类大脑和宇宙,三者之间存在着某种未被完全揭示的奥秘。三体论的核...

jackli2020
今天
15
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部