接受与数据显示

前端接受数据,并把数据显示到要转发到的页面。

两个配置文件: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_4_0.xsd"
         version="4.0">
    
    <!--注册DispatcherServlet 本质就是一个servlet-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <!--加载springmvc-servlet.xml文件-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>

        <!--配置web.xml文件的启动级别-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
</web-app>

springmvc-servlet.xml

<?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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.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.xsd">

    <!--扫描特定包下的注解-->
    <context:component-scan base-package="com.zkw.restful"/>
    <!--过滤静态资源-->
    <mvc:default-servlet-handler/>
    <!--mvc注解驱动-->
    <mvc:annotation-driven/>

    <!--视图解析器-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <!--前缀-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!--后缀-->
        <property name="suffix" value=".jsp"/>
	</bean>
</beans>

先来一个pojo实体类User,这个主要是为了演示从前端接受回来对象

package com.zkw.pojo;

import lombok.*;

@Setter @Getter @AllArgsConstructor @NoArgsConstructor @ToString
public class User {
    
    
    private int id ;
    private String name;
    private int age;
    private String sex;
}

然后来一个Controller

@RequestParam("")这个注解和mybatis中的@Param注解差不多。

@RequestParam(“username”) String name 相当于把name起了一个别名叫做username,在前端请求用username

package com.zkw.restful;

import com.zkw.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/user")
public class UserController {
    
    

    @GetMapping("/t1")
    public String test1(@RequestParam("username") String name, Model model){
    
    
        // 1. 接受前端的数据
        System.out.println("前端接受到的数据为:"+name);
        //2. 把接受的数据 返回给前端
        model.addAttribute("msg",name);
        return "hello";
    }

    //传递一个对象
    //http://localhost:8080/user/t2?id=1&name=Tom&age=3&sex=%E7%94%B7
    @GetMapping("/t2")
    public String test2(User user,Model model){
    
    
        System.out.println(user);
        model.addAttribute("msg",user);
        return "hello";
    }

    @GetMapping("/t3")
    public String test3(@RequestParam("username") String name,ModelMap map){
    
    
        map.addAttribute("msg",name);
        System.out.println("前端的数据为:"+name);
        return "hello";
    }
}

最后来一个jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${msg}
</body>
</html>

结果为:

1、请求http://localhost:8080/user/t1?username=Tom结果为

在这里插入图片描述

2、请求http://localhost:8080/user/t2?id=1&name=Tom&age=3&sex=男 结果为

在这里插入图片描述

如果你的字段名写的和实体类不一致那么这个字段上的数据为null

在这里插入图片描述

3、请求http://localhost:8080/user/t3?username=Dog结果为

在这里插入图片描述

Guess you like

Origin blog.csdn.net/fgets__/article/details/121081989