码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC中的返回值问题之二返回数值类型和字符串类型

时间:2017-12-16 23:14:03      阅读:304      评论:0      收藏:0      [点我收藏+]

标签:enc   ems   html   version   javascrip   query   https   bsp   type   

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

需要导入依赖

技术分享图片

 

当引入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>


 
 
 

 



 

 

 

SpringMVC中的返回值问题之二返回数值类型和字符串类型

标签:enc   ems   html   version   javascrip   query   https   bsp   type   

原文地址:http://www.cnblogs.com/sujulin/p/7771664.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!