zoukankan      html  css  js  c++  java
  • SpringMVC中的返回值问题之二返回数值类型和字符串类型

    返回数值类型和字符串类型

    需要导入依赖

    当引入Jackson-databind-2.5.1.jar时自动引入Jackson-annotations-2.5.0.jar

    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.8.1</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.5.1</version>
    </dependency>

    web.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>CharactorEncoding</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>CharactorEncoding</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:springmvc07RetuenObject.xml</param-value>
    </init-param>

    <!--意思:Tomact启动,就将servlet创建好放入内存中了-->
    <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>

    </web-app>

    静态资源


    <?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="cn.sjl.day04returnobject"></context:component-scan>

    <!--访问静态资源-->
    <mvc:resources mapping="/js/**" location="/js/"></mvc:resources>
    <!--注解驱动-->
    <mvc:annotation-driven/>
    </beans>



    //控制器
    @Controller
    public class FirstController {

    /**
    * 返回数值类
    * @return
    */
    @RequestMapping(value = "/first") //控制器方法(即访问方法)
    @ResponseBody //响应体
    public Object doFirst(){
    return 1;
    }


    /**
    * 返回字符串类型
    * @return
    */
    @RequestMapping(value = "/second",produces = "text/html;charset=utf-8") //控制器方法(即访问方法)
    @ResponseBody //响应体
    public Object doSecond(){
    return "你好";
    }


    }

    return.jsp页面
    <%@ page contentType="text/html;charset=UTF-8" language="java"  isELIgnored="false" %>
    <html>
    <head>
    <title>返回值数值</title>
    </head>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.8.3.min.js"></script>
    <script type="text/javascript">
    $(function(){
    $("#btn").click(function(){
    $.ajax({
    url:"${pageContext.request.contextPath}/first",

    /* url:"${pageContext.request.contextPath}/second",*/
    success:function (data) {
    alert(data);
    }
    });
    });
    });

    </script>
    <body>
    <input type="button" id="btn" value="Ajax"/>
    </body>
    </html>


     
     
     


     
     
  • 相关阅读:
    公开课 | 高段位的测试开发工程师是如何养成的?
    测试面试真题 | 从屡次碰壁到成长蜕变,半年拿下某大厂 60W 年薪测试开发 Offer!
    Git实战(五)| 让工作更高效,搞定Git的分支管理
    双 11 钜惠 | 测试开发爆款课程全年最低价,送 Mate40Pro、Kindle、华为手环等万元豪礼!
    测试面经 | 从测试螺丝钉到大厂测试开发,三点成长心得和面试经验
    Sql Server 常用函数
    IE Web Control介绍以及TreeView 控件树形结构不能显示的问题
    一个javascript脚本写的时钟
    使用DataGrid控件实现主细表
    解决 SQL Server 耗尽内存的情况
  • 原文地址:https://www.cnblogs.com/sujulin/p/7771664.html
Copyright © 2011-2022 走看看