首页设置
映射
- 通过Controller
@Controller
public class IndexController {
@RequestMapping({"/index"})
public String tohello(){
return "index";
}
}
2.通过ViewControllers
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
}
}
index.html
可结合thymeleaf模板引擎使用需导入
<html lang="en" xmlns:th="http://www.thymeleaf.org">
通过th href:@{} 设置资源获取路径
配合server.servlet.context-path=/xx 可修改访问的url路径
<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta 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="">
<link rel="icon" href="/assets/img/favicon.ico">
<title>Signin Template for Bootstrap</title>
<!-- Bootstrap core CSS
th href:@{} 相对地址 可配合 server.servlet.context-path=/xx 修改访问的url-->
<link href="/assets/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="/assets/css/signin.css" rel="stylesheet">
</head>
<body class="text-center">
<form class="form-signin" th:action="@{/user/login}">
<img class="mb-4" src="/assets/img/favicon.ico" alt="" width="72" height="72">
<h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign in</h1>
<label for="inputEmail" class="sr-only" >Email address</label>
<input th:name="email" type="email" id="inputEmail" class="form-control" th:placeholder="#{login.email}" required autofocus>
<label for="inputPassword" class="sr-only">Password</label>
<input th:name="pwd" type="password" id="inputPassword" class="form-control" th:placeholder="#{login.password}" required>
<div class="checkbox mb-3">
<label>
<input type="checkbox" value="remember-me">[[#{login.Rememberme}]]
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">[[#{login.sign}]]</button>
<p class="mt-5 mb-3 text-muted">© 2017-2018</p>
</form>
</body>
</html>
国际化
在index页面添加
<!--设置路径 l代表语言参数-->
<a th:href="@{/index.html(l='zh_CN')}">中文</a>
<a th:href="@{/index.html(l='en_US')}">English</a>
在resources目录下新建i18n目录
并建立
login.properties
login_zh_CN.properties
和login_en_US.properties
添加对应代码
login.properties和login_zh_CN.properties:
login.tip=请登录
login.password=密码
login.Rememberme=记住我
login.email=邮箱
login.sign=登录
login_en_US.properties:
login.tip=Please sign in
login.password=Password
login.Rememberme=Remember me
login.email=Email Address
login.sign=Sign in
在application.properties中设置
spring.messages.basename=i18n.login,让spring从i18n.login目录下获取配置文件
springMVC国际化机制就是可以设置整个系统的运行语言,其定义了一个国际化支持接口LocaleResolver
建立MyLocaleResolver继承LocaleResolver重写resolveLocale,功能实现
public class MyLocaleResolver implements LocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest request) {
String langue = request.getParameter("l");
Locale locale = Locale.getDefault();
if (!StringUtils.isEmpty(langue)){
String[] split = langue.split("_");
//根据_分割l属性 获取到 语言 地区
locale = new Locale(split[0], split[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
}
}
注册到springioc中,方法名必须为localeResolver,@Bean会将方法名作为beanid
@Bean
public LocaleResolver localeResolver()
{
return new MyLocaleResolver();
}
登录功能
设置表单提交
<form class="form-signin" th:action="@{/user/login}">
编写对应的Controller
@Controller
public class LoginController {
@RequestMapping("/user/login")
public String Login(@RequestParam("email")String email,
@RequestParam("pwd") String password,
Model model){
if (!StringUtils.isEmpty(email)&&"123456".equals(password))
return "dashboard";
else{
model.addAttribute("msg","用户名或密码错误");
return "index";
}
}
}
为了数据回显设置,在输入框上方添加
<!-- th:if="${not #strings.isEmpty(msg) 判断msg是否为空 -->
<p style="color:red" th:text="${msg}"}"></p>
为了修改url是用户信息隐藏可以在MyMvcConfig中
registry.addViewController("/main").setViewName("dashboard");
并修改LoginController中的返回值为return "redirect:/main";
即
@Controller
public class LoginController {
@RequestMapping("/user/login")
public String Login(@RequestParam("email")String email,
@RequestParam("pwd") String password,
Model model){
if (!StringUtils.isEmpty(email)&&"123456".equals(password))
return "redirect:/main";
else{
model.addAttribute("msg","用户名或密码错误");
return "index";
}
}
}
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
registry.addViewController("/main").setViewName("dashboard");
}
@Bean
public LocaleResolver localeResolver()
{
return new MyLocaleResolver();
}
}
登录拦截
创建一个拦截器
public class MyLoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String loging =(String) request.getSession().getAttribute("loging");
if (!loging.isEmpty())
{
request.setAttribute("msg","请先登录");
request.getRequestDispatcher("/index.html").forward(request,response);
return false;
}
return true;
}
}
修改对应的Controller
@Controller
public class LoginController {
@RequestMapping("/user/login")
public String Login(@RequestParam("email")String email,
@RequestParam("pwd") String password,
Model model, HttpSession session){
if (!StringUtils.isEmpty(email)&&"123456".equals(password))
{
session.setAttribute("loging","success");
return "redirect:/main";
}
else{
model.addAttribute("msg","用户名或密码错误");
return "index";
}
}
}
注册到ioc容器中
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyLoginInterceptor()).
addPathPatterns("/**").
excludePathPatterns("/index.html","/main","/","/user/login","/assets/css/**","/assets/js/**","/assets/img/**");
}