SpringMVC中接收数据和返回数据

SpringMVC接收数据,并输出到控制台,首先我们写一个User实体类

package com.zhiying.pojo;

public class User {
    private int id;
    private String name;

    public User() {
    }

    public User(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

然后是一个Controller控制器

package com.zhiying.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class UserController {
    @RequestMapping("/u1")
    public String test1(String name) {
        System.out.println("接收到前端的参数为" + name);
        return "test";
    }
}

然后是配置文件

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

<!--    自动扫描指定的包,该包下面的所有注解类交给IoC容器管理-->
    <context:component-scan base-package="com.zhiying.controller"/>

<!--    视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--        前缀匹配-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!--        后缀匹配-->
        <property name="suffix" value=".jsp"/>
    </bean>
<!--    <bean id="/hello" class="com.zhiying.controller.ControllerTest1"/>-->

</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_4_0.xsd"
         version="4.0">

<!--    配置DispatchServlet,其本质是一个Servlet-->
    <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:springmvc-servlet.xml</param-value>
        </init-param>

        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

最后是我们的jsp文件


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

这里我们可以看到,成功接收到了,但是如果我们在url中把name写成了username,他就会显示null

在这里我们可以加一个注解,指定一个名字,修改Controller类

package com.zhiying.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class UserController {
    @RequestMapping("/u1")
    //在前端接收的数据最好用上@RequestParam,证明这是前端接收的
    public String test1(@RequestParam("username") String name) {
        System.out.println("接收到前端的参数为" + name);
        return "test";
    }
}

如果接收到的是一个对象,我们需要修改Controller类

package com.zhiying.controller;

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

@Controller
public class UserController {
    @RequestMapping("/u1")
    //在前端接收的数据最好用上@RequestParam,证明这是前端接收的
    public String test1(@RequestParam("username") String name, Model model) {
        System.out.println("接收到前端的参数为" + name);
        model.addAttribute("msg",name);//这就是把后台的数据返回到前端的最常有的方式
        return "test";
    }

    @RequestMapping("/u2")
    public String test2(User user) {
        System.out.println("接收到前端的对象为" + user.toString());
        return "test";
    }
}

返回数据的话,也就是我们经常使用的就是Model,这里用Controller演示,也就是能把后台的数据显示到前端,通过model.addAttribute("msg",name);可以实现

package com.zhiying.controller;

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

@Controller
public class UserController {
    @RequestMapping("/u1")
    //在前端接收的数据最好用上@RequestParam,证明这是前端接收的
    public String test1(@RequestParam("username") String name, Model model) {
        System.out.println("接收到前端的参数为" + name);
        model.addAttribute("msg",name);//这就是把后台的数据返回到前端的最常有的方式
        return "test";
    }

}

发布了376 篇原创文章 · 获赞 242 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/HeZhiYing_/article/details/103996261