• 获取POST数据
    这一节会继续Java学习笔记09。

    一般在开发的时候,我们需要获取表单中提交的数据,那么我们必须先创建一个say.jsp,这个jsp的内容是一个非常简单的表单,POST方式提交,提交到/hello/show/路径上。


    1. <form action='/hello/show/' method="post">  

    2. <input name="username" type="text" />  

    3. <input name="password" type="text" />  

    4. <input name="" type="submit" />  

    5. </form>  


然后我们需要一个控制器文件,两个Action,一个是现实say.jsp静态页面,一个是接收处理POST提交过来的数据。
其中sya()方法显示静态页面;show()方法处理POST数据。
show方法两个参数,User user这个对象 Spring会自动将POST数据填充到User这个类上面去,Modelmodel主要用来实现Controller和模板之间数据传递。


  1. package com.mvc.rest;  

  2.  

  3. import org.springframework.stereotype.Controller;  

  4. import org.springframework.ui.Model;  

  5. import org.springframework.web.bind.annotation.RequestMapping;  

  6.  

  7. //@Controller 是一个标识这个是控制器类的注解标签,如果是控制器类 都需要有这个注解。  

  8. @Controller  

  9. //@RequestMapping(value="/hello") 会映射到url /hello/则访问HelloController中的Action  

  10. @RequestMapping(value="/hello")  

  11. public class HelloController {  

  12.      

  13.    //@RequestMapping(value="/say") 会映射到url /hello/say则访问HelloController中的Action  

  14.    @RequestMapping(value="/say")  

  15. java教程,自学编程,青软培训