SpringMVC学习笔记四

本文介绍了Spring MVC框架中如何从前端获取参数并传递给控制器,以及如何将数据从控制器发送回前端显示。包括使用方法参数、HttpServletRequest、自定义转换器处理不同类型的数据,通过Model、ModelAndView等方式响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



一.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.该方法只能适合本控制层.
@InitBinder
public 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"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值