SpringMVC的返回值

首先是方法是返回值void 的  :

package cn.happy.springmvc05ajax;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by linlin on 2017/8/21.
 */
@Controller
public class FirstController {
    @RequestMapping("/doFirst")
    public void doFirst(HttpServletResponse response) throws IOException {
response.getWriter().write("ajax");
    }
}


首先我们定义一个@Controller来代表他是注解的

然后我们在方法上定义一个@RequestMapping来表示他的方法是 注解方法。

然后我们在这里面传一个值  ,然后我们回调到页面上 弄一个点击事件  来获取到他的值


<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%--<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>--%>
<html>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.js"></script>
<script type="text/javascript">
$(function () {
    $("#btn").click(function () {
         $.post('${pageContext.request.contextPath}/doFirst',{},function (data) {
         alert(data);
         });
    })
})

</script>
<body>
<h2>Hello Index!</h2>
<input type="button" id="btn" value="Ajax"/>
</body>
</html>

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
<!--    <mvc:annotation-driven/>-->
<context:component-scan base-package="cn.happy.springmvc05ajax"></context:component-scan>


</beans>


上面的是我的springmvc.xml这里面我们只需要弄一个包扫描器就可以了

对于Web.xml我们的几种方法并且有什么大 的改变  就是一些 xml更换了



<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <!--字符编码过滤器-->
<filter>
    <filter-name>charaterEncoding</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>utf-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>charaterEncoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <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:springmvc06Object.xml</param-value>
    </init-param>
  <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.jpg</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.js</url-pattern>
  </servlet-mapping>
</web-app>


这是一个简单的  返回值 ,下面我们看一下 返回Object  的 包括list 集合  map集合 对象 等等


package cn.happy.springmvc06object;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by linlin on 2017/8/21.
 */
@Controller
public class FirstControllerObject {
    @ResponseBody
    @RequestMapping("/first")
    public Object doFirst(){
        return 1;
    }

    @ResponseBody
    @RequestMapping(value = "/second",produces = "text/html;charset=UTF-8")
    public Object doSecond(){
        return "苏苏";
    }
    @ResponseBody
    @RequestMapping(value = "/third" )
    public Object doThird(){
        UserInfo u=new UserInfo();
        u.setAge(12);
        u.setName("苏苏");
        return u;
    }
    @ResponseBody
    @RequestMapping(value = "/domap" )
    public Object domap(){
        Map<String,UserInfo> map=new HashMap<String, UserInfo>();
        UserInfo u=new UserInfo();
        u.setName("苏苏");
        u.setAge(23);
        UserInfo u1=new UserInfo();
        u1.setName("啦啦");
        u1.setAge(213);
        map.put(u.getName(),u);
        map.put(u1.getName(),u1);

        return map;
    }
    @ResponseBody
    @RequestMapping(value = "/dolist" )
    public Object dolist(){
        List<UserInfo> list=new ArrayList<UserInfo>();
        UserInfo u=new UserInfo();
        u.setAge(12);
        u.setName("苏苏");
        UserInfo u1=new UserInfo();
        u1.setAge(22);
        u1.setName("苏苏1");
      list.add(u);
      list.add(u1);
        return list;
    }
}

package cn.happy.springmvc06object;

/**
 * Created by linlin on 2017/8/21.
 */
public class UserInfo {
    private String name;
    private int age;

    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;
    }
}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">

<context:component-scan base-package="cn.happy.springmvc06object"></context:component-scan>
  <mvc:annotation-driven/>

</beans>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%--<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>--%>
<html>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.js"></script>
<script type="text/javascript">
$(function () {
    $("#btn").click(function () {
        $.ajax({
            url:"${pageContext.request.contextPath}/domap",
            success:function(data){ //data指的是从server打印到浏览器的数据
              /*  alert(data.name+data.age);*/
              $.each(data,function (i,dom) {
                  alert(dom.name+"\t"+dom.age);
              });
            }
        });

    })
})

</script>
<body>
<h2>Object!</h2>
<input type="button" id="btn" value="Object"/>
</body>
</html>

下面我们看一下关于重定向和转发。我们默认的返回页面 的是转发,如果加上redirect就是重定向 ,我们都知道重定向和转发的区别,有一点就是转发携带 数据,而重定向不携带数据,所有这里面我们的实例就是做个对比 ,加上那个 我们页面里面没有东西显示。

package cn.happy.springmvc06object;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by linlin on 2017/8/21.
 */
@Controller
public class requestControllerObject {
  @RequestMapping("/addBook")
    public String addBook(HttpServletRequest request){
        request.setAttribute("bookname","苏苏苏苏苏苏");
        return "redirect:/list";
    }
    @RequestMapping("/list")
    public String listBook(){
        return "/listBook.jsp";
    }
}

<%--
  Created by IntelliJ IDEA.
  User: linlin
  Date: 2017/8/21
  Time: 16:42
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
    <title>添加图书</title>
</head>
<body>
<h1>添加图书</h1>
<form action="<%=path%>/addBook" method="post">
   <input type="submit"/>
</form>
</body>
</html>

<%--
  Created by IntelliJ IDEA.
  User: linlin
  Date: 2017/8/21
  Time: 16:45
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
    <title>图书列表</title>
</head>
<body>
<h1>图书列表</h1>
${bookname}
</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值