zoukankan      html  css  js  c++  java
  • 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/>
  • 相关阅读:
    2009中国IT界名人
    jQuery简介
    Spring下载地址
    ContextLoaderListener
    MyBatisUtil类
    SSM事务
    后台管理中心跳转问题解决
    mybatis返回boolean值时数据库返回null
    yarn作业提交过程
    Hadoop集群运行wordcount jar包出错
  • 原文地址:https://www.cnblogs.com/zjc364259451/p/11455946.html
Copyright © 2011-2022 走看看