java框架注册登录代码_IDEA实现 springmvc的简单注册登录功能的示例代码

1.基本环境搭建

spring简介

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

project创建

fd2e2e0a3660faf8a451ddd3fd318b2b.png

af1136dee43b9ceb894778c43d59a0c9.png

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

e31464958066fa5d7d715571e89a952a.png

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

c828a8893bc310a13d8515cac1c87a46.png

整个目录结构:

b2246022d978967448ecbe7570611e6d.png

pom依赖

UTF-8

1.7

1.7

4.3.18.RELEASE

5.1.48

3.0-alpha-1

1.9

1.4

8.1.8.v20121106

1.7.5

6.8.7

org.springframework

spring-core

4.3.18.RELEASE

org.springframework

spring-beans

${spring.version}

org.springframework

spring-jdbc

${spring.version}

org.springframework

spring-context

${spring.version}

org.springframework

spring-webmvc

${spring.version}

org.springframework

spring-context-support

${spring.version}

org.springframework

spring-tx

${spring.version}

mysql

mysql-connector-java

${mysql.version}

commons-dbcp

commons-dbcp

1.4

javax.servlet

servlet-api

3.0-alpha-1

provided

javax.servlet.jsp

jsp-api

2.2

org.aspectj

aspectjweaver

1.9.4

org.springframework

spring-web

${spring.version}

jstl

jstl

1.2

org.testng

testng

6.14.3

test

org.springframework

spring-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

/p>

"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd" >

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

version="3.0">

Archetype Created Web Application

Archetype Created Web Application

org.springframework.web.context.ContextLoaderListener

contextConfigLocation

classpath:applicationContext.xml

dispatcherServlet

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:dispatcher-servlet.xml

1

dispatcherServlet

/

resource里面新建两个spring xml文件

48f637fff6a409216d745a3429753c3b.png

2.applicationContext.xml

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

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:aop="http://www.springframework.org/schema/aop"

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

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:p="http://www.springframework.org/schema/p"

>

com.mysql.jdbc.Driver

jdbc:mysql://localhost:3306/web1

root

123

id="jdbcTemplate" p:dataSource-ref="dataSource"/>

class="org.springframework.jdbc.datasource.DataSourceTransactionManager"

p:dataSource-ref="dataSource"/>

3.dispatcher-servlet.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/cache"

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/mvc

http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"

>

4.Jsp

679227c8c475f4f80704dea6c2315f91.png1.login

主页

${error}

" method="post">

用户名:

密码:

注册

2.register

Title

用户名:

密码:

3.success

成功

${User}

4.success1

注册成功

${Insert}

配置好Tomcat就可以运行了

48ea598b8b5bae4ebee2335bd349ae6e.png

很简单的登录界面

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值