IDEA实现 springmvc的简单注册登录功能的示例代码

这篇文章主要介绍了IDEA实现 springmvc的简单注册登录功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

1.基本环境搭建

spring简介

SpringMVC框架是以请求为驱动,围绕Servlet设计,将请求发给控制器,然后通过模型对象,分派器来展示请求结果视图。其中核心类是DispatcherServlet,它是一个Servlet,顶层是实现的Servlet接口。

project创建

在这里插入图片描述
随便

在图中填上项目名称即可,其他直接next

在这里插入图片描述

如上图所示,创建两个包,并且指定包的类型,如下图,java包指定为Sources Root,resouces包指定为Resources root

在这里插入图片描述

整个目录结构:

在这里插入图片描述

pom依赖

  UTF-81.71.74.3.18.RELEASE5.1.483.0-alpha-11.91.48.1.8.v201211061.7.56.8.7  org.springframeworkspring-core4.3.18.RELEASE org.springframeworkspring-beans${spring.version} org.springframeworkspring-jdbc${spring.version} org.springframeworkspring-context${spring.version} org.springframeworkspring-webmvc${spring.version} org.springframeworkspring-context-support${spring.version} org.springframeworkspring-tx${spring.version} mysqlmysql-connector-java${mysql.version} commons-dbcpcommons-dbcp 1.4 javax.servletservlet-api3.0-alpha-1provided javax.servlet.jspjsp-api2.2 org.aspectjaspectjweaver1.9.4 org.springframeworkspring-web${spring.version} jstljstl1.2 org.testngtestng6.14.3test org.springframeworkspring-test${spring.version}test

2.

1.domain 实体类

 package domain; public class User { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }

2.dao层(实现了查询和插入)

 package dao; import domain.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; @Repository public class UserDao{ private JdbcTemplate jdbcTemplate; private final static String sql=" Select count(*) from user_name where username=? and password=? "; private final static String sqlInsert="insert into user_name(username,password) values (?,?)"; @Autowired public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public int FindUser(String username,String password) { return jdbcTemplate.queryForObject(sql,new Object[]{username,password},Integer.class); } // public void InsertUser(String username,String password){ jdbcTemplate.update(sqlInsert, username,password); } }

3.service层

 package service; import dao.UserDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserService { private UserDao userdao; @Autowired public void setUserDao(UserDao userdao) { this.userdao = userdao; } public boolean Match(String username,String password) { int count=userdao.FindUser(username,password); return count>0; } public void InsertUser(String username,String password){ userdao.InsertUser(username,password); } }

4.controller层(这里用的包名为web)

 package web; import domain.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import service.UserService; import javax.servlet.http.HttpServletRequest; @Controller public class UserController { private UserService userService; @Autowired public void setUserService(UserService userService) this.userService = userService; } @RequestMapping(value = "/index.html") public String tologin() { return "login"; } @RequestMapping(value = "/login") public ModelAndView login(HttpServletRequest request, User user){ boolean isValidUser=userService.Match(user.getUsername(),user.getPassword()); if (isValidUser){ request.getSession().setAttribute("User",user.getUsername()+":登录成功"); return new ModelAndView("success"); }else{ return new ModelAndView("login"); } } @RequestMapping("/insert") public String InsertUser(User user, Model model){ userService.InsertUser(user.getUsername(),user.getPassword()); model.addAttribute("Insert","注册成功"); return "success1"; } @RequestMapping("/insertPage") public String InsertPage() { return "register"; } }

3.xml配置

1.web.xml

  Archetype Created Web ApplicationArchetype Created Web Application org.springframework.web.context.ContextLoaderListener contextConfigLocationclasspath:applicationContext.xml dispatcherServletorg.springframework.web.servlet.DispatcherServlet contextConfigLocationclasspath:dispatcher-servlet.xml1 dispatcherServlet/

resource里面新建两个spring xml文件

在这里插入图片描述

2.applicationContext.xml
spring的配置文件applicationContext.xml中的一些配置的作用。

    com.mysql.jdbc.Driverjdbc:mysql://localhost:3306/web1root123

3.dispatcher-servlet.xml

    

4.Jsp

在这里插入图片描述1.login

 <%@ page contentType="text/html;charset=UTF-8" language="java" %><%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  主页 ${error} 
" method="post"> 用户名:
密码:
注册

2.register

 <%@ page contentType="text/html;charset=UTF-8" language="java" %>  Title 
用户名:
密码:

3.success

 <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  成功 ${User} 

4.success1

 <%@ page contentType="text/html;charset=UTF-8" language="java" %>  注册成功  ${Insert}

配置好Tomcat就可以运行了

在这里插入图片描述

很简单的登录界面

到此这篇关于IDEA实现 springmvc的简单注册登录功能的文章就介绍到这了,更多相关idea springmvc注册登录内容请搜索html中文网以前的文章或继续浏览下面的相关文章希望大家以后多多支持html中文网!

以上就是IDEA实现 springmvc的简单注册登录功能的示例代码的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java