springMVC学习笔记(五) ---- 数据处理的几种方式
来源:互联网 发布:贵阳大数据是传销吗 编辑:程序博客网 时间:2024/05/16 18:32
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--结果视图的前缀+后缀--> <property name="prefix" value="/jsp/"/> <property name="suffix" value=".jsp"/> </bean><context:component-scan base-package="com.controller"/>
package com.controller;import com.vo.User;import org.springframework.stereotype.Controller;import org.springframework.ui.ModelMap;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Created by cfxd000 on 2016/11/15. */@Controller@RequestMapping("/")public class Data4Type { //第一种还是servlet API @RequestMapping("/data1") public String data1(HttpServletRequest request, HttpServletResponse response){ String id = request.getParameter("id"); if(id != null){ System.out.println(id); request.setAttribute("id",id); } return "data"; } //第二种modelAndView @RequestMapping("/data2") public ModelAndView data2(HttpServletRequest request, HttpServletResponse response){ ModelAndView mv = new ModelAndView(); mv.setViewName("data"); mv.addObject("id",request.getParameter("id")); return mv; } //第三种传入值自动映射到实体类,并且用modelMap @RequestMapping("/data3") public String data3(User user, ModelMap modelMap){ System.out.println(user); modelMap.addAttribute("id",user.getId()); modelMap.addAttribute("name",user.getName()); modelMap.addAttribute("age",user.getAge()); return "data"; } //第四种restful风格 @RequestMapping("/data4/{id}/{name}/{age}") public String data4(ModelMap modelMap, @PathVariable("id") int id, @PathVariable("name") String name, @PathVariable("age") int age ){ modelMap.addAttribute("id",id); modelMap.addAttribute("name",name); modelMap.addAttribute("age",age); return "data"; }} 0 0