@RequestMapping("/hello")
public String sayHello() {
System.out.println("Hello SpringMVC!!");
// 跳转到XX页面
return "success";
}
/**
* 测试向前台传数据
* 返回类型是字符串
*
* @param model
* @return
*/
@RequestMapping("/testString")
public String testString(Model model) {
System.out.println("testString,,,");
User user = new User();
user.setUsername("Nick阿斯顿");
user.setPassword("123456");
user.setAge(21);
model.addAttribute("user", user);
return "success";
}
/**
* 返回是void
* 测试向前台传数据
*
* @param model
* @return
*/
@RequestMapping("/testVoid")
public void testVoid(Model model, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
System.out.println("testVoid,,,");
//自己编写请求转发:一次请求
//request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request, response);
//自己编写重定向:两次请求
response.sendRedirect(request.getContextPath() + "/success.jsp");
//直接进行响应
//1.设置中文不乱吗
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
//2.响应内容
response.getWriter().print("你好
");
return;
}
/**
* 返回类型是ModelAndView对象
*
* @param
* @return
*/
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView() {
System.out.println("testString,,,");
//1.创建ModelAndView对象
ModelAndView mv = new ModelAndView();
User user = new User();
user.setUsername("Nick左眼会陪右眼哭");
user.setPassword("123456");
user.setAge(21);
mv.addObject("user", user);
//跳转到哪个页面(会经过视图解析器)
mv.setViewName("success");
return mv;
}
/**
* 使用关键字的方式转发或者重定向
*
* @param model
* @return
*/
@RequestMapping("/testForward")
public String testForward(Model model) {
System.out.println("testForward,,,");
User user = new User();
user.setUsername("Nick阿斯顿");
user.setPassword("123456");
user.setAge(21);
model.addAttribute("user", user);
//请求转发
//return "forward:/WEB-INF/pages/success.jsp";
return "forward:/success.jsp";
//重定向(请求不到/WEB-INF/pages的资源,而且拿不到request域的数据)
//return "redirect:/success.jsp";
}
DispatcherServlet会拦截到所有的资源,导致一个问题就是静态资源(img、css、js)也会被拦截到,从而不能被使用。解决问题就是需要配置静态资源不进行拦截,在springmvc.xml配置文件添加如下配置
mvc:resources标签配置不过滤
1. location元素表示webapp目录下的包下的所有文件
2. mapping元素表示以/static开头的所有请求路径,如/static/a 或者/static/a/b
<mvc:resources location="/lib/" mapping="/lib/**"/>
<mvc:resources location="/css/" mapping="/css/**"/>
<mvc:resources location="/images/" mapping="/images/**"/>
<mvc:resources location="/js/" mapping="/js/**"/>
* 获取请求体的数据
* @param body
*/
@RequestMapping("/testJson")
public void testJson(@RequestBody String body) {
System.out.println(body);
}
/**
* 获取请求体的数据
* @param body
*/
@RequestMapping("/testJson")
public void testJson(@RequestBody User user) {
System.out.println(user);
}
/**
* 模拟异步请求和响应
*
* @param
* @return
*/
@RequestMapping("/testJson")
public @ResponseBody
User testJson(@RequestBody User user) { //@ResponseBody会将javaBean对象转换为json响应
System.out.println("testJson,,,");
//客户端发送ajax请求,传的是json字符串,spring已经把json封装到User对象中
System.out.println("接收到的内容:" + user);
//做响应,模拟查询数据库
user.setUsername("你好呀");
user.setAge(99);
return user;
}
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
package cn.kt.controller;/*
*Created by tao on 2020-04-09.
*/
import cn.kt.domian.User;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.security.sasl.SaslException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Controller
@RequestMapping("/user")
public class UserController {
/**
* 测试向前台传数据
* 返回类型是字符串
*
* @param model
* @return
*/
@RequestMapping("/testString")
public String testString(Model model) {
System.out.println("testString,,,");
User user = new User();
user.setUsername("Nick阿斯顿");
user.setPassword("123456");
user.setAge(21);
model.addAttribute("user", user);
return "success";
}
/**
* 返回是void
* 测试向前台传数据
*
* @param model
* @return
*/
@RequestMapping("/testVoid")
public void testVoid(Model model, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
System.out.println("testVoid,,,");
//自己编写请求转发:一次请求
//request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request, response);
//自己编写重定向:两次请求
response.sendRedirect(request.getContextPath() + "/success.jsp");
//直接进行响应
//1.设置中文不乱吗
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
//2.响应内容
response.getWriter().print("你好
");
return;
}
/**
* 返回类型是ModelAndView对象
*
* @param
* @return
*/
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView() {
System.out.println("testString,,,");
//1.创建ModelAndView对象
ModelAndView mv = new ModelAndView();
User user = new User();
user.setUsername("Nick左眼会陪右眼哭");
user.setPassword("123456");
user.setAge(21);
mv.addObject("user", user);
//跳转到哪个页面(会经过视图解析器)
mv.setViewName("success");
return mv;
}
/**
* 使用关键字的方式转发或者重定向
*
* @param model
* @return
*/
@RequestMapping("/testForward")
public String testForward(Model model) {
System.out.println("testForward,,,");
User user = new User();
user.setUsername("Nick阿斯顿");
user.setPassword("123456");
user.setAge(21);
model.addAttribute("user", user);
//请求转发
//return "forward:/WEB-INF/pages/success.jsp";
return "forward:/success.jsp";
//重定向(请求不到/WEB-INF/pages的资源,而且拿不到request域的数据)
//return "redirect:/success.jsp";
}
/**
* 模拟异步请求和响应
*
* @param
* @return
*/
@RequestMapping("/testJson")
public @ResponseBody
User testJson(@RequestBody User user) { //@ResponseBody会将javaBean对象转换为json响应
System.out.println("testJson,,,");
//客户端发送ajax请求,传的是json字符串,spring已经把json封装到User对象中
System.out.println("接收到的内容:" + user);
//做响应,模拟查询数据库
user.setUsername("你好呀");
user.setAge(99);
return user;
}
}
<%--
Created by IntelliJ IDEA.
User: tao
Date: 2020-04-09
Time: 23:23
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Titletitle>
head>
<script src="lib/jquery/jquery.min.js">script>
<script>
$(function () {
$("#btn").click(function () {
$.ajax({
//编写json的属性和值
url: "user/testJson",
contentType: "application/json;charset=UTF-8",
data: '{"username":"阿斯顿","password":654321,"age":55}',
dataType: "json",
type: "post",
success: function (data) {
alert(data);
alert(data.username);
}
})
})
})
script>
<body>
<h1>欢迎进入首页h1>
<a href="/user/testString">测试testStringa><br>
<a href="/user/testVoid">测试testVoida><br>
<a href="/user/testModelAndView">测试testModelAndViewa><br>
<a href="/user/testForward">测试testForwarda><br>
<button id="btn">发送Ajax的异步请求button>
body>
html>