attention:
想要把网站的图标换掉就在static下放一个 favicon.ico 的图标图片即可
Spring MVC 5.0.5官方文档
static和template
springboot整合了springmvc的拦截功能。拦截了所有的请求。默认放行的资源是:resources/static/ 目录下所有静态资源。(不走controller控制器就能直接访问到资源)。
html页面如果放在resources/templates目录下,则需要走controller控制器,controller放行,允许该资源访问,该资源才能被访问到。否则就会报404错误(它不可以直接被访问)。
因此默认时访问public目录下的index.html
通过配置类来extends WebMvcConfigurationSupport实现访问template目录下的login.html下
(当然通过controller来访问也是可以的,但是Spring Boot推荐采用配置类来实现)
重点提醒一下,一定要实现WebMvcConfigurer接口来访问,不要继承Support类
package com.example.webexpriment.config;
import com.example.webexpriment.component.MyLocaleResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//浏览器发送/gary请求,来到/success页面
registry.addViewController("/").setViewName("login");
System.out.println("WOC");
registry.addViewController("/index.html").setViewName("login");
}
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
}
搞定玩就可以访问主页了
附上Controller的访问方法
@RequestMapping({"/","/index.html"})//数组
public String index()
{
return "login";
}
在html先里面修改 记得先导入模板引擎的提示功能xmlns:th=“http://www.thymeleaf.org”
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-u2Ay0y1I-1592971582236)(C:\Users\Gary\AppData\Roaming\Typora\typora-user-images\image-20200623112714362.png)]
applicaltion.properties文件下加上
spring.messages.basename=i18n.login
html标签
?l=zh_CN在thymeleaf中的写法:
The th:href attribute allows us to (optionally) have a working static href attribute in our template, so that our template links remained navigable by a browser when opened directly for prototyping purposes. (摘自官方文档)
<a class="btn btn-sm" th:href="@{/index.html(l='zh_CN')}">中文</a>
<a class="btn btn-sm"th:href="@{/index.html(l='en_US')}">English</a>
thymeleaf模板引擎来取国际化的值
参见thymeleaf官方文档
Variable Expressions: ${...}
Selection Variable Expressions: *{...}
Message Expressions: #{...}
Link URL Expressions: @{...}
Fragment Expressions: ~{...}
修改example:
<h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign inh1>
行内写法
<label>
<input type="checkbox" value="remember-me" > [[#{login.remember}]]
label>
提醒一下,记得设置IDE把编码自动转化为ASCII码
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>Signin Template for Bootstraptitle>
<link href="asserts/css/bootstrap.min.css" rel="stylesheet">
<link href="asserts/css/signin.css" rel="stylesheet">
head>
<body class="text-center">
<form class="form-signin" th:action="@{/user/login}" action="dashboard.html">
<img class="mb-4" src="asserts/img/bootstrap-solid.svg" alt="" width="72" height="72">
<h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign inh1>
<label class="sr-only" th:text="#{login.username}">Usernamelabel>
<input type="text" name="username" class="form-control" th:placeholder="#{login.username}" placeholder="Username" required="" autofocus="">
<label class="sr-only" th:text="#{login.password}">Passwordlabel>
<input type="password" name="password" class="form-control" th:placeholder="#{login.password}" placeholder="Password" required="">
<div class="checkbox mb-3">
<label>
<input type="checkbox" value="remember-me" > [[#{login.remember}]]
label>
div>
<button class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{login.btn}">Sign inbutton>
<p class="mt-5 mb-3 text-muted">© 2017-2018p>
<a class="btn btn-sm" th:href="@{/index.html(l='zh_CN')}">中文a>
Englisha>
form>
body>
html>
需要自己写
package com.example.webexpriment.component;
import org.apache.tomcat.jni.Local;
import org.springframework.web.servlet.LocaleResolver;
import org.thymeleaf.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;
/**
* 可以在链接上携带区域信息
*/
public class MyLocaleResolver implements LocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest httpServletRequest) {
String l = httpServletRequest.getParameter("l");
System.out.println(l);
//区域信息
Locale locale= Locale.getDefault();
//用StringUtils工具判断一下先
if(!StringUtils.isEmpty(l))
{
String[] s = l.split("_");
locale = new Locale(s[0], s[1]);
}
System.out.println(locale);
return locale;
}
@Override
public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
}
}
StringUtils
然后再配置类当中加入到容器当中 @Bean
package com.example.webexpriment.config;
import com.example.webexpriment.component.MyLocaleResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//浏览器发送/gary请求,来到/success页面
registry.addViewController("/").setViewName("login");
System.out.println("WOC");
registry.addViewController("/index.html").setViewName("login");
}
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
}
系统默认:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-neI2zIcq-1592971582238)(C:\Users\Gary\AppData\Roaming\Typora\typora-user-images\image-20200623120325447.png)]
中文:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yFqi2MkG-1592971582239)(C:\Users\Gary\AppData\Roaming\Typora\typora-user-images\image-20200623120649430.png)]
English:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rV2CGQyR-1592971582241)(C:\Users\Gary\AppData\Roaming\Typora\typora-user-images\image-20200623120706716.png)]
attention:
Map
修改访问的方式
<form class="form-signin" action="dashboard.html" th:action="@{/user/login}" method="post">
!!!修改html,input上面加上name属性
<input type="text" name="username" class="form-control" th:placeholder="#{login.username}" placeholder="Username" required="" autofocus="">
<input type="password" name="password" class="form-control" th:placeholder="#{login.password}" placeholder="Password" required="">
不加的话是编译器获取不到值
然后处理请求
package com.example.webexpriment.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.thymeleaf.util.StringUtils;
import java.util.Map;
@Controller
public class LoginController {
@PostMapping(value="/user/login")
// @RequestMapping(value="/user/login",method = RequestMethod.POST) 用上面更加方便
//从请求参数上获取用户名和密码
public String login(@RequestParam("username") String username,
@RequestParam("password")String password,
Map<String,Object>map){
if(!StringUtils.isEmpty(username)&&"123456".equals(password))
{
//登陆成功
return "dashboard";
}
else{
//登录失败
map.put("msg","用户名密码错误");
return "login.html";
}
}
}
[1.$符号取上下文中的变量:
2.#](https://s.weibo.com/weibo?q=%23、*+和%24的区别: 1.%24符号取上下文中的变量: 符号取thymeleaf工具中的方法、文字消息表达式:
Welcome to our grocery store!
3. *{…}选择表达式一般跟在th:object后,直接选择object中的属性
applicaltion.properties下加上
#禁用模板引擎的缓存
spring.thymeleaf.cache=false
再ctrl+F9来刷新
p标签和if条件
参见官网文档
<p style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}">p>
防止表单的重复提交
package com.example.webexpriment.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.thymeleaf.util.StringUtils;
import java.util.Map;
@Controller
public class LoginController {
// @RequestMapping(value="/user/login",method = RequestMethod.POST) 用上面更加方便
//从请求参数上获取用户名和密码
@PostMapping(value="/user/login")
public String login(@RequestParam("username") String username,
@RequestParam("password")String password,
Map<String,Object>map){
if(!StringUtils.isEmpty(username)&&"123456".equals(password))
{
//登陆成功
System.out.println("登录页面成功");
//重定向,防止表单重复提交
return "redirect:/main.html";
}
else{
//登录失败
map.put("msg","用户名密码错误");
return "login";
}
}
}
package com.example.webexpriment.config;
import com.example.webexpriment.component.MyLocaleResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//浏览器发送/gary请求,来到/success页面
registry.addViewController("/").setViewName("login");
registry.addViewController("/index.html").setViewName("login");
registry.addViewController("/main.html").setViewName("dashboard");
}
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CQLaC6ly-1592971582241)(C:\Users\Gary\AppData\Roaming\Typora\typora-user-images\image-20200624111600067.png)]
1)Controller当中post后传入一个session判断输入了没有
package com.example.webexpriment.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.thymeleaf.util.StringUtils;
import javax.servlet.http.HttpSession;
import java.util.Map;
@Controller
public class LoginController {
// @RequestMapping(value="/user/login",method = RequestMethod.POST) 用上面更加方便
//从请求参数上获取用户名和密码
@PostMapping(value="/user/login")
public String login(@RequestParam("username") String username,
@RequestParam("password")String password,
Map<String,Object>map, HttpSession session){
if(!StringUtils.isEmpty(username)&&"123456".equals(password))
{
//登陆成功
System.out.println("登录页面成功");
session.setAttribute("loginUser",username);
//重定向,防止表单重复提交
return "redirect:/main.html";
}
else{
//登录失败
map.put("msg","用户名密码错误");
return "login";
}
}
}
2)添加新的类,实现接口
package com.example.webexpriment.component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginHandlerInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//预检查
Object user = request.getSession().getAttribute("loginUser");
if(user==null)
{
System.out.println("username为空");
//拦截,返回登录页
request.setAttribute("msg","没有权限请先登录");
request.getRequestDispatcher("/index.html").forward(request,response);
return false;
}
else{
System.out.println(user);
return true;
//放行,已登录了
}
}
@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 {
}
}
3)配置类重写方法
@Override
//注册拦截器
public void addInterceptors(InterceptorRegistry registry) {
//addPathPatterns()拦截哪些请求/**全部拦截
registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/index.html","/","/user/login");
//静态资源Spring Boot自己已经搞定映射了,无需多管了
}
1)、RestfulCRUD:CRUD满足Rest风格;
URI: /资源名称/资源标识 HTTP请求方式区分对资源CRUD操作
普通CRUD(uri来区分操作) | RestfulCRUD | |
---|---|---|
查询 | getEmp | emp—GET |
添加 | addEmp?xxx | emp—POST |
修改 | updateEmp?id=xxx&xxx=xx | emp/{id}—PUT |
删除 | deleteEmp?id=1 | emp/{id}—DELETE |
2)、实验的请求架构;
实验功能 | 请求URI | 请求方式 |
---|---|---|
查询所有员工 | emps | GET |
查询某个员工(来到修改页面) | emp/1 | GET |
来到添加页面 | emp | GET |
添加员工 | emp | POST |
来到修改页面(查出员工进行信息回显) | emp/1 | GET |
修改员工 | emp | PUT |
删除员工 | emp/1 | DELETE |
3)、员工列表:
未完待续