spring boot 登录注册 demo (一)
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>
简易说明:
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"; } } }
@RequestMapping("/login") ensures that HTTP requests to/login
are mapped to thelogin()
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"; }
这里没有@ResponseBody ,因此会去匹配到login.html