• Eclipse+Spring+gradle


    eclipse常用快捷键

    • ctrl+shfit+s:get、set方法
    • shift+Alt+L:快捷生成变量

    spring boot创建工具:https://start.spring.io/

    修改下载依赖

    repositories {
    	maven{
           url 'http://maven.aliyun.com/nexus/content/groups/public/'
          
         }
    }
    
    

    Eclipse下载插件:

    buildship、spring-tool-site

    配置项目编码为UTF-8

    项目配置

    #服务器端口号
    server.port=8080
    
    #配置JSP视图的位置和后缀
    spring.mvc.view.prefix=/WEB-INF/jsp/
    spring.mvc.view.suffix=.jsp
    
    #数据源配置
    spring.datasource.url=jdbc:mysql://localhost:3308/registerdemo?useUnicode=true&characeterEncoding=utf-8&serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true
    spring.datasource.username=root
    spring.datasource.password=root
    #spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    
    #Hibernate配置
    spring.jpa.hibernate.ddl-auto=create
    spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
    
    
    

    配置依赖

    plugins {
    	id 'org.springframework.boot' version '2.1.4.RELEASE'
    	id 'java'
    }
    
    apply plugin: 'io.spring.dependency-management'
    
    group = 'com.lys'
    version = '0.0.1-SNAPSHOT'
    sourceCompatibility = '1.8'
    
    repositories {
    	maven{
           url 'http://maven.aliyun.com/nexus/content/groups/public/'
          
         }
    }
    
    dependencies {
    	implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    	implementation 'org.springframework.boot:spring-boot-starter-web'
            compile 'org.springframework.boot:spring-boot-devtools'#在修改程序之后,能够自动重启服务器;修改静态文件(html、css)时不会频繁地重启服务器	
    
    	runtime 'org.springframework.boot:spring-boot-starter-tomcat'
    	runtime 'org.apache.tomcat.embed:tomcat-embed-jasper'
    	runtimeOnly 'mysql:mysql-connector-java'
    	testImplementation 'org.springframework.boot:spring-boot-starter-test'
    }
    
    

    项目架构

    控制器

    package com.lys.register.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    @Controller
    @RequestMapping("/register")
    public class RegisterControll {
    
    	@RequestMapping(method=RequestMethod.GET)
    	public String home(){
    		return "register";
    	}
    }
    
    
    package com.lys.register.controller;
    
    
    import javax.servlet.http.HttpSession;
    
    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.bind.annotation.RequestMethod;
    
    import com.lys.register.bean.LoginFrom;
    import com.lys.register.model.User;
    import com.lys.register.service.UserService;
    
    @Controller
    @RequestMapping("/login")
    public class LoginController {
    	
    	@Autowired
    	UserService UserService;
    
    	@RequestMapping(method=RequestMethod.GET)
    	public String home(){
    		return "login";
    	}
    	
    	@RequestMapping(value="doLogin",method=RequestMethod.POST)
    	public String doLogin(LoginFrom login,Model model,HttpSession session) {
    		User user = new User(login.getName(),login.getPwd());
    		System.out.println(user);
    		User loginUser=UserService.checkLogin(user, model);
    		if(loginUser==null) {
    			return "login";
    		}else {
    			session.setAttribute("user", loginUser);
    			return "loginSuccess";
    		}
    		
    	}
    	
    	@RequestMapping(value="doLogout",method=RequestMethod.GET)
    	public String doLogout(HttpSession session) {
    		session.invalidate();
    		return "login";
    	}
    }
    
    

    bean文件

    package com.lys.register.bean;
    
    
    public class LoginFrom {
    
    	private String name;
    	private String pwd;
    	
    	
    	public LoginFrom() {
    	}
    	public LoginFrom(String name, String pwd) {
    		super();
    		this.name = name;
    		this.pwd = pwd;
    		
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public String getPwd() {
    		return pwd;
    	}
    	public void setPwd(String pwd) {
    		this.pwd = pwd;
    	}
    	
    	
    }
    
    
    package com.lys.register.bean;
    
    
    
    public class RegisterFrom {
    
    	private String name;
    	private String pwd;
    	private String email;
    	
    	public RegisterFrom() {
    	}
    	public RegisterFrom(String name, String pwd, String email) {
    		super();
    		this.name = name;
    		this.pwd = pwd;
    		this.email = email;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public String getPwd() {
    		return pwd;
    	}
    	public void setPwd(String pwd) {
    		this.pwd = pwd;
    	}
    	public String getEmail() {
    		return email;
    	}
    	public void setEmail(String email) {
    		this.email = email;
    	}
    	
    	
    	
    }
    
    

    dao文件

    package com.lys.register.dao;
    
    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.stereotype.Repository;
    
    import com.lys.register.model.User;
    
    @Repository
    public interface UserRepository extends JpaRepository<User, Long> {
    
    	User findByUsername(String username);//数据库查询,约定find或get
    }
    
    

    model文件

    package com.lys.register.model;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    @Entity
    @Table(name="user")
    public class User {
    
    	@Id
    	@GeneratedValue(strategy=GenerationType.SEQUENCE)
    	private Long id;
    	private String username;
    	private String email;
    	private String password;
    	
    	public User() {
    	}
    	
    	public User(String username, String email, String password) {
    		super();
    		
    		this.username = username;
    		this.email = email;
    		this.password = password;
    	}
    
    	
    	public User(String username, String password) {
    		super();
    		this.username = username;
    		this.password = password;
    	}
    
    
    	public Long getId() {
    		return id;
    	}
    	
    	public String getUsername() {
    		return username;
    	}
    	public void setUsername(String username) {
    		this.username = username;
    	}
    	public String getEmail() {
    		return email;
    	}
    	public void setEmail(String email) {
    		this.email = email;
    	}
    	public String getPassword() {
    		return password;
    	}
    	public void setPassword(String password) {
    		this.password = password;
    	}
    	
    	
    	
    }
    
    

    service

    package com.lys.register.service;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.ui.Model;
    
    import com.lys.register.dao.UserRepository;
    import com.lys.register.model.User;
    
    @Service
    public class UserService {
    
    	@Autowired
    	UserRepository userRepository;
    	
    	public User addUser(User user,Model model) {
    		try {
    			User registerUser=userRepository.save(user);
    			return registerUser;
    		} catch (Exception e) {
                 e.printStackTrace();
                 model.addAttribute("error",e.getMessage());
    		}
    		
    		return null;
    	}
    	
    	public User checkLogin(User user,Model model) {
    		User findUser = userRepository.findByUsername(user.getUsername());
    		if(findUser==null) {
    			model.addAttribute("error","用户名不存在");
    			return null;
    		}else {
    			if(findUser.getPassword().equals(user.getPassword())) {
    				return findUser;
    			}else {
    				model.addAttribute("error","用户名和密码不匹配");
    				return null;
    			}
    		}
    	}
    
    }
    
    

    jsp文件

    login.jsp

    <%@page import="com.lys.register.model.User"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>用户登录</title>
    	<style type="text/css">
    	
    		.error{
    		  color:red;
    		}
    	</style>
    </head>
    <body>
    
        <%
           User user = (User)session.getAttribute("user");
          if(user==null){
        	  String error = (String)request.getAttribute("error");
        	  if(error !=null){
        %>
        <div class="error">
           ${error}
        </div>
        <%
        	  }
        %>
    	   <form action="/login/doLogin" method="post">
    	              用户名: <input type="text" name="name"><br/>
    	              密码: <input type="password" name="pwd"><br/>           
    	           <input type="submit" name="登录" value="登录"><br/>
    	   </form>
       <%
          }else{
        	  out.println("欢迎"+user.getUsername()+"<a href="/login/doLogout">退出登录</a>");
          }
       %>
    </body>
    </html>
    

    loginSuccess.jsp

    
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>用户登录</title>
    </head>
    <body>
    
     
    
       恭喜${sessionScope.user.username}&lt;${sessionScope.user.email}&gt;登录成功!
    </body>
    </html>
    

    register.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>用户注册</title>
    </head>
    <body>
    
       <form action="/register/doRegister" method="post">
                  用户名: <input type="text" name="name"><br/>
                  邮箱: <input type="text" name="email"><br/>
                  密码: <input type="password" name="password"><br/>
               <input type="submit" name="注册" value="注册"><br/>
       </form>
    </body>
    </html>
    

    registerSuccess.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>用户注册</title>
    </head>
    <body>
    
       注册成功!<a herf="/login">转到登录</a>
    </body>
    </html>
    

    registerFailed

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>用户注册</title>
    </head>
    <body>
    
       注册失败!失败原因 ${error}
    </body>
    </html>
    
  • 相关阅读:
    Nokia Lumia 800销售反馈 苹果iPhone、三星Galaxy不敌800设计
    各大网站用户数据库被爆,遭大量网友下载
    最美发明家:GPS、手机通讯网都源自她的发明
    iPhone5或明年下半年发布 配备iOS6和A6芯片
    保存文件到手机内存
    2012年十大科技趋势:NFC、语音控制与弯曲屏
    电脑报独家报道:宽带升级全国真相调查
    Android的电话拨号器
    Java程序员成长之路(接口与抽象类究竟有什么区别)
    联系人相关
  • 原文地址:https://www.cnblogs.com/lyszyl/p/10836196.html
Copyright © 2020-2022  润新知