springmvc返回值、数据写到页面、表单提交、ajax、重定向
实验是在前一篇文章的项目上做的;
数据写到页面
后台往前台传数据
TestController添加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /** * 方法的返回值采用ModelAndView, new ModelAndView("index", map);, * 相当于把结果数据放到request里面 * @return * @throws Exception */ @RequestMapping( "/toPerson4.do" ) public ModelAndView toPerson4() throws Exception{ Person person = new Person(); person.setName( "jerome" ); person.setAge(22); person.setAddress( "nanan" ); SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd" ); Date date = format.parse( "2012-12-21" ); person.setBirthday(date); Map<String,Object> map = new HashMap<String, Object>(); map.put( "p" , person); return new ModelAndView( "jsp/index" ,map); } |
页面接收:index.jsp
1 2 3 4 5 6 | <body> <h5>${p.name }</h5> <h5>${p.age }</h5> <h5>${p.address }</h5> <h5><fmt:formatDate value= "${p.birthday }" pattern= "yyyy-MM-dd" /></h5> </body> |
* 文章包含被禁用的url,无法保存和发布。
太坑了,这个链接也屏蔽~
重启tomcat访问:
输出信息正确;
另外一种方式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | /** * 直接在方法的参数列表中来定义Map,这个Map即使ModelAndView里面的Map * 由视图解析器统一处理,统一走ModelAndView的接口 * 也不建议使用 * @param map * @return * @throws Exception */ @RequestMapping( "/toPerson5.do" ) public String toPerson5(Map<String,Object> map) throws Exception{ Person person = new Person(); person.setName( "jerome" ); person.setAge(22); person.setAddress( "nanan" ); SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd" ); Date date = format.parse( "2012-12-21" ); person.setBirthday(date); map.put( "p" , person); return "jsp/index" ; } |
http://localhost:8080/springmvc-2/test/toPerson5.do
输出正确;
建议使用方式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /** *在参数列表中直接定义Model,model.addAttribute("p", person); *把参数值放到request类里面去,建议使用 * @param map * @return * @throws Exception */ @RequestMapping( "/toPerson6.do" ) public String toPerson6(Model model) throws Exception { Person person = new Person(); person.setName( "jerome" ); person.setAge(22); person.setAddress( "nanan" ); SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd" ); Date date = format.parse( "2012-12-21" ); person.setBirthday(date); //把参数值放到request类里面去 model.addAttribute( "p" , person); return "jsp/index" ; } |
http://localhost:8080/springmvc-2/test/toPerson6.do
输出正确数据;
不需要页面跳转:ajax
在TestController加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | /** * ajax的请求返回值类型应该是void,参数列表里直接定义HttpServletResponse, * 获得ProntWriter的类,最后可把结果写到页面 * 不建议使用 * @param name * @param response */ @RequestMapping( "/ajax.do" ) public void ajax(String name, HttpServletResponse response) { String result = "hello " + name; try { response.getWriter().write(result); } catch (IOException e) { e.printStackTrace(); } } |
前台调用 新建ajax.jsp
<input id="myButton" type="button" value="click">
Webroot新建js文件夹将jquery拷贝进来;
引进来jquery 和写js脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <script type= "text/javascript" > $(function(){ $( "#myButton" ).click(function(){ $.ajax({ url: "test/ajax.do" , type: "post" , dataType: "text" , data:{ name: "zhangsan" }, success:function(responseText){ alert(responseText); }, error:function(){ alert( "system error" ); } }); }); }); </script> |
后台写一个转发:
1 2 3 4 | @RequestMapping( "/toAjax.do" ) public String toAjax() { return "jsp/ajax" ; } |
http://localhost:8080/springmvc-2/test/toAjax.do
Click,弹出hello zhangsan,成功;
以上方法不建议使用,建议使用:
1 2 3 4 5 6 7 8 9 10 11 | /** * 直接在参数的列表上定义PrintWriter,out.wrote(result); * 把结果写到页面,建议使用 * @param name * @param out */ @RequestMapping( "/ajax1.do" ) public void ajax1(String name, PrintWriter out ) { String result= "hello1 " +name; out .write(result); } |
url:"test/ajax1.do",
重启tomcat 访问
http://localhost:8080/springmvc-2/test/toAjax.do
Click
弹出 hello1 zhangsan;
表单:
拷贝一份index,起名form.jsp
1 2 3 4 5 6 7 8 | <body> <form action= "test/toPerson7.do" method= "post" > name:<input name= "name" type= "text" ><br/> age:<input name= "age" type= "text" ><br/> address:<input name= "address" type= "text" ><br/> birthday:<input name= "birthday" type= "text" ><br/> <input type= "submit" ><br/> </form> |
TestController
1 2 3 4 5 | @RequestMapping( "/toPerson7.do" ) public String toPerson7(Person person) { System. out .println(person); return "jsp/index" ; } |
重启tomcat 访问:
http://localhost:8080/springmvc-2/test/toForm.do
提交跳转到
http://localhost:8080/springmvc-2/test/toPerson7.do
控制台输出
Person [name=aa, address=asdf, birthday=Tue Jun 03 00:00:00 CST 2014, age=22]
请求方式的指定:
后台可以指定提交方法,如果前台不是用的同一种提交方式 将报错;
1 2 3 4 5 6 7 8 9 10 11 | /** * @RequestMapping(value="/toPerson7.do",method=RequestMethod.POST) * 可以指定请求方式,前台就必须要以它制定好的方式来访问,不然会出现405错误 * @param person * @return */ @RequestMapping(value= "/toPerson7.do" ,method=RequestMethod.POST) public String toPerson7(Person person) { System. out .println(person); return "jsp/index" ; } |
重定向:
同一个 controller
1 2 3 4 5 6 7 8 9 | /** * 重定向:controller内部重定向,redirect:加上同一个controller中 * 的requesMapping的值 * @return */ @RequestMapping( "/redirectToForm.do" ) public String redirectToForm() { return "redirect:toForm.do" ; } |
http://localhost:8080/springmvc-2/test/redirectToForm.do
重定向到
http://localhost:8080/springmvc-2/test/toForm.do
Controller之间的重定向:
拷贝一份TestController改成TestController1
留这个
1 2 3 4 5 6 7 8 9 10 11 12 | @Controller //用来标注当前类是springmvc的控制层的类 @RequestMapping( "/test1" ) //controller的唯一标识或者命名空间 public class TestController1 { @RequestMapping( "/toForm.do" ) public String toForm() { return "jsp/form" ; } } |
TestController 添加
1 2 3 4 5 6 7 8 9 10 | /** * controller之间的重定向:必须要指定好controller的命名空间再 * 指定requestMapping的值,redirect:后必须要加/,是从根目录开始, * 否则就从当天test找了 * @return */ @RequestMapping( "/redirectToForm1.do" ) public String redirectToForm1() { return "redirect:/test1/toForm.do" ; } |
重启tomcat 访问
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步