首页 > 其他分享 >springmvc的简单使用(3)

springmvc的简单使用(3)

时间:2022-08-19 17:11:36浏览次数:45  
标签:return String springmvc request 简单 mydate sf 使用 public

一:日期处理:

1:日期的提交处理:

 单个日期处理:

要使用注解,并且注解要搭配springmvc文件中的<annotationdriven>

<mvc:annotation-driven></mvc:annotation-driven>
<form action="${pageContext.request.contextPath}/mydate.do">
日期:<input type="date" name="mydate">
<input type="submit" name="提交">
</form>
SimpleDateFormat sf =new SimpleDateFormat("yyyy-MM-dd");
@RequestMapping("/mydate")
public String date(
@DateTimeFormat(pattern = "yyyy-MM-dd")
Date mydate) {

System.out.println(mydate);
System.out.println(sf.format(mydate));
return "main";
}

 全局日期处理:

SimpleDateFormat sf =new SimpleDateFormat("yyyy-MM-dd");
//注册一个全局的日期处理注解
@InitBinder
public void initBinder(WebDataBinder dataBinder){
dataBinder.registerCustomEditor(Date.class,new CustomDateEditor(sf,true));
}
@RequestMapping("/mydate")
public String date(Date mydate) {

System.out.println(mydate);
System.out.println(sf.format(mydate));
return "main";
}

2:日期的显示处理

在页面上显示正常的日期,必须使用JSTL。步骤:添加依赖jstl,在页面上导入标签库,使用标签显示数据

如果是单个的日期对象,可以直接转换为格式化的字符串进行显示

如果是list中的实体类对象的成员变量为日期类型,则必须使用jstl进行显示

<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h2>学生集合</h2>
<table width="800px" border="1">
<tr>
<th>姓名</th>
<th>生日</th>
</tr>
<c:forEach items="${stu}" var="stu">
<tr>
<td>${stu.name}</td>
<td>${stu.birthday}--------<fmt:formatDate value="${stu.birthday}" pattern="yyyy-MM-dd" ></fmt:formatDate></td>
</tr>
</c:forEach>
</table>
</body>
@RequestMapping("/list")
public String date(HttpServletRequest request) throws ParseException {
Student s1 = new Student("张三", sf.parse("2021-1-1"));
Student s2 = new Student("李四", sf.parse("2021-2-1"));
Student s3 = new Student("王五", sf.parse("2021-3-1"));
List<Student> list=new ArrayList<Student>();
list.add(s1);
list.add(s2);
list.add(s3);
request.setAttribute("stu",list);

return "show";
}

三:springmvc的拦截器:

<form action="${pageContext.request.contextPath}/login">
姓名:<input type="text" name="name">
密码:<input type="password" name="pwd">
<input type="submit" name="提交">
</form>
${msg}
@RequestMapping("/showmain")
public String showmain(){
System.out.println("访问login页面");
return "main";

}

@RequestMapping("/showlogin")
public String showlogin(){
System.out.println("访问login页面");
return "login";

}

@RequestMapping("/login")
public String login(String name,String pwd,HttpServletRequest request){
if((name.equals("ztb")) && (pwd.equals("123456"))){
System.out.println("登陆成功");
request.getSession().setAttribute("user",name);
return "main";
}else {
request.setAttribute("msg","用户名或密码不正确,请重新登录");
return "login";
}

}
在没有设置拦截器的时候,访问shownmain是能够直接访问到的

public class LlginInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//是否登陆过的判断
if(request.getSession().getAttribute("user")==null){
//此时没登陆,大回到登陆页面,并给出提示
request.setAttribute("msg","还没有登陆,请先去登录");
request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request,response);
return false;
}
return true;//否则放行
}
}
在springmvc中注册拦截器:
<!--    注册拦截器-->
<mvc:interceptors>
<mvc:interceptor>
<!-- 隐射拦截的请求(拦截所有请求)-->
<mvc:mapping path="/**"/>
<!-- 设置放行的请求-->
<mvc:exclude-mapping path="/showlogin"/>
<mvc:exclude-mapping path="/login"/>
<!-- 配置具体的拦截器实现功能的类-->
<bean class="com.ztb.controller.LlginInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>

 设置完拦截器后,再次访问showmain需要先登陆验证!

标签:return,String,springmvc,request,简单,mydate,sf,使用,public
From: https://www.cnblogs.com/zhangtaibing/p/16602672.html

相关文章

  • leetcode 225. Implement Stack using Queues 用队列实现栈(简单)
    一、题目大意请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop和empty)。实现MyStack类:voidpush(intx)将元素x压入栈顶。......
  • swoole的使用场景
    一、TCP服务器   二、UDP服务器   三、HTTP服务器   四、WebSocket服务器  五、物联网服务器  六、执行异步任务 七、协程 ......
  • 使用arthas监控慢接口记录
         公司明天有个直播,考虑到上次出现过问题,有个自动化推荐的接口需要做优化。先是下载项目后看了下,流程太长,也看不出来哪里出问题了。本地刚好可以测试,使用artha......
  • 使用a链接实现点击下载网络图片
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content=......
  • git的使用
    1.下载并配置gitconfig--globaluser.name"用户名"gitconfig--globaluser.email"邮箱"2.创建gitinit3.移到暂存区gitadd.4.提交到本地gitcommit-m'提......
  • 逻辑分析仪的简单使用介绍(附带i2c、串口、spi数据分析)
    本次文章给大家介绍一种便宜好用的协议分析工具,逻辑分析仪,首先声明,小飞哥作这篇介绍文章,不是为了打广告哈,实在是因为这个小玩意很好用,有些小伙伴还不太清楚该如何使用!!!废话......
  • python将代码打包成whl或者压缩文件使用pip安装
    需要条件:安装setuptools,wheel创建目录helloworld—init.py—代码1.py—代码2.pysetup.py编辑__init__.pyfromhelloworldimport代码1,代码2在helloworld同级下......
  • Net core中使用System.Drawing对上传的图片流进行压缩
    由于netcore中默认没有System.Drawing,可以通过nuget下载一个来代替System.Drawing.Common 直接压缩图片///<summary>///图片压缩///</summary>///<paramnam......
  • 三.Suricata的安装与使用
    一.IDS功能通过监听网卡流量并匹配规则引擎进行入侵实时检测和预警,检测手段上也与Wazuh比较类似 二.IPS功能与Wazuh的主动响应的功能不一样,IPS功能并不需要对防火墙进......
  • 打印流_概述和使用
    PrintStream打印流PrintStream为其他输出流添加了功能,使他们能够方便地打印各种数据值表示形式PrintStream特点:只负责数据的输出,不负责数据的读取与其他输出流不同不......