开发网站通过第三方微信认证登录开发费用,软件公司工资高吗,舟山论坛网站建设,岫岩网站建设16.1 修改index.html中表单跳转的地址 将action的地址改为user/login#xff0c;意思是点击提交按钮后#xff0c;就会跳转到user/login地址#xff0c;然后只要用Controller类的RequsetMapping去接这个地址就行了。
body classtext-centerform cl…
16.1 修改index.html中表单跳转的地址 将action的地址改为user/login意思是点击提交按钮后就会跳转到user/login地址然后只要用Controller类的RequsetMapping去接这个地址就行了。
body classtext-centerform classform-signin th:action{user/login}img classmb-4 th:src{/img/bootstrap-solid.svg} alt width72 height72h1 classh3 mb-3 font-weight-normalPlease sign in/h1label classsr-onlyUsername/labelinput typetext classform-control placeholderUsername required autofocuslabel classsr-onlyPassword/labelinput typepassword classform-control placeholderPassword requireddiv classcheckbox mb-3labelinput typecheckbox valueremember-me Remember me/label/divbutton classbtn btn-lg btn-primary btn-block typesubmitSign in/buttonp classmt-5 mb-3 text-muted© 2017-2018/pa classbtn btn-sm中文/aa classbtn btn-smEnglish/a/form/body
16.2 controller类 当访问localhost:8080/user/login时会返回在页面返回一字符串本例是helloResponseBody是作用在方法上的ResponseBody 表示该方法的返回结果直接写入 HTTP response body 中。
package jiang.com.springbootstudy.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;Controller
public class LoginController {RequestMapping(/user/login)ResponseBodypublic String login(){return hello;}
}以上代码只是简单实现了点击按钮后访问请求最后响应了一个字符串的过程。该项目中不需要使用ResponseBody注解实现请求后的数据转发合页面跳转即可。注意之前的表单中input没有name属性只有placeholder属性改为name属性即可然后相应的属性值改下因为后面需要使用RequestParm注解来接受表单数据最终代码如下
package jiang.com.springbootstudy.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.thymeleaf.util.StringUtils;Controller
public class LoginController {RequestMapping(/user/login)public String login(RequestParam(username) String username,RequestParam(password) String password,Model model){if (!StringUtils.isEmpty(username)123456.equals(password)){return redirect:/main.html;}else {model.addAttribute(msg,用户名或密码错误);return index;}}
}这里使用了重定向redirect return redirect:/main.html会使访问localhost:8080/user.login地址后网页被转移到localhost:8080/main.html地址注意这个是main.html是虚拟的地址并不是转发到main.html页面的意思因此需要使用mvc的配置类对请求就行转发转发到实际地址中。最终如果访问localhost:8080/main.html地址就会打开一个dashboard.html页面。mvc配置类代码如下
package jiang.com.springbootstudy.config;import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;Configuration
public class MyMvcConfig implements WebMvcConfigurer {Overridepublic void addViewControllers(ViewControllerRegistry registry) {registry.addViewController(/).setViewName(index); //setViewName后面不用加htmlregistry.addViewController(/index.html).setViewName(index);registry.addViewController(/main.html).setViewName(dashboard); // 前面的main.html是假的只是一种请求后面才是真的输入请求后会跳转的地方是后面的html}
}16.3 修改index页面 根据Controller类的处理逻辑如果用户名和密码错误就会返回indem.html页面并转发msg数据。转发后需要在相应位置上把msg数据显示出来。需要在相应位置上加一行html代码并且对p标签的样式进行了更改需要增加的html代码如下
link th:href{/css/mystyle.css} typetext/css relstylesheet
p classp-msg th:text${msg} th:if${not #strings.isEmpty(msg)}/p css样式的代码如下
p.p-msg{color: red;
} 输入错误后转发到index.html的结果 16.4