热门IT资讯网

Spingboot+thymeleaf+ajax简单登录实现

发表于:2024-11-28 作者:热门IT资讯网编辑
编辑最后更新 2024年11月28日,登录页面

登录页面

                                        

服务端页面

import com.jth.pojo.ResultInfo;import com.jth.pojo.User;import lombok.extern.log4j.Log4j;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.*;import javax.servlet.http.HttpSession;import javax.validation.Valid;/** * @author liwen406 * @Title: DispatcherController * @Description: * @date 2019/5/8 / 20:47 */@Log4j@Controllerpublic class DispatcherController {    @GetMapping("/{page}")    public String pagea(@PathVariable String page) {        return page;    }    @ResponseBody    @PostMapping("/dologin")    public Object login(@Valid @RequestBody User user, HttpSession session) {        log.info("提交表单数据为:"+user);        ResultInfo result = new ResultInfo();        if (user.getLoginacct().equals("admin") && user.getUserpswd().equals("admin")) {            result.setSuccess(true);        } else {            result.setSuccess(false);        }        return result;    }}

pojo

import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;import lombok.experimental.Accessors;/** * @author liwen406 * @Title: ResultInfo * @Description:  回调方法数据 * @date 2019/5/8 / 21:20 */@Data@NoArgsConstructor@AllArgsConstructor@Accessors(chain = true)public class ResultInfo {    private boolean success;    private Object data;}import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;import java.io.Serializable;/** * @author liwen406 * @Title: User * @Description: * @date 2019/5/8 / 20:39 */@Data@NoArgsConstructor@AllArgsConstructorpublic class User implements Serializable {    private Integer id;    private String username;    private String loginacct;    private String userpswd;    private String email;    private String createtime;}

结果

2019-05-09 12:44:35.218  INFO 11284 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'2019-05-09 12:44:35.218  INFO 11284 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'2019-05-09 12:44:35.226  INFO 11284 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 8 ms[SeleniumAutoConsole] INFO [http-nio-8080-exec-1] com.jth.controller.DispatcherController.login(31) | 提交表单数据为:User(id=null, username=null, loginacct=121, userpswd=admin, email=null, createtime=null)2019-05-09 12:44:37.361  INFO 11284 --- [      Thread-47] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
0