码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMvc(2)

时间:2019-09-04 00:25:37      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:set   let   mvc   add   ota   jsp   out   返回   obj   

  

(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(2)

标签:set   let   mvc   add   ota   jsp   out   返回   obj   

原文地址:https://www.cnblogs.com/Kuriyama-Mirai/p/11455767.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!