1. 程式人生 > >spring boot 登錄註冊 demo

spring boot 登錄註冊 demo

script conn factory mode 模板 response requests not instance

Welcome to Spring Boot

代碼結構

技術分享




src/main/java 下

controller層,路由功能
dao層,數據庫的訪問
domain,bean的存放
service,業務層
application.java,spring boot的主啟動程序

src/main/resources/application.properties ,spring boot的配置文件

















詳細代碼說明

pom.xml

技術分享
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>com.jwen</groupId> 7 <artifactId>login</artifactId> 8 <version
>0.0.1-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <name>login</name> 12 <description>Demo project for Spring Boot</description> 13 14 <parent> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId
>spring-boot-starter-parent</artifactId> 17 <version>1.5.4.RELEASE</version> 18 <relativePath /> <!-- lookup parent from repository --> 19 </parent> 20 21 <properties> 22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 24 <java.version>1.8</java.version> 25 </properties> 26 27 <dependencies> 28 <dependency> 29 <groupId>org.springframework.boot</groupId> 30 <artifactId>spring-boot-starter</artifactId> 31 </dependency> 32 33 <dependency> 34 <groupId>org.springframework.boot</groupId> 35 <artifactId>spring-boot-starter-web</artifactId> 36 </dependency> 37 <dependency> 38 <groupId>org.springframework.boot</groupId> 39 <artifactId>spring-boot-starter-thymeleaf</artifactId> 40 </dependency> 41 <dependency> 42 <groupId>org.springframework.boot</groupId> 43 <artifactId>spring-boot-starter-test</artifactId> 44 <scope>test</scope> 45 </dependency> 46 <dependency> 47 <groupId>org.springframework.boot</groupId> 48 <artifactId>spring-boot-devtools</artifactId> 49 </dependency> 50 <dependency> 51 <groupId>org.springframework.boot</groupId> 52 <artifactId>spring-boot-starter-data-jpa</artifactId> 53 </dependency> 54 <dependency> 55 <groupId>mysql</groupId> 56 <artifactId>mysql-connector-java</artifactId> 57 </dependency> 58 </dependencies> 59 60 <build> 61 <plugins> 62 <plugin> 63 <groupId>org.springframework.boot</groupId> 64 <artifactId>spring-boot-maven-plugin</artifactId> 65 <dependencies> 66 <dependency> 67 <groupId>org.springframework</groupId> 68 <artifactId>springloaded</artifactId> 69 </dependency> 70 </dependencies> 71 </plugin> 72 </plugins> 73 </build> 74 75 76 </project>
View Code

簡易說明:

thymeleaf -- 用來渲染模板,spring boot 不建議使用JSP

devtools -- 用來熱部署,可以只關註coding,保存後自行restart

jpa -- 數據庫訪問,很好很強大

LoginApplication.java

package com.jwen.login;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class LoginApplication {

    public static void main(String[] args) {
        SpringApplication.run(LoginApplication.class, args);
    }
}

UserController.java

技術分享
package com.jwen.login.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
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 org.springframework.web.bind.annotation.ResponseBody;

import com.jwen.login.domain.User;
import com.jwen.login.service.UserService;

@Controller
@EnableAutoConfiguration
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/")
    @ResponseBody
    String welcome() {
        return "welcome my first spring boot project";
    }

    @RequestMapping("/notVerify")
    @ResponseBody
    String notVerify() {
        return "username or password NOT correct";
    }

    @RequestMapping("/login")
    String login(Model model) {
        model.addAttribute("user", new User());
        return "login";
    }

    @RequestMapping("/register")
    String register(Model model) {
        model.addAttribute("user", new User());
        return "register";
    }

    @RequestMapping(value = "/registerUser", method = RequestMethod.POST)
    @ResponseBody
    String registerUser(User user, Model model) {
        return userService.registerUser(user);
    }

    @RequestMapping(value = "/userLogin", method = RequestMethod.POST)
    String userLogin(User user, Model model) {
        boolean verify = userService.verifyUser(user);
        if (verify) {
            model.addAttribute("name", user.getName());
            model.addAttribute("password", user.getPassword());
            return "result";
        } else {
            return "redirect:/notVerify";
        }

    }

}
View Code

@RequestMapping("/login")  ensures that HTTP requests to /login are mapped to the login() method,這個註解可以使指定的uri指向到該方法
@ResponseBody 返回response,一般return為string的話,頁面就直接返回該string

配置application.properties,指向html文件:
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.cache=false
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html

會去識別resources/templates下的html文件,根據return回來的string來匹配

@RequestMapping("/login")
String login(Model model) {
    model.addAttribute("user", new User());
    return "login";
    }

[email protected] ,因此會去匹配到login.html

待續




spring boot 登錄註冊 demo