一、SpringMVC支持在控制器的业务方法中写入参数作为传递过来的变量
@Controller @RequestMapping(value="/kaiye") public class HelloHAE{ @RequestMapping(value="/hello.action") public String hello(Model model,String name,double number){ model.addAttribute("Message",name+"|"+String.valueOf(number)); return "success"; } }
二、SpringMVC支持限定某个业务控制方法,只允许GET或POST请求方式访问
@Controller @RequestMapping(value="/kaiye") public class HelloHAE{ @RequestMapping(method={RequestMethod.POST,RequestMethod.GET},value="/hello.action") public String hello(Model model){ model.addAttribute("Message","hello"); return "success"; } @RequestMapping(method=RequestMethod.GET,value="/goodbye.action") public String goodbye(Model model){ model.addAttribute("Message","goodbye"); return "success"; } }
三、SpringMVC支持在业务控制方法中写入Request,Response等传统web参数
package com.jyk.springmvc.httpRequestResponse; import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.support.RequestContext; /** * SpringMVC支持web传统方式httpRequest和httpResponse获取参数(建议用model) * 也支持void类型的业务方法 * @author fat */ @Controller @RequestMapping(value="/kaiye") public class HelloHAE{ @RequestMapping(method={RequestMethod.POST,RequestMethod.GET},value="/hello.action") public void hello(HttpServletRequest request,HttpServletResponse response) throws IOException{ String name = request.getParameter("name"); String number = request.getParameter("number"); System.out.println(name+":"+number); //绑定到session域中 request.getSession().setAttribute("name",name); request.getSession().setAttribute("number",number); //页面重定向 response.sendRedirect(request.getContextPath()+"/success.jsp"); } }
四、在默认情况下,springmvc不能将String类型转成java.util.Date类型,需要使用@InitBind来解决字符串转日期类型,同时支持在参数中传递多个实体类,如下代码中Bean对象包含了Person和Animal对象
/** * 日期格式转换,和多个实体类型接收参数 * @author fat */ @Controller @RequestMapping(value="/kaiye") public class HelloHAE{ /** * 自定义类型转换器 * @param request * @param binder * @throws Exception */ @InitBinder protected void initBinder(HttpServletRequest request,ServletRequestDataBinder binder) throws Exception{ binder.registerCustomEditor(Date.class, new CustomDateEditor (new SimpleDateFormat("yyyy-MM-dd"), true)); } @RequestMapping(value="/hello.action") public String hello(Model model,Person person){ System.out.println(person.toString()); model.addAttribute("person",person); return "success"; } /** * 参数中传多个实体类时 * @param model * @param person * @return */ @RequestMapping(value="/entities.action") public String entitiesTest(Model model,Bean bean){ System.out.println(bean.getPerson()); System.out.println(bean.getAnimal()); model.addAttribute("person",bean.getPerson()); model.addAttribute("animal",bean.getAnimal()); return "success"; } }
五、SpringMVC支持在业务控制方法中收集数组参数
<form action="${pageContext.request.contextPath}/kaiye/array.action" method="post"> <table border="1" align="center"> <tr> <th>名字</th> <td><input type="checkbox" name="ids" value="1"/></td> </tr> <tr> <th>学号</th> <td><input type="checkbox" name="ids" value="2"/></td> </tr> <tr> <th>日期</th> <td><input type="checkbox" name="ids" value="3"/></td> </tr> <tr> <td> <input type="submit" value="009测试"/> </td> </tr> </table> </form>
@Controller @RequestMapping(value="/kaiye") public class HelloHAE{ @RequestMapping(value="/array.action") public String hello(Model model,int ids[]){ for(int i:ids){ System.out.println(i); } return "success"; } }
六、SpringMVC支持在业务控制方法中收集List<JavaBean>参数
<form action="${pageContext.request.contextPath}/kaiye/beanList.action" method="post"> <table border="1" align="center"> <tr> <td><input type="checkbox" name="persons[0].name" value="名字1"/></td> <td><input type="checkbox" name="persons[0].number" value="编号1"/></td> </tr> <tr> <td><input type="checkbox" name="persons[1].name" value="名字2"/></td> <td><input type="checkbox" name="persons[1].number" value="编号2"/></td> </tr> <tr> <td> <input type="submit" value="010测试"/> </td> </tr> </table> </form>
public class Bean { private List<Person> persons = new ArrayList<Person>(); public List<Person> getPersons() { return persons; } public void setPersons(List<Person> persons) { this.persons = persons; } }
@Controller @RequestMapping(value="/kaiye") public class HelloHAE{ @RequestMapping(value="/beanList.action") public String beanList(Model model,Bean bean){ System.out.println(bean.getPersons()); return "success"; } }
七、SpringMVC支持结果的转发和重定向,在转发情况下,共享request域对象,会将参数从第一个业务控制方法传入第二个业务控制方法
/** * 重定向共享参数 * @author fat */ @Controller @RequestMapping(value="/kaiye") public class HelloHAE{ @RequestMapping(value="/hello.action") public String hello(Model model,String name){ System.out.println("hello "+name); return "forward:/kaiye/goodbye.action"; } @RequestMapping(value="/goodbye.action") public String goodbye(Model model,String name){ System.out.println("goodbye "+name); return "success"; } }
八、SpringMVC支持异步发送表单数据到JavaBean,并响应JSON文本返回
<h3>013测试</h3> <input type="button" value="person转json"> <input type="button" value="persons转json"> <input type="button" value="map转json"> <script type="text/javascript"> $(":button:first").click(function(){ var url = "${pageContext.request.contextPath}/kaiye/hello.action"; var sendData = null; $.post(url,sendData,function(backData,textStatu,ajax){ alert(ajax.responseText); }) }); $(":button:eq(1)").click(function(){ var url = "${pageContext.request.contextPath}/kaiye/hellos.action"; var sendData = null; $.post(url,sendData,function(backData,textStatu,ajax){ alert(ajax.responseText); }) }); $(":button:eq(2)").click(function(){ var url = "${pageContext.request.contextPath}/kaiye/hellomap.action"; var sendData = null; $.post(url,sendData,function(backData,textStatu,ajax){ alert(ajax.responseText); }) }); </script>
/** * 将实体对象以Json的格式返回 * @author fat */ @Controller @RequestMapping(value="/kaiye") public class HelloHAE{ @RequestMapping(value="/hello") public @ResponseBody Person hello(){ return new Person("kaiye","呵呵","12"); } @RequestMapping(value="/hellos") public @ResponseBody List<Person> hellos(){ List<Person> persons = new ArrayList<Person>(); persons.add(new Person("狗","1","33")); persons.add(new Person("猫","2","44")); return persons; } @RequestMapping(value="/hellomap.action") public @ResponseBody Map<String,Object> hellomap(){ Map<String,Object> map = new HashMap<String,Object>(); map.put("age",1); map.put("name","你好"); return map; } }