抽取公共页面
表单提交
<form class="form-signin" action="index.html" method="post" th:action="@{/login}">
IndexController
//去登录页
@GetMapping(value = {"/","/login"})
public String loginPage(){
return "login";
}
@PostMapping("/login")
public String main(User user, HttpSession session, Model model){
if(StringUtils.hasLength(user.getUsername()) && "12345".equals(user.getPassword())){
session.setAttribute("loginUser",user);
//登录成功重定向到main.html,防止表单重复提交
return "redirect:/main.html";
}else {
model.addAttribute("msg","账号密码错误");
return "login";
}
}
/*
跳转页面,
去main页面
*/
@GetMapping("/main.html")
public String mainPage(HttpSession session,Model model){
Object loginUser = session.getAttribute("loginUser");
if(loginUser != null){
return "main";
}else {
model.addAttribute("msg","请重新登录");
return "login";
}
}
登录时错误提示
<label style="color: red" th:text="${msg}"></label>
显示登录用户
<a href="#" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
<img src="images/photos/user-avatar.png" alt="" />
[[${session.loginUser.username}]]
<span class="caret"></span>
</a>
遍历数据
@GetMapping("/dynamic_table")
public String dynamic_table(Model model){
List<User> users = Arrays.asList(new User("zhangsan","123435"),
new User("lisi","1234567"),
new User("wangwu","12345678"),
new User("jahui","93749237"));
model.addAttribute("users",users);
return "table/dynamic_table";
}
<thead>
<tr>
<th>#</th>
<th>用户名</th>
<th>密码</th>
</tr>
</thead>
<tbody>
<tr class="gradeX" th:each="user,status:${users}">
<td th:text="${status.count}">aaa</td>
<td th:text="${user.username}">bbb</td>
<td>[[${user.password}]]</td>
</tr>
</tbody>
</table>
拦截器
@Configuration
public class AdminWebConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor())
.addPathPatterns("/**")
.excludePathPatterns("/","/login","/css/**","/fonts/**","/img/**","/js/**");
}
}
/*
目标方法执行之前
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//登录检查
HttpSession session = request.getSession();
Object loginUser = session.getAttribute("loginUser");
if(loginUser != null){
return true;
}
// session.setAttribute("msg","先登录");
// response.sendRedirect("/");
request.setAttribute("msg","请先登录");
request.getRequestDispatcher("/").forward(request,response);
return false;
}
/*
目标方法执行完成以后
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
/*
页面渲染之后
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
文件上传
@Controller
public class FormTestController {
@GetMapping("/form_layouts")
public String from_layouts() {
return "form/form_layouts";
}
/**
* 自动封装上传过来的文件
* @param email
* @param username
* @param headerImg
* @param photos
* @return
*/
@PostMapping("/upload")
public String upload(@RequestParam("email") String email,
@RequestParam("username") String username,
@RequestParam("headerImg") MultipartFile headerImg,
@RequestPart("photos") MultipartFile[] photos) throws IOException {
if(!headerImg.isEmpty()){
String originalFilename = headerImg.getOriginalFilename();
headerImg.transferTo(new File("D:\\abc\\"+originalFilename));
}
if(photos.length>0){
for(MultipartFile photo:photos){
if(!photo.isEmpty()){
String originalFilename = photo.getOriginalFilename();
photo.transferTo(new File("D:\\abc\\"+originalFilename));
}
}
}
return "main";
}
}
<form role="form" th:action="@{/upload}" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="exampleInputEmail1">邮箱</label>
<input type="email" name="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email">
</div>
<div class="form-group">
<label for="exampleInputPassword1">名字</label>
<input type="password" name="username" class="form-control" id="exampleInputPassword1" placeholder="Password">
</div>
<div class="form-group">
<label for="exampleInputFile">头像</label>
<input type="file" name="headerImg" id="exampleInputFile">
</div>
<div class="form-group">
<label for="exampleInputFile">生活照</label>
<input type="file" name="photos" multiple>
</div>
<div class="checkbox">
<label>
<input type="checkbox"> 检查
</label>
</div>
<button type="submit" class="btn btn-primary">提交</button>
</form>
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=100MB
原生servlet注入
@WebServlet(urlPatterns = "/my")
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("66666");
}
}
@ServletComponentScan(basePackages = "com.atjh.boot05webadmin")
@SpringBootApplication
public class Boot05WebAdminApplication {
public static void main(String[] args) {
SpringApplication.run(Boot05WebAdminApplication.class, args);
}
}