SpringMvc

Posted kuriyama-mirai

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringMvc相关的知识,希望对你有一定的参考价值。

  

(1)接收的参数为日期类型

  使用Controller注解的类在接收参数时如果参数为时间类型的参数时会报错:表示请求的参数有问题

技术图片

  解决方法(1):在对应的Controller类中加入如下代码 表示:当接收的参数为日期类型时先经过该方法进行处理。适合接收单个日期参数

@InitBinder
    public void initBinder(ServletRequestDataBinder binder)
        //只要网页中传来的数据格式为yyyy-MM-dd 就会转化为Date类型
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),//模式可以更改
                true));
    

 解决方案(2):当接收的参数比较多时(接收的是一个对象)

@RequestMapping("/list.do")
    public String list(User user) 
        System.out.println(user);
        return "index";
    

技术图片

参数有上图几个,这时为了避免报错400,我们可以在该实体类的Date属性中加入@DateTimeFormat(pattern="yyyy-MM-dd")注解 适合用于实体类中

技术图片

 (2)controller 进行数据保存

   2.1 数据保存到request作用域的方式.   

    1. 使用ModelAndView,那么该方法的返回类型必须是ModelAndView

      

package com.zhiyou100.ydb.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.zhiyou100.ydb.bean.User;

@Controller
@RequestMapping("/user")
@SessionAttributes(names="name")
public class UserController 
//    使用ModelAndView 将数据保存到request
    @RequestMapping("/mod.do")
    public ModelAndView Mod() 
        ModelAndView mv =new ModelAndView("index");
        mv.addObject("name", "张三");
        return mv;
    

测试:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
$requestScope.name 
<img src="/springmvc-03/img/1.jpeg" height="700px">
</body>
</html>

    2. 使用Model, 方法的返回值还是字符串类型。

package com.zhiyou100.ydb.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.zhiyou100.ydb.bean.User;

@Controller
@RequestMapping("/user")
@SessionAttributes(names="name")
public class UserController 
       // 使用Model 将数据保存到request
      @RequestMapping("/mode.do")
       public String Mode(Model mdoel) 
             mdoel.addAttribute("name", "李四");
return "index";

 测试

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
$requestScope.name 
<img src="/springmvc-03/img/1.jpeg" height="700px">
</body>
</html>

    3. 使用Map.方法的返回值还是字符串类型。

package com.zhiyou100.ydb.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.zhiyou100.ydb.bean.User;

@Controller
@RequestMapping("/user")
@SessionAttributes(names="name")
public class UserController 
     //    使用Map 将数据保存到request
    @RequestMapping("map.do")
    public String Setmap(Map<String, Object> map) 
        map.put("name", "王五");
        return "index";
    

测试

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
$requestScope.name 
<img src="/springmvc-03/img/1.jpeg" height="700px">
</body>
</html>

  4. 原始的HttpServletRequest对象保存

package com.zhiyou100.ydb.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.zhiyou100.ydb.bean.User;

@Controller
@RequestMapping("/user")
@SessionAttributes(names="name")
public class UserController 
   //    使用原始 HttpServletRequest 将数据保存到request
    @RequestMapping("req.do")
    public String Setmap(HttpServletRequest request) 
        request.setAttribute("name", "刘六");
        return "index";
    

测试:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
$requestScope.name 
<img src="/springmvc-03/img/1.jpeg" height="700px">
</body>
</html>

  2.2数据保存到session作用域的方式.

  1. 使用原始的HttpSession保存。
package com.zhiyou100.ydb.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.zhiyou100.ydb.bean.User;

@Controller
@RequestMapping("/user")
@SessionAttributes(names="name")
public class UserController 
 //    使用原始 HttpSession 将数据保存到Session
    @RequestMapping("ses.do")
    public String Setmap(HttpSession session) 
        session.setAttribute("name", "赵七");
        return "index";
    

测试:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
$sessionScope.name 
<img src="/springmvc-03/img/1.jpeg" height="700px">
</body>
</html>

   2.使用注解@SessionAttributes(name=key1,key2)

package com.zhiyou100.ydb.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.zhiyou100.ydb.bean.User;

@Controller
@RequestMapping("/user")
@SessionAttributes(names="name")//键名叫:name的保存的作用域为session
public class UserController 
//    使用注解 将数据保存到request
    @RequestMapping("/sess.do")
    public String Mode(Model mdoel) 
        mdoel.addAttribute("name", "李四");
        return "index";
    

测试:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
$sessionScope.name 
<img src="/springmvc-03/img/1.jpeg" height="700px">
</body>
</html>

   2.3保存到application中

package com.zhiyou100.ydb.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.zhiyou100.ydb.bean.User;

@Controller
@RequestMapping("/user")
@SessionAttributes(names="name")
public class UserController 
       // 使用Model 将数据保存到request
      @RequestMapping("/mode.do")
       public String Mode(Model mdoel,HttpSession session) 
             mdoel.addAttribute("name", "李四");
        session.getServletContext().setAttribute("name","我在application中")
                return "index";
      

测试:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
$applicationScope.name 
<img src="/springmvc-03/img/1.jpeg" height="700px">
</body>
</html>

因为RequestMapping注解下的方法 返回的默认为请求转发 如何让其变成重定向呢?

技术图片

 

 

 解决方案 :加入redirect:

技术图片

 

 (3)静态资源的映射关系

当我们在jsp文件中引入img 图片,在用浏览器运行会出现找不到图片现象

技术图片

 

1.首先配置任何请求都要经过DispatcherServlet

 技术图片

 

 2.这时就需要使用静态资源映射 在springmvc的配置文件中添加

技术图片

  (4)Springmvc完成ajax功能

      1.加入Jackson的jar包 和 jquery jar包

      技术图片技术图片

 

 

      2.引入js

技术图片

 

      3.在响应的方法上加 @ResponseBody : 表示把Java对象转化为json对象

@RequestMapping("Ajax1")
    @ResponseBody
    public String ajax1(String name) 
        System.out.println(name);
        return "你好";
    

      4.编写ajax方法

技术图片

      5.方法的返回类型可以是字符串 ,对象 , 集合

package com.zhiyou100.ydb.controller;

import java.util.ArrayList;
import java.util.List;

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

import com.zhiyou100.ydb.bean.User;

@Controller
@RequestMapping(value="ajax", produces = "text/plain;charset=UTF-8")
public class AjaxController 
    
    @RequestMapping("Ajax1")
    @ResponseBody
    public String ajax1(String name) 
        System.out.println(name);
        return "你好";
    
    @RequestMapping("Ajax2")
    @ResponseBody
    public User ajax2(String name) 
        User user = new User("张三",10);
        return user;
    
    @RequestMapping("Ajax3")
    @ResponseBody
    public List<User> ajax3(String name) 
        User user1 = new User("张三",10);
        User user2 = new User("李四",10);
        User user3 = new User("王五",10);
        User user4 = new User("刘六",10);
        User user5 = new User("赵七",10);
        List<User> list = new ArrayList<>();
        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);
        list.add(user5);
        return list;
    
    
    

(注意:)如果ajax返回的为字符串,那么就会出现乱码

    因为在spring-webmvc 源码中返回的是字符串默认的格式是ISO--1

技术图片

 

 解决方案(1):在springmvc 映射文件中加入

技术图片

 

 解决方案(2):在RequestMapping的属性中添加返回数据类型属性  produces = "text/plain;charset=UTF-8" 

 技术图片

以上是关于SpringMvc的主要内容,如果未能解决你的问题,请参考以下文章

SpringMVCSpringMVC系列2之@RequestMapping 映射约束请求

SpringMVCSpringMVC简介&第一个SpringMVC项目

SpringMVCSpringMVC简介&第一个SpringMVC项目

SpringMVC概述+SpringMVC运行流程+SpringMVC搭建

不得不看的SpringMVC篇!!!(概述+SpringMVC运行流程+SpringMVC搭建)

SpringMVC