3.Eclipse+SSM+登录

环境搭建参考https://blog.csdn.net/YKYZSYA/article/details/108000053
对象封装参考https://blog.csdn.net/YKYZSYA/article/details/108070188
查所有参考https://blog.csdn.net/YKYZSYA/article/details/108034576

index.jsp(body标签内)

<!-- 提交地址是login,提交方式是post -->
   <form action="login" method="post" onsubmit="return login()">
   <table border="1px solid red" cellpadding="0px" cellspacing="0px" align="center">
   <tr>
   <td align="center" colspan="2">登录</td>
   </tr>
   <tr>
   <td>账号:</td>
   <td><input type="text" name="name" id="name"></td>
   </tr>
    <tr>
   <td> 密码:</td>
   <td><input type="password" name="pwd" id="pwd"></td>
   </tr>
  <tr>
  <td align="center" colspan="2"><input type="submit" value="提交"></td>
  </tr>
   </table>
   </form>

html标签外,判断账号密码是否为空,若为空则不提交

<script type="text/javascript">
    function login(){
        //得到name输入框对象
        var name = document.getElementById("name");
        var pwd = document.getElementById("pwd");
        //判断输入框是否有内容
        if(name.value.length==0){
            confirm("账号不能为空");
            return false;
        }else if(pwd.value.length==0){
            confirm("密码不能为空");
            return false;
        }else {
            return true;
        }
    }
</script>

Dao层接口层(UserDao.java(是一个接口))

//登录
 public Userinfo login(Userinfo userinfo);

Dao层mapper层 usermapper.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">
 <!-- 对应连接的dao层接口 naspace内部输入的是对应接口的路径 -->
<mapper namespace="com.ly.dao.UserDao">
<!-- 因为登录属于按条件查询,所以调用查询标签,resultType表示返回的结果类型,
因为spring配置了对象路径,所以这里填写封装的对象Userinfo 的首字母小写
parameterType表示返回类型-->
<select id="login" resultType="userinfo" parameterType="userinfo">
select * from userinfo where name=#{name} and pwd=#{pwd}
</select>
</mapper>

Service层接口层(UserSer.java(是一个接口))

//登录
 public Userinfo login(Userinfo userinfo);

Service层实现层(UserSerImpl.java(UserSer的实现类))

@Service
public class UserSerImpl implements UserSer {
@Autowired
 UserDao userDao;
 @Override
 public Userinfo login(Userinfo userinfo) {
  // TODO Auto-generated method stub
  return userDao.login(userinfo);
 }
}

Controller层

@Controller
public class UserCon {
  @Autowired
 UserSer userSer;
 //login是表单提交的地址
 @RequestMapping("login")
 public ModelAndView login(Userinfo userinfo,HttpSession session) {
  ModelAndView mv=new ModelAndView();
  userinfo=userSer.login(userinfo);
  if (userinfo!=null) {
   session.setAttribute("userinfo", userinfo);
   //redirect是重定向,querally是查所有
   mv.setViewName("redirect:queryall");
  }else {
   mv.setViewName("error.jsp");
  }
  return mv;
 }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值