一:日期处理:
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