返回数值类型和字符串类型
需要导入依赖
当引入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>
//控制器@Controllerpublic 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>
时间: 2024-10-06 15:23:49