1.登陆页面
只要是路径就用@
<form class="loginForm" th:action="@{/login}" method="post">
<div class="inputbox">
<label for="user" th:text="#{login.username}">Username</label>
<input id="user" type="text" name="username" required/>
</div>
<div class="inputbox">
<label for="mima" th:text="#{login.password}">Password</label>
<input id="mima" type="password" name="password" required/>
</div>
<div class="subBtn" >
<input type="checkbox">[[#{login.remember}]]
</div>
<br/>
<div class="subBtn">
<input type="submit" value="Sign" th:value="#{login.submit}"/>
<input type="reset" value="Reset" th:value="#{login.reset}"/>
</div>
<br/>
<div style="margin-left: 100px;">
<a href="#" th:href="@{index.html(l='zh_CN')}">中文</a>
<a href="" th:href="@{index.html(l='en_US')}" >English</a>
</div>
</form>
关键-》form中action和method 用户名和密码中的name
2.controller层
“123"写在前面是为了防止空指针异常
若不使用重定向,刷新页面会弹出一个框,让你确定 ;redirect:后面不能有空格,否则浏览器的url会%29%这种样子
package com.cc.springboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;
import java.util.Map;
@Controller
public class LoginController {
@PostMapping("/login")
public String login(String username, String password, Map<String,Object>map){
// 判断用户名不为空,且密码为123
if(!StringUtils.isEmpty(username) && "123".equals(password)){
//登录成功
//重定向 redirect: 可以重定向到任何一个请求中(包括其他项目),地址栏改变
// return "main/index"; //在静态资源中查找文件
return "redirect:/main.html"; //导航栏地址,不是在静态资源中查找文件,需要视图
}
// 登陆失败
map.put("msg","用户名或密码错误");
return "main/login";
}
}
config/ webMvcConfigurer 视图:(Ctrl+D 复制粘贴在下一行)
package com.cc.springboot.config;
import com.cc.springboot.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.WebMvcConfigurer;
@Configuration
public class MySpringMvcConfigurer {
@Bean
// 跳转首页
public WebMvcConfigurer webMvcConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("main/login");
registry.addViewController("/index.html").setViewName("main/login");
registry.addViewController("/main.html").setViewName("main/index");
}
};
}
@Bean
// 切换语言 自己定义的区域解析器
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
}