毕设-离不开的登录页

来源:互联网 发布:知乎精华故事 编辑:程序博客网 时间:2024/06/05 23:52

计划:登录页使用js验证;配置数据库;使用Mybatis从数据库查数据;使用Shiro验证登录

1、登录页

去除@ResponseBody(返回Json字符串),当返回值为String类型,SpringBoot会使用DispatcherServlet(自动配置)找到要返回的页面,我使用Thymeleaf模板,它提供了一套Spring的集成

Thymeleaf offers a set of Spring integrations that allow you to use it as a full-featured substitute for JSP in Spring MVC applications.

These integrations will allow you to:

  • Make the mapped methods in your Spring MVC @Controller objects forward to templates managed by Thymeleaf, exactly like you do with JSPs.
  • Use Spring Expression Language (Spring EL) instead of OGNL in your templates.
  • Create forms in your templates that are completely integrated with your form-backing beans and result bindings, including the use of property editors, conversion services and validation error handling.
  • Display internationalization messages from messages files managed by Spring (through the usual MessageSource objects).
chrome翻译下-- 反正就是用的时候很方便,用过的都说好

但是。写一个LoginController控制器处理 http://localhost:8080/login/login 请求

/** * @Author yinyunqi * @date 2017年12月20日 * @Content 登录控制 */package com.yongqi.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;@Controller@RequestMapping("/login")public class LoginController {   @RequestMapping("/login")    public String doLogin() {return "login";    }}

然后添加模板依赖

    <!-- Thymeleaf模板 -->    <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId>    </dependency>
泪奔,如果你出现任何问题,无法显示页面,报一些看不懂的错,比如

Could not resolve view with name 'login' in servlet with name 'dispatcherServlet' 等等

只有一种出路。关掉,重启(或许clean一下项目),正常,耸肩,我想这是热启动没有将Thymeleaf加载进去,需要注意的是模板默认使用的配置是

页面放在templates下,静态文件在static下,如果需要自定义,就在application.properties中配置

#MVC配置spring.mvc.view.prefix=classpath:/XX/spring.mvc.view.suffix=.html
login.html如下

<?xml version="1.0" encoding="utf-8" ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>后台登录</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body><img src="/img/design02.png" alt="" /> <form action="" method="post">    <p>账号:<input type="text" name="username" /></p>    <p>密码:<input type="text" name="password" /></p>    <p><input type="submit" value="登录"/></p></form> </body></html>
页面



这样html页面和静态资源都可以用了

接着写验证,写完该吃饭了,啧

//处理登录验证http://localhost:8080/login/verify    @RequestMapping("/verify")    public String doVerify(HttpServletRequest request) {System.out.println(request.getParameter("username"));if (request.getParameter("username").equals("1")) {    return "success";}else {    return "fail";}    }
要注意的是要在templates中创建两个返回的页面,不然报错,也可能位置错误,细心,耐心,不行clean 重新编译

前台添加了请求地址

<?xml version="1.0" encoding="utf-8" ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>后台登录</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body><form action="/login/verify" method="post">    <p>账号:<input type="text" name="username" /></p>    <p>密码:<input type="text" name="password" /></p>    <p><input type="submit" value="登录"/></p></form> </body></html>
现在情况是:username输入1进success.html,否则fail.html

此时项目结构图


不过还是乱,为了可持续发展,晚上回去整理,然后将页面画好看点








原创粉丝点击