SpringMVC学习(二)

本文详细介绍了SpringMVC处理器方法的返回值类型,包括String、Object、void、基本数据类型和ModelAndView,并给出了ajax请求返回学生集合的示例。此外,文章还讲解了SpringMVC的四种跳转方式——请求转发和重定向,以及redirect和forward的使用。同时,讨论了日期处理、SpringMVC的默认参数类型、日期显示处理以及标签的作用。最后,提到了资源在WEB-INF目录下的安全性问题和配置方法。
摘要由CSDN通过智能技术生成

 处理器(action)方法的返回值

1.String:客户端资源的地址,自动拼接前后缀,还可以屏蔽自动拼接字符串,可以返回指定的路径

2.Object:返回json格式的对象,自动将对象或集合转为json。使用的jackson工具进行转换,必须添加jackson依赖,一般用于ajax请求

3.void:无返回值,一般用于ajax请求

4.基本数据类型:用于ajax请求

5.ModelAndView:返回数据和视图对象,现在用的很少

例:完成ajax请求访问服务器,返回学生集合

1.添加jackson依赖

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>SpringMVC_004</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>SpringMVC_004 Maven Webapp</name>


  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>5.2.5.RELEASE</version>
      </dependency>
      <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>3.1.0</version>
      </dependency>
<!--      添加jackson依赖,用于ajax请求-->
      <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-databind</artifactId>
          <version>2.9.8</version>
      </dependency>
  </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

2.在webapp目录新建js目录,添加jQuery函数库

jQuery函数库在网上下载后自己复制进js目录即可

3.在index.jsp页面上导入函数库

<%--
  Created by IntelliJ IDEA.
  User: HP
  Date: 2022-06-27
  Time: 10:15
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
<%--    导入jQuery函数库--%>
    <script src="js/jquery-3.6.0.js"></script>
</head>
<body>
<br>
<br>
<br>
<a href="javascript:showStu()">访问服务器返回学生集合</a><br>
<div id="mydiv">等待服务器返回数据</div>
<script type="text/javascript">
    function showStu(){
    //    使用jQuery封装的ajax()方法发送请求
        $.ajax({
            url:"${pageContext.request.contextPath}/list.action",
            type:"get",
            dataType:"json",
            success:function (stuList){
                var s ="";
                $.each(stuList,function (i,stu){
                    s+=stu.name+"----"+stu.age+"<br>";
                });
            //    回显数据
                $("#mydiv").html(s);
            }

        });
    }
</script>
</body>
</html>

4.在action上添加注解@ResponseBody,用来处理ajax请求

package com.ys.controller;

import com.ys.pojo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class AjaxAction {
    @RequestMapping("/list")
    @ResponseBody//ajax支持
    public List<Student> list(){
        List<Student> list =new ArrayList<>();
        Student student1=new Student("张三",22);
        Student student2=new Student("李四",24);
        Student student3=new Student("王五",26);
        list.add(student1);
      
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值