SpringMVC学习笔记四

来源:互联网 发布:河南省大数据产业园 编辑:程序博客网 时间:2024/06/07 05:13


一.Controller接受网页参数.

   1.使用方法的形参来接受

复制代码
复制代码
//使用基本类型和字符串来接受@RequestMapping(value="/param2.do")public String param(People p){     System.out.printlt(p.getName()+"===="+p.getAge());     return "param";}注意:该方法的形参一定要和网页参数名相同.而且这种方式可以自动转型.
复制代码
public class Person {    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    private String name;    private int age;  }
复制代码
复制代码
复制代码
//使用对象类型来接受@RequestMapping(value="/param2.do")public String param(People p){     System.out.printlt(p.getName()+"===="+p.getAge());     return "param";}
复制代码

 

复制代码

 2.使用request来接受基本类型.

   

复制代码
@RequestMapping(value="/param.do")public String param(HttpServletRequest request,HttpServletResponse response){     String name=request.getParameter("name");     String age=request.getParameter("age");     System.out.printlt(name+"===="+age);     return "param";}
复制代码

 如果接受的类型为时间类型我们可以做如下方式来处理.

//boxing automatically@RequestMapping("/person1")public String toPerson(Person p){    System.out.println(p.getName()+" "+p.getAge());    return "hello";}
//1.该方法只能适合本控制层.@InitBinderpublic void initBinder(ServletRequestDataBinder binder){    binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),            true));}

 //2.可以定义一个全局时间转化类.

复制代码
public class DateConvert implements Converter<String, Date> {    @Override    public Date convert(String stringDate){        System.out.println("=======================_______");        //时间转化类(时间格式)        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");        try {            return simpleDateFormat.parse(stringDate);        } catch (ParseException e) {            e.printStackTrace();        }        return null;    }}
复制代码

 在SpringMVC配置文件中声明该配置类

复制代码
<!-- 第三步:注册处理器映射器/处理器适配器 ,添加conversion-service属性-->       <mvc:annotation-driven conversion-service="conversionService"/>       <!-- 第二步: 创建convertion-Service ,并注入dateConvert-->    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">        <property name="converters">            <set>                <ref bean="dateConvert"/>            </set>        </property>    </bean>    <!-- 第一步:  创建自定义日期转换规则  class:为时间转化类的全类名-->       <bean id="dateConvert" class="com.eduask.ykq.controller.DateConvert"/>
复制代码

 

二.如何向网页响应数据

   1.可以把数据保存在request对象中

    

复制代码
//pass the parameters to front-end http://www.kanxia.org/info/6956.html@RequestMapping("/show")public String showPerson(HttpServletRequest request,HttpServletResponse response){    Person p =new Person();        p.setAge(20);    p.setName("jayjay");    request.setAttribute("p",p);    return "show";}
复制代码

 

   2.可以把数据保存在ModelAndView中 但是这种方式的方法的返回类型必须是ModelAndView

复制代码
//pass the parameters to front-end@RequestMapping("/show")public ModelAndView showPerson(){    Person p =new Person();        p.setAge(20);    p.setName("jayjay");    ModelAndView andView=new ModelAndView("show");    andView.addObject("p",p);    return andView;}
复制代码

 

   3.可以把数据保存在Model中

  

复制代码
//pass the parameters to front-end@RequestMapping("/show")public String showPerson(Model model){    Person p =new Person();        p.setAge(20);    p.setName("jayjay");    model.addAttribute("p",p);    return "show";}
复制代码

 

   4.可以把数据保存在Map中

复制代码
//pass the parameters to front-end@RequestMapping("/show")public String showPerson(Map<String,Object> map){    Person p =new Person();    map.put("p", p);    p.setAge(20);    p.setName("jayjay");    return "show";}
复制代码

   前台可在Request域中取到"p"

0 0
原创粉丝点击