用于初始化一些用户进行搜索的服务。文件:UserService.java 如下所示 -
package com.yiibai.services; import com.yiibai.model.User; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @Service public class UserService { private List<User> users; // Love Java 8 public List<User> findByUserNameOrEmail(String username) { List<User> result = users.stream().filter(x -> x.getUsername().equalsIgnoreCase(username)).collect(Collectors.toList()); return result; } /*public List<User> findByUserNameOrEmail(String username, String email) { List<User> result = new ArrayList<User>(); for (User user : users) { if ((!StringUtils.isEmpty(username)) && (!StringUtils.isEmpty(email))) { if (username.equals(user.getUsername()) && email.equals(user.getEmail())) { result.add(user); continue; } else { continue; } } if (!StringUtils.isEmpty(username)) { if (username.equals(user.getUsername())) { result.add(user); continue; } } if (!StringUtils.isEmpty(email)) { if (email.equals(user.getEmail())) { result.add(user); continue; } } } return result; }*/ // Init some users for testing @PostConstruct private void iniDataForTesting() { users = new ArrayList<User>(); User user1 = new User("maxsu", "password111", "maxsu@yahoo.com"); User user2 = new User("yiflow", "password222", "yflow@yahoo.com"); User user3 = new User("minaxu", "password333", "minaxu@yahoo.com"); User user4 = new User("minalee", "password333", "minalee@yahoo.com"); User user5 = new User("yiiflow", "password333", "yiiflow@gmail.com"); users.add(user1); users.add(user2); users.add(user3); users.add(user4); users.add(user5); } }Spring Boot启动器(程序入口),参考以下代码实现。文件:SpringBootWebApplication.java 如下所示 -
package com.yiibai; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootWebApplication { public static void main(String[] args) throws Exception { SpringApplication.run(SpringBootWebApplication.class, args); } } 4. HTML表单+ jQuery Ajax一个简单的HTML表单,用 bootstrap 框架装饰。
文件:ajax.html
获取HTML表单,并通过JSON.stringify将搜索条件转换为JSON格式,并通过jQuery.ajax发送POST请求。
文件:main.js 如下所示 -