indexControllor 业务层操作,处理前端信息和输出
登陆时需要在session中存入一个key值的名称。后面通过判断是否存在,过滤信息。 退出时通过key删除session中的此信息。
package com.csh.springboot03web.controller;
import org.springframework.boot.web.servlet.server.Session;
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 javax.servlet.http.HttpSession;
@Controller
public class indexControllor {
@RequestMapping("/index")
public String index(){
return "index";
}
@RequestMapping("/todashboard")
public String todashboard(@RequestParam("username") String username, @RequestParam("password") String password, HttpSession session, Model model){
System.out.println(username+" "+password);
if("admin".equals(username)&&("1234".equals(password))){
session.setAttribute("loginUser",session.getId());
System.out.println(session.getId());
return "dashboard";
}
model.addAttribute("msg","用户名或者密码错误");
return "index";
}
@RequestMapping("/out")
public String out(Model model,HttpSession session){
session.removeAttribute("loginUser");
model.addAttribute("msg","以安全退出,请重新登陆");
return "index";
}
}
国际化操作,继承接口,获取前端数据,返回处理好的Locale 数据 再传入bean 中注入。
package com.csh.springboot03web.config;
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 request) {
String l = request.getParameter("l");
Locale locale = Locale.getDefault();//如果沒有就是用默認的;
//如果請教的信息包含國際化内容,
if(!StringUtils.isEmpty(l)){
String[] s = l.split("_");
//國家地區
locale=new Locale(s[0],s[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
}
}
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
这里是mvc层的bean 注入 可以添加试图控制, 拦截器,国际化操作的bean注入或者重写原始方法。
package com.csh.springboot03web.config;
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.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
//配置类 视图解析器
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
//在此可以自定义一些视图解析器。定义名称跳转
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
}
/* // 用注解@bean 为spring中注入配置信息 必须把定义的组件放到bean中才可以生效*/
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
//把过滤器注入到bean容器中,让其生效,在这里可以设定 过滤器生效的位置和排除一些指定的地址。
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
.excludePathPatterns("/index.html","/todashboard","/css/**","/js/**","/img/**","/out");
}
}
前端html页面中 导入static 数据 用 @{} properties 或者yml文件中的数据用出入 #{}
导入 后端传出数据 用${}
<!DOCTYPE html>
<html lang="zh" xmlns="http://www.w3.org/1999/xhtml"
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 Bootstrap</title>
<!-- Bootstrap core CSS -->
<link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
<!-- Custom styles for this template -->
<link th:href="@{/css/signin.css}" rel="stylesheet">
</head>
<body class="text-center">
<form class="form-signin" action="todashboard">
<img class="mb-4" th:src="@{/img/bootstrap-solid.svg}" alt="" width="72" height="72">
<h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}"></h1>
<p style="color: red" th:text="${msg}" th:if="msg!=null"></p>
<label class="sr-only" >用户名</label>
<input type="text" class="form-control" th:name="username" th:placeholder="#{login.username}" required="" autofocus="">
<label class="sr-only" >密码</label>
<input type="password" class="form-control" th:name="password" th:placeholder="#{login.password}" required="">
<div class="checkbox mb-3">
<label>
<input type="checkbox" value="remember-me" th:text="#{login.remember}">
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{login.btn}">Sign in</button>
<p class="mt-5 mb-3 text-muted">© 2017-2018</p>
<a th:href="@{/index.html(l='zh_CN')}" class="btn btn-sm">中文</a>
<a th:href="@{/index.html(l='en_US')}" class="btn btn-sm">English</a>
</form>
</body>
</html>