一.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"/>
//3.RESTFul风格的SringMVC
3.1 RestController
@Controller @RequestMapping("/rest") public class RestController { @RequestMapping(value="/user/{id}",method=RequestMethod.GET) public String get(@PathVariable("id") Integer id){ System.out.println("get"+id); return "/hello"; } @RequestMapping(value="/user/{id}",method=RequestMethod.POST) public String post(@PathVariable("id") Integer id){ System.out.println("post"+id); return "/hello"; } @RequestMapping(value="/user/{id}",method=RequestMethod.PUT) public String put(@PathVariable("id") Integer id){ System.out.println("put"+id); return "/hello"; } @RequestMapping(value="/user/{id}",method=RequestMethod.DELETE) public String delete(@PathVariable("id") Integer id){ System.out.println("delete"+id); return "/hello"; } }
3.2 form表单发送put和delete请求
在web.xml中配置
<!-- configure the HiddenHttpMethodFilter,convert the post method to put or delete --> <filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
3.3 在前台可以用以下代码产生请求
<form action="rest/user/1" method="post"> <input type="hidden" name="_method" value="PUT"> <input type="submit" value="put"> </form> <form action="rest/user/1" method="post"> <input type="submit" value="post"> </form> <form action="rest/user/1" method="get"> <input type="submit" value="get"> </form> <form action="rest/user/1" method="post"> <input type="hidden" name="_method" value="DELETE"> <input type="submit" value="delete"> </form>
在web.xml中配置<servlet>
<servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <!-- <load-on-startup>1</load-on-startup> --> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name>
<!--这里需要注意的地方是 web.xml配置路径是不能使用.do必须使用/ 否则RESTFul风格无法接受到参数值报404
-->
<url-pattern>/</url-pattern> </servlet-mapping>
二.如何向网页响应数据
1.可以把数据保存在request对象中
//pass the parameters to front-end @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"