javaweb项目开发案例(java项目开发案例经典)

发布日期:2024-05-19 01:16:07     手机:https://m.xinb2b.cn/wenda/news4782.html    违规举报
核心提示:spring.messages.basename=i18n.login 这样就相当于把国际化资源文件让SpringBoot配置的ResourceBundleMessageSource管理了起来 2021新版IDEA修改全部默认配置中的文件

javaweb项目开发案例(java项目开发案例经典)

spring.messages.basename=i18n.login

这样就相当于把国际化资源文件让SpringBoot配置的ResourceBundleMessageSource管理了起来 2021新版IDEA修改全部默认配置中的文件编码模式,解决properties配置文件乱码问题

通过以上设置,我们根据浏览器语言的设置切换国际化,下面展示原理:

SpringMVC的自动配置中有默认的区域信息解析器===>国际化Locale(区域信息对象),LocaleResolver(获取区域信息对象) 点击链接实现国际化切换 1.编写自己的区域信息解析器,并放到容器中

自定义区域信息解析器:

public class MyLocaleResolver implements LocaleResolver

{

@Override

public Locale resolveLocale(HttpServletRequest Request) {

String l=Request.getParameter(“l”);

Locale locale=Locale.getDefault();//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) {

}

}

SpringMVC扩展类: 负责将自定义的组件加入到容器中

//使用WebMvcConfigurerAdapter可以来扩展SpringMvc的功能

@Configuration

public class myConfig extends WebMvcConfigurerAdapter

{

//所有的WebMvcConfigurerAdapter组件都会一起起作用

@Bean//将容器注册在容器中

public WebMvcConfigurerAdapter addViewControllers()

{

WebMvcConfigurerAdapter adapter=new WebMvcConfigurerAdapter() {

@Override

public void addViewControllers(ViewControllerRegistry registry) {

registry.addViewController(“/”).setViewName(“index”);

registry.addViewController(“/index.html”).setViewName(“index”);

}

};

return adapter;

}

@Bean

//在SpringMVC扩展类中,将刚才写的区域信息解析器放到容器中

public LocaleResolver localeResolver()

{

return new MyLocaleResolver();

}

}

效果展示:

登录模块

===================================================================

SpringMVC新特性支持的Rest风格的注解

@RestController注解

@RestController等常见注解

@PostMapping, @GetMapping, @PutMapping, @DeleteMapping四个支持Rest风格的注解 模板引擎页面修改后要时时生效==>禁用掉模板引擎的缓存+重新编译 在全局配置文件中禁用掉模板引擎的缓存

#禁用掉模板引擎的缓存,这样页面内容一修改,就可以看到修改后的效果

spring.thymeleaf.cache=false

IDEA在项目运行期间,不会让我们对页面的修改生效,如果想让我们对页面的修改时时生效,第一步禁用缓存,第二步按住ctrl+f9重新编译当前页面

Thymeleaf 内置对象和内置方法

Thymeleaf 内置对象和内置方法

转发到某一页面导致的表单重复提交问题

解决表单重复提交问题

登录成功后,要防止表单被重复提交,可以重定向到主页

拦截器进行登录检查,防止不经过登录直接来到某一页面 SpringBoot已经做好了静态资源的映射 1.自定义登录拦截器,通过获取session中存放的数据,来判断是否已经登录过

public class LoginHanlderIntercept implements HandlerInterceptor

{

@Override

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

Object user=request.getSession().getAttribute(“loginUser”);

if(user==null)

{

//未登陆,返回登陆页面

request.setAttribute(“msg”,“没有权限请先登陆”);

request.getRequestDispatcher(“/index.html”).forward(request,response);

return false;

}

else

{

//已登陆,放行请求

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 {

//登陆后,将之前存储在session里面的登录凭证销毁,无论是否存在凭证,都执行销毁操作

request.getSession().removeAttribute(“loginUser”);

}

}

2.如果登录成功,那么往session中存放一个username作为登录凭证

@Controller

public class LoginController

{

@PostMapping(“/user/login”)

public String Login(@RequestParam(“username”)String username,

@RequestParam(“password”)String password

, Map<String,Object> map, HttpSession session)

{

if(username.equals(“大忽悠”)&&“123456”.equals(password))

{

session.setAttribute(“loginUser”,username);

//登录成功

return “redirect:/main.html”;

}

//登录失败

map.put(“msg”,“用户名或密码错误”);

return “index”;

}

}

3.在springmvc扩展类中将自定义的拦截器进行注册

//使用WebMvcConfigurerAdapter可以来扩展SpringMvc的功能

@Configuration

public class myConfig extends WebMvcConfigurerAdapter

{

//所有的WebMvcConfigurerAdapter组件都会一起起作用

@Bean//将容器注册在容器中

public WebMvcConfigurerAdapter addViewControllers()

{

WebMvcConfigurerAdapter adapter=new WebMvcConfigurerAdapter() {

@Override

public void addViewControllers(ViewControllerRegistry registry) {

registry.addViewController(“/”).setViewName(“index”);

registry.addViewController(“/index.html”).setViewName(“index”);

registry.addViewController(“/main.html”).setViewName(“success”);

}

@Override

public void addInterceptors(InterceptorRegistry registry) {

registry.addInterceptor(new LoginHanlderIntercept()).addPathPatterns(“

@RequestMapping(“handle01”)

public String handle01(){

System.out.println(“handle01…”);

return “forward:/hello.jsp”;

}

@RequestMapping(“handle02”)

public String handle02(){

System.out.println(“handle02…”);

return “forward:/handle01”;

}

@RequestMapping(“handle03”)

public String handle03(){

System.out.println(“handle03…”);

return “redirect:/hello.jsp”;

}

@RequestMapping(“handle04”)

public String handle04(){

System.out.println(“handle04…”);

return “redirect:/handle03”;

}

}

SprinBoot中的日期格式化问题

SpringBoot底层日期格式化原理: 默认有一个日期格式化器:

![在这里插入图片描述](
https://img-blog.csdnimg.cn/de16e8c47d51400989e3c05bd813b6ae.wf?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》 【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUzMTU3MTcz,size_16,color_FFFFFF,t_70)

默认使用的日期格式是/方式,如果后台接收到前台的日期格式不是,那么就会报错: 我们可以在配置文件中进行日期格式修改,替换默认的日期格式:

spring.mvc.date-format=yyyy-MM-dd

Thymeleaf 日期格式化处理

${#dates.format(key)}

${#dates.format(key, ‘yyyy-MM-dd HH:mm:ss’)}

格式化传递过来的 Date 对象,如果没有指定时间格式,将使用浏览器当前使用的时间格式

Thymeleaf 日期格式化处理

JQuery中的submit事件来提交表单,也可以阻止表单的提交
 
 
本文地址:https://wenda.xinb2b.cn/news4782.html,转载请注明出处。

推荐图文
推荐问答知道
网站首页  |  关于我们  |  联系方式  |  使用协议  |  版权隐私  |  网站地图  |  违规举报  |  蜀ICP备18010318号-4  |  百度地图  | 
Processed in 0.104 second(s), 91 queries, Memory 0.47 M