【springMVC】简单的前后端数据交流

时间:2023-03-09 18:52:21
【springMVC】简单的前后端数据交流

最最常见两种,一则返回视图模板(文档),二则为json数据。就使用一个源代码文件来看看springmvc是怎么做到的。

1、UserController.java源代码文件

(这里额外的使用了fastjson架包来将对象解析为json)

package com.zay;

import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; /**
* Created by zay on 2016/11/29.
*/ @Controller
@RequestMapping("/User/")
public class UserController { /*
* 前后端json数据交流
* */
@ResponseBody
@RequestMapping("CheckUsername.do")
public String checkUsername(HttpServletRequest request, HttpServletResponse response) throws IOException{
System.out.println(request.getParameter("username")); //获取username字段值
//这里可进行处理
//得到处理结果
AjaxModel ajax = new AjaxModel(); //自定义的 一个ajax数据模型,为了规范前后端数据交流。
ajax.setMsg("the password is wrong ");
return JSON.toJSONString(ajax);
}
/*
* 返回视图模板字符串,这里将自动补充先前spring配置的视图前后缀以确定最终的视图页面。
* */
@RequestMapping("register.do")
public String register(){
return "register";
}
}

2、其他文件的配置

springmvc-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射-->
<mvc:annotation-driven />
<!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean -->
<context:component-scan base-package="com.zay" />
<!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp" />
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<!-- load-on-startup元素标记容器是否在启动的时候就加载这个servlet(
值必须是一个整数,表示servlet应该被载入的顺序 -->
</servlet> <servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

register.jsp

<%--
Created by IntelliJ IDEA.
User: zay
Date: 2016/11/29
Time: 15:56
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注册页面</title>
<script src="js/jquery-3.1.1.min.js"></script>
<script>
$(function(){
$("#user").blur(function(){
$.ajax({
url:'/springmvc/User/CheckUsername.do',
data:{
username:$("#user").val()
},
dataType:"json",
type:"POST",
success:function(data,status){
alert(data.msg);
}
});
});
});
</script>
</head>
<body>
<p>给自己找个登录名:</p>
<input id="user" type="text" name="user"/>
</body>
</html>

3、架包的引用,除了基本的spring架包外,还使用了fastjson架包。

【springMVC】简单的前后端数据交流