springMVC(四)—— 完成接收参数

通过springmvc来完成接受参数

前端代码:

<%@ 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>
<form action="list.do" method="post">
    姓名:<input type="text" name="uname" />
    性别:<input type="text" name="sex"/>
    年龄:<input type="text" name="age">
    <input type="submit" value="提交"/>
    
</form>
</body>
</html>

 创建一个实体类User

package com.zhiyou100.zjc.bean;
//这里的属性名要和前端表单中的name值相同
public class User {
    private String uname;
    private String sex;
    private String age;
    public String getUname() {
        return uname;
    }
    public void setUname(String uname) {
        this.uname = uname;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "User [uname=" + uname + ", sex=" + sex + ", age=" + age + "]";
    }
    
}

 Controller类中的代码

package com.zhiyou100.zjc.annotation;

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

import com.zhiyou100.zjc.bean.User;

@Controller
public class UserAnnotation {
   
    @RequestMapping("list")
    public ModelAndView list(User user) {
        ModelAndView mav =  new ModelAndView();
        mav.setViewName("info");
        mav.addObject("user", user);
        System.out.println(user);
        return mav;
    }
}

 接受的参数为日期类型

1、使用Controller类中使用initBinder方法,这种方法多用于,接收的参数为只有一个Date的时候

package com.zhiyou100.zjc.annotation;

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

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zhiyou100.zjc.bean.User;

@Controller
public class UserAnnotation {
    
    @RequestMapping("info")
    @ResponseBody
    public String list(Date date) {
        System.out.println(date);
        return "";
    }
    @InitBinder
    public void initBinder(ServletRequestDataBinder binder){
        //只要网页中传来的数据格式为yyyy-MM-dd 就会转化为Date类型
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),
                true));
    } 
}

 

2、在实体类中Date属性的上面使用注解,这种方法,多用于接收的参数为一个对象的时候

@DateTimeFormat(pattern = "yyyy-MM-dd")//不是输出结果的格式,只是接收参数的格式
private Date date;

 

controller进行数据保存

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

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

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

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

4. 原始的HttpServletRequest对象保存

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

  1. 使用原始的HttpSession保存。
  2. 使用注解@SessionAttributes(name={key1,key2})

静态资源的映射关系

静态资源可以正常的显示。

上图的这种情况需要在springmvc配置文件上加入以下代码:

<!--释放静态资源-->
<mvc:default-servlet-handler/>

 

posted @ 2019-09-03 21:52  小成~  阅读(247)  评论(0编辑  收藏  举报