我是靠谱客的博主 美满大神,最近开发中收集的这篇文章主要介绍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>


 
 
 

 



 

 

 

转载于:https://www.cnblogs.com/sujulin/p/7771664.html

最后

以上就是美满大神为你收集整理的SpringMVC中的返回值问题之二返回数值类型和字符串类型的全部内容,希望文章能够帮你解决SpringMVC中的返回值问题之二返回数值类型和字符串类型所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(39)

评论列表共有 0 条评论

立即
投稿
返回
顶部