springMVC学习(二)

一、数据写回到页面
    1.方法返回值使用ModelAndView,new ModelAndView("index", map) ,相当于把结果数据放到request里面(不建议使用)
/**
  * 方法返回值使用ModelAndView,new ModelAndView("index", map)
  * 相当于把结果数据放到request里面
  *  不建议使用
  * @return
  * @throws Exception
  */
 @RequestMapping("toPerson5.do")
 public ModelAndView toPerson5() throws Exception{
         Person person = new Person();
         person.setName("zhangsan");
         person.setAge(23);
         person.setAddress("beijing");
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
         Date date = sdf.parse("1982-09-09");
         person.setBirthday(date);
 
         Map<String, Object> map = new HashMap<String, Object>();
         map.put("p", person);
         return new ModelAndView("info", map);
 }
     2.直接在方法的参数列表中来定义map,这个map即是ModelAndView里面的map, 由视图解析器统一处理,统一走ModelAndView的接口(不建议使用)
/**
  * 直接在方法的参数列表中来定义map,这个map即是ModelAndView里面的map,
  * 由视图解析器统一处理,统一走ModelAndView的接口
  * 不建议使用
  * @param map
  * @return
  * @throws Exception
  */
 @RequestMapping("toPerson6.do")
 public ModelAndView toPerson6(Map<String, Object> map) throws Exception{
             Person person = new Person();
             person.setName("zhangsan");
             person.setAge(23);
               person.setAddress("beijing"); 
             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
             Date date = sdf.parse("1982-09-09");
             person.setBirthday(date);
 
              map.put("p", person);
              return new ModelAndView("info", map);
 }
3.在参数列表中直接定义Model,model.addAttribute("p",person) ,把参数值放到request里面去(建议使用)
/**
  * 在参数列表中直接定义Model,model.addAttribute("p",person)
  * 把参数值放到request里面去
  * 建议使用
  * @param model
  * @return
  * @throws Exception
  */
 @RequestMapping("toPerson7.do")
 public String toPerson7(Model model) throws Exception{
             Person person = new Person();
             person.setName("lisi");
             person.setAddress("shanghai");
             person.setAge(32);
             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
             Date birthday = sdf.parse("1782-12-12");
             person.setBirthday(birthday);
 
             model.addAttribute("p", person);
             return "info";
 }

二、Ajax调用springmvc的方法
1.ajax请求返回值类型应该是void,参数列表中直接定义HttpServletResponse ,获得PrintWriter的类,最后课表结果写到页面(不建议使用)
/**
  * Ajax调用springmvc方法
  * ajax请求返回值类型应该是void,参数列表中直接定义HttpServletResponse
  * 获得PrintWriter的类,最后课表结果写到页面
  * 不建议使用
  * @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页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
   
    <title></title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">
 <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.8.3.min.js"></script>
 <script type="text/javascript">
            $(function(){
                 $("#mybutton").click(function(){
                      $.ajax({
                               url:"${pageContext.request.contextPath}/test/ajax1.do",
                               type:"post",
                               dataType:"text",
                               data:{
                                    name:"zhangsan"
                               },
                               success:function(responseText){
                                        alert(responseText);
                               },
                               error:function(){
                                       alert("system error");
                               }
                      });
               });
         });
 </script>
 
 
  </head>
 
  <body>
    <input id="mybutton" type="button" value="click" />
  </body>
</html>

因为所有的jsp页面都是放在WEB-INF下,所以不能直接访问,需要通过springmvc
/**
  * 转到ajax页面
  * @return
  */
 @RequestMapping("toAjax.do")
 public String toAjax(){
             return "ajax";
 }

2.直接在参数的列表上定义PrintWriter,调用out.write(result),把结果写到页面 (建议使用)
@RequestMapping("ajax1.do")
 public void ajax1(String name, PrintWriter out){
              String result = "hello " + name;
              out.write(result);
 }

三、使用重定向
1.Controller内部重定向,redirect:加上同一个Controller中的requestMapping的值
创建一个form.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
   
    <title></title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">
 
  </head>
 
  <body>
    <form action="${pageContext.request.contextPath }/test/toPerson8.do" method="post">
     name:<input type="text" name="name"/><br/>
     age:<input type="text" name="age"/><br/>
     address:<input type="text" name="address"/><br/>
     birthday:<input type="text" name="birthday"/><br/>
     <input type="submit" value="submit" />
    </form>
  </body>
</html>

通过springmvc转到form.jsp页面
@RequestMapping("toForm.do")
 public String toForm(){
           return "form";
 }

Controller内部重定向
/**
  * Controller内部重定向,redirect:加上同一个Controller中的requestMapping的值
  * @return
  */
 @RequestMapping("redirectToForm.do")
 public String redirectToForm() {
              return "redirect:toForm.do";
 }
2.Controller之间的重定向,必须要指定好Controller的命名空间再指定requestMapping的值, redirect:后面必须要加'/',代表要从根目录开始
/**
  * Controller之间的重定向,必须要指定好Controller的命名空间再指定requestMapping的值,
  * redirect:后面必须要加'/',代表要从根目录开始
  * @return
  */
 @RequestMapping("redirectToForm1.do")
 public String redirectToForm1() {
             return "redirect:/test1/toForm.do";
 }
补:指定请求方法
@RequestMapping(method=RequestMethod.POST)可以指定请求方式,前台页面就必须要以它指定好的方式来访问
/**
  * @RequestMapping(method=RequestMethod.POST)可以指定请求方式
  * 前台页面就必须要以它指定好的方式来访问
  * @param person
  * @return
  */
 @RequestMapping(value="toPerson8.do", method=RequestMethod.POST)
 public String toPerson8(Person person){
        System.out.println(person);
        return "jsp1/hello";
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值