SpringMVC的四种跳转方式及日期显示方法

SpringMVC的四种跳转方式

本质还是两种跳转:请求转发和重定向,衍生出四种是请求转发页面,转发action,重定向页面,重定向action;默认的跳转是请求转发,直接跳转到jsp页面展示,还可以使用框架提供的关键字redirect:,进行一个重定向操作,包括重定向页面和重定向action,使用框架提供的关键字forward:,进行服务器内部转发操作,包括转发页面和转发action。当使用redirect:和forward:关键字时,视图解析器中前缀后缀的拼接就无效了。
页面部分:

<%--
  Created by IntelliJ IDEA.
  User: peihj
  Date: 2022/5/12
  Time: 20:26
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<br><br><br>
<a href="${pageContext.request.contextPath}/one.action">1、请求转发界面(默认)</a>
<br><br><br>
<a href="${pageContext.request.contextPath}/two.action">2、请求转发action</a>
<br><br><br>
<a href="${pageContext.request.contextPath}/three.action">3、重定向页面</a>
<br><br><br>
<a href="${pageContext.request.contextPath}/four.action">4、重定向action</a>
<br><br><br>
<a href="${pageContext.request.contextPath}/five.action">5、随便跳页面</a>
<br><br><br>
<a href="${pageContext.request.contextPath}/data.action?name=zar">访问服务器,进行数据携带跳转</a>
<br><br><br>

<form action="${pageContext.request.contextPath}/mydate.action">
    日期:<input type="date" name="mydate"><br>
    <input type="submit" value="提交">
</form>

<br><br><br>
<a href="${pageContext.request.contextPath}/list.action">显示集合中对象的日期成员</a>


</body>
</html>

package com.peihj.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class jumpAction {
    @RequestMapping("/one.action")
    public String one(){
       //  默认是请求转发,使用视图解析器拼接前缀后缀进行页面跳转
       //  观察地址栏的变化:  http://localhost:8080/one.action
        System.out.println("默认方法,使用视图解析器拼接");
        return "main";
    }

    @RequestMapping("/two.action")
    public String two(){
        System.out.println("请求转发action");
        // forward: 这组字符串可以屏蔽前缀和后缀的拼接.实现请求转发跳转
        //  观察地址栏的变化:  http://localhost:8080/two.action
        return "forward:/other.action";
    }

    @RequestMapping("/three.action")
    public String three(){
        System.out.println("重定向界面........");
        // redirect:可以屏蔽前缀和后缀的拼接实现重定向跳转
        // 观察地址栏的变化  http://localhost:8080/admin/main.jsp
        return "redirect:/admin/main.jsp";
    }

    @RequestMapping("/four.action")
    public String four(){
        System.out.println("重定向action........");
        // redirect:可以屏蔽前缀和后缀的拼接实现重定向跳转
        // 观察地址栏的变化  http://localhost:8080/other.action
        return "redirect:/other.action";
    }

    @RequestMapping("/five.action")
    public String five(){
        System.out.println("随便跳........");
        // redirect:可以屏蔽前缀和后缀的拼接实现重定向跳转
        // 观察地址栏的变化: http://localhost:7070/five.action
        return "forward:/fore/login.jsp";
    }

}

otherAction

package com.peihj.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class otherAction {
    @RequestMapping("other.action")
    public String other(){
        System.out.println("other的action");
        return "main";
    }
}

SpringMVC默认的参数类型

这些类型只要写在方法参数中就可以使用了。

  • HttpServletRequest 对象
  • HttpServletResponse 对象
  • HttpSession 对象
  • Model/ModelMap 对象
  • Map对象

注意Model,Map,ModelMap都使用的是request请求作用域,意味着只能是请求转发后,页面才可以得到值。

package com.peihj.controller;

import com.peihj.pojo.Users;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class dataaction {
    @RequestMapping("data.action")
    public String main(HttpServletRequest request, HttpServletResponse response, HttpSession session, Map map, Model model, ModelMap modelMap){
        // 做一个数据传入main.jsp
        Users users = new Users(24, "张三");

        // 传入数据
        request.setAttribute("requestUsers",users);
        session.setAttribute("sessionUsers",users);
        model.addAttribute("modelusers",users);
        map.put("mapusers",users);
        modelMap.addAttribute("modelmapusers",users);

        return "main";
    }
}

pojo文件

package com.peihj.pojo;

public class Users {
    private int age;
    private String name;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Users(int age, String name) {
        this.age = age;
        this.name = name;
    }

    public Users() {
    }

    @Override
    public String toString() {
        return "Users{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}

<%--
  Created by IntelliJ IDEA.
  User: peihj
  Date: 2022/5/12
  Time: 20:31
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>mian........</h1>

<%--request.setAttribute("requestUsers",users);
        session.setAttribute("sessionUsers",users);
        model.addAttribute("modelusers",users);
        map.put("mapusers",users);
        modelMap.addAttribute("modelmapusers",users);--%>

requestUsers:${requestUsers}<br>
sessionUsers:${sessionUsers}<br>
modelusers:${modelusers}<br>
mapusers:${mapusers}<br>
modelmapusers:${modelmapusers}<br>

从index页面来的数据:${param.name}

</body>
</html>

日期处理

日期注入

日期类型不能自动注入到方法的参数中。需要单独做转换处理。使用@DateTimeFormat注解,需要在springmvc.xml文件中添加mvc:annotation-driven/标签。
springmvc配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.peihj.controller"/>

    <mvc:annotation-driven/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/admin/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

在方法的参数上使用@DateTimeFormat注解

package com.peihj.controller;

import com.peihj.pojo.Student;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class mydateAction {
    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");

    @RequestMapping("mydate.action")
    public String mydate(@DateTimeFormat(pattern = "yyyy-MM-dd") Date mydate){
        System.out.println(mydate);
        System.out.println(sf.format(mydate));
        return "show";
    }
}

类中全局日期处理
注册一个注解,用来解析本类中所有的日期类型,自动转换.

// 注册一个日期全局注解
	SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
    @InitBinder
    public void initBinder(WebDataBinder webDataBinder){
        webDataBinder.registerCustomEditor(Date.class,new CustomDateEditor(sf,true));
    }
 @RequestMapping("mydate.action")
    public String mydate(@DateTimeFormat(pattern = "yyyy-MM-dd") Date mydate, HttpServletRequest request){
        System.out.println(mydate);
        System.out.println(sf.format(mydate));

        request.setAttribute("mydate",sf.format(mydate));
        return "show";
    }

日期的显示处理
在页面上显示好看的日期,必须使用JSTL.
A)添加依赖jstl

	<dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>

B)在页面上导入标签库
如果是单个日期对象,直接转为好看的格式化的字符串进行显示. 如果是list中的实体类对象的成员变量是日期类型,则必须使用jstl进行显示.

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--导入jstl核心标签库--%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--导入jstl格式化标签库--%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

C)使用标签显示数据

<%--
  Created by IntelliJ IDEA.
  User: peihj
  Date: 2022/5/12
  Time: 22:39
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--导入jstl核心标签库--%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--导入jstl格式化标签库--%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>show...............</h1>

日期为:${mydate}
<br><br><br>
<h2>学生集合</h2>
<table width="800px" border="1">
    <tr>
        <th>姓名</th>
        <th>生日</th>
    </tr>
    <c:forEach items="${list}" 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>
</html>

参考

https://www.bilibili.com/video/BV1oP4y1K7QT?p=39&t=134.4

你可能感兴趣的:(java学习,SSM,java,spring)