SpringMVC (八)SpringMVC返回值類型

SpringMVC的返回值類型有MedelAndView,String,void,Object數值型,集合類型等等javascript

前兩種咱們以前寫案例的時候一直在用,如今看一下返回值是voidhtml

第一種:返回viodjava

返回值是void的話,就要結合ajax來寫jquery

準備一個用戶類web

package demo09Return; /** * Created by mycom on 2018/3/26. */
public class UserInfo { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }

和控制器類ajax

package demo09Return; import com.alibaba.fastjson.JSON; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.ArrayList; import java.util.List; /** * Created by mycom on 2018/3/26. */ @Controller public class ReturnController { @RequestMapping("/one") public void doFirst(HttpServletResponse response) throws IOException { List<UserInfo> list=new ArrayList<UserInfo>(); UserInfo user=new UserInfo(); user.setUsername(""); user.setPassword("1225"); list.add(user); String data = JSON.toJSONString(list); response.getWriter().write(data); } }

在頁面上spring

<%-- Created by IntelliJ IDEA. User: mycom Date: 2018/3/26 Time: 17:13 To change this template use File | Settings | File Templates. --%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.8.3.min(1).js"></script>
<script type="text/javascript"> $(function () { $("input").click(function () { $.ajax({ url:"/one", type:"POST", success:function (data) { $.each(eval("("+data+")"),function (i,dom) { alert(dom.username); }) } }) }) }) </script>
<head>
    <title>Title</title>
</head>
<body>
<input type="button" value="按鈕">
</body>
</html>

在配置文件中json

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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="demo09Return"></context:component-scan>

    <!--視圖解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>


</beans>

注意不要忘記修改web.xml中的classpath的值spring-mvc

 第二種:返回Objectmvc

引入jar

<!--返回Object類型的jar-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.8.1</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.5.1</version>
        </dependency>

 

//返回int
    @RequestMapping("/first") @ResponseBody public Object doFirst(){ return 1; } //返回String
    @RequestMapping(value = "/second",produces = "text/html;charset=UTF-8") @ResponseBody public Object doSecond(){ return "明天會更好"; } //返回對象
    @RequestMapping(value = "/third") @ResponseBody public Object doThird(){ UserInfo user=new UserInfo(); user.setUsername(""); user.setPassword("123456"); return user; } //返回集合
    @RequestMapping(value = "/four") @ResponseBody public Object doFour(String name){ List<UserInfo> list=new ArrayList<UserInfo>(); UserInfo user=new UserInfo(); user.setUsername(""); user.setPassword("1225"); UserInfo user2=new UserInfo(); user2.setUsername(name); user2.setPassword("123456"); list.add(user); list.add(user2); String data = JSON.toJSONString(list); return list; } //返回map
    @RequestMapping(value = "/five") @ResponseBody public Object doFive(){ Map<String,UserInfo> map=new HashMap<String, UserInfo>(); UserInfo user=new UserInfo(); user.setUsername(""); user.setPassword("1225"); UserInfo user2=new UserInfo(); user2.setUsername(""); user2.setPassword("123456"); map.put(user.getUsername(),user); map.put(user2.getUsername(),user2); return map; }

配置文件中:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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="demo09Return"></context:component-scan>

    <!--視圖解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--註解驅動-->
    <mvc:annotation-driven/>


</beans>

測試頁面:

<%-- Created by IntelliJ IDEA. User: mycom Date: 2018/3/26 Time: 17:13 To change this template use File | Settings | File Templates. --%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.8.3.min(1).js"></script>
<script type="text/javascript">
    /*$(function () { $("input").click(function () { $.ajax({ url:"/four", type:"POST", success:function (data) { $.each(function (i,dom) { alert(dom.username); }) } }) }) })*/ $(function () { $("input").click(function () { $.ajax({ url:"/five", type:"POST", success:function (data) { $.each(function (i,dom) { alert(dom.username); }) } }) }) }) </script>
<head>
    <title>Title</title>
</head>
<body>
<input type="button" value="按鈕">
</body>
</html>
相關文章
相關標籤/搜索