资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

如何在springmvc中使用spring与mybatis实现一个用户登录功能

如何在springmvc中使用spring与mybatis实现一个用户登录功能?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

成都创新互联公司是专业的定日网站建设公司,定日接单;提供网站设计、成都网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行定日网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

web.xml


 
  dispatcher
  org.springframework.web.servlet.DispatcherServlet
  
   
   contextConfigLocation
   classpath*:config/spring-mvc.xml
  
  1
 
 
  dispatcher
  
  *.do
 
  
 
  login.jsp
 

配置完后,我们需要在对springmvc框架进行配置,配置文件名为spring-mvc.xml,也是存放在config文件夹下:



 
 

 
 

 
 
  
  

 

当springmvc配置完成后,就需要编写业务啦,也就是service包下的东西,首先编写一个接口类userservice,里面存放了我们抽象出来的登录方法login

package com.mjl.service;

import org.springframework.ui.Model;

/**
 * Created by alvin on 15/9/7.
 */
public interface UserService {
 public boolean login(String username,String password);
}

然后在创建一个userservice的实现类userserviceimpl用于实现我们所抽象出来的登录方法

package com.mjl.service;

import com.mjl.dao.IUserDao;
import com.mjl.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

/**
 * Created by alvin on 15/9/7.
 */
//@Service("UserService") 注解用于标示此类为业务层组件,在使用时会被注解的类会自动由
 //spring进行注入,无需我们创建实例
@Service("UserService")
public class UserServiceImpl implements UserService {
 //自动注入iuserdao 用于访问数据库
 @Autowired
 IUserDao Mapper;

 //登录方法的实现,从jsp页面获取username与password
 public boolean login(String username, String password) {
//  System.out.println("输入的账号:" + username + "输入的密码:" + password);
  //对输入账号进行查询,取出数据库中保存对信息
  User user = Mapper.selectByName(username);
  if (user != null) {
//   System.out.println("查询出来的账号:" + user.getUsername() + "密码:" + user.getPassword());
//   System.out.println("---------");
   if (user.getUsername().equals(username) && user.getPassword().equals(password))
    return true;

  }
  return false;

 }
}

编写完业务层代码后,我们就可以写控制层代码啦,控制层的代码用于处理页面提交的业务

package com.mjl.controller;

import com.mjl.model.User;
import com.mjl.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;


/**
 * Created by alvin on 15/9/7.
 */

//@Controller注解用于标示本类为web层控制组件
@Controller
//@RequestMapping("/user")用于标定访问时对url位置
@RequestMapping("/user")
//在默认情况下springmvc的实例都是单例模式,所以使用scope域将其注解为每次都创建一个新的实例
@Scope("prototype")
public class UserController {
 //自动注入业务层的userService类
 @Autowired
  UserService userService;

 //login业务的访问位置为/user/login
 @RequestMapping("/login")
  public String login(User user,HttpServletRequest request){
  //调用login方法来验证是否是注册用户
  boolean loginType = userService.login(user.getUsername(),user.getPassword());
  if(loginType){
   //如果验证通过,则将用户信息传到前台
   request.setAttribute("user",user);
   //并跳转到success.jsp页面
   return "success";
  }else{
   //若不对,则将错误信息显示到错误页面
   request.setAttribute("message","用户名密码错误");
   return "error";
  }
 }

}

控制层代码写完后,就可以进行前端页面代码编写了,登录代码

<%--
 Created by IntelliJ IDEA.
 User: alvin
 Date: 15/9/7
 Time: 下午10:05
 To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
 String path = request.getContextPath();
 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


 







     登入窗口       用户名:             密码:                     

登入成功代码

<%--
 Created by IntelliJ IDEA.
 User: alvin
 Date: 15/9/8
 Time: 下午6:21
 To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
 String path = request.getContextPath();
 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


 



登入成功!

您好!${user.username}
返回

登入失败代码

 <%--
 Created by IntelliJ IDEA.
 User: alvin
 Date: 15/9/8
 Time: 下午6:22
 To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
 String path = request.getContextPath();
 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


 


登入失败!
${message}

/login.jsp" rel="external nofollow" >返回

看完上述内容,你们掌握如何在springmvc中使用spring与mybatis实现一个用户登录功能的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


当前标题:如何在springmvc中使用spring与mybatis实现一个用户登录功能
标题路径:http://cdkjz.cn/article/ppepgc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220