jsp前后端传递数据的几种方法

package com.zg.sboottest.controller;

import com.zg.sboottest.entities.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @Auther: Zg
 * @Date: 2022/9/7 - 09 - 07 - 13:11
 * @Description: com.zg.sboottest.controller
 * @version: 1.0
 */
@Controller
public class MyController {

    @RequestMapping("/test")
    public String Index(){
        return "Index";
    }

    @RequestMapping("/table")
    public String Table(HttpServletResponse resp, HttpServletRequest req){
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String realname = req.getParameter("realname");
        System.out.println("================"+username+"================"+password+"================"+realname);
        return "redirect:/Success.jsp";
    }

    @RequestMapping("/param")
    public String Param(Student student){
        System.out.println(student.toString());
        return "redirect:/Success.jsp";
    }

    @RequestMapping("/rest/{username}/{id}")
    public String TestResful(@PathVariable String username, @PathVariable String id){
        System.out.println("username="+username);
        System.out.println("id="+id);
        return "redirect:/Success.jsp";
    }

    @RequestMapping("/test/mav")
    public ModelAndView TestModelAndView(){
        /**
         * ModelAndView包含Model和View的功能
         * Model:向请求域中共享数据
         * View:设置逻辑视图实现页面跳转
         */
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("testModelAndView","Hello, ModelAndView");
        modelAndView.setViewName("Success");
        return modelAndView;
    }

    @RequestMapping("test/model")
    public String TestModel(Model model){
        model.addAttribute("testModel","hello,Model");
        return "Success";
    }

    @RequestMapping("test/modelMap")
    public String TestModelMap(ModelMap modelMap){
        System.out.println(modelMap.getClass().getName());
        modelMap.addAttribute("TestModelMap","hello,ModelMap");
        return "Success";
    }

    @RequestMapping("/test/map")
    public String TestMap(Map<String,Object> map){
        System.out.println(map.getClass().getName());
        map.put("TestMap","hello,TestMap!");
        return "Success";
    }

    @RequestMapping("/test/session")
    public String TestSession(HttpSession session){
        session.setAttribute("TestSession","hello,TestHttpSession");
        return "Success";
        //return "redirect:/Success.jsp";
    }

    @RequestMapping("/test/application")
    public String testApplication(HttpSession session){
        ServletContext servletContext = session.getServletContext();
        servletContext.setAttribute("testApplicationScope", "hello,application");
    return "Success";
    }
}

package com.zg.sboottest.entities;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @Auther: Zg
 * @Date: 2022/9/7 - 09 - 07 - 11:58
 * @Description: com.zg.sboottest.entities
 * @version: 1.0
 */

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@Slf4j
public class Student {
    private Integer id;
    private String userName;
    private String realName;
    private String password;
    private String age;
}

<%--
  Created by IntelliJ IDEA.
  User: zg
  Date: 2022/9/7
  Time: 13:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h1>Index!!!</h1>
    <br/>
    <h2>1,测试向request域提供数据:</h2>
    <form action="${pageContext.request.contextPath}/table" method="post">
        <table cellpadding="0" cellspacing="0" border="0"
               class="form_table">
            <tr>
                <td valign="middle" align="right">
                    用户名:
                </td>
                <td valign="middle" align="left">
                    <input type="text" class="inputgri" name="username" />
                </td>
            </tr>
            <tr>
                <td valign="middle" align="right">
                    真实姓名:
                </td>
                <td valign="middle" align="left">
                    <input type="text" class="inputgri" name="realname" />
                </td>
            </tr>
            <tr>
                <td valign="middle" align="right">
                    密码:
                </td>
                <td valign="middle" align="left">
                    <input type="password" class="inputgri" name="password" />
                </td>
            </tr>
            <tr>
                <td valign="middle" align="right">
                    性别:
                </td>
                <td valign="middle" align="left">
                    nan
                    <input type="radio" class="inputgri" name="sex" value="nan" checked="checked"/>
                    nv
                    <input type="radio" class="inputgri" name="sex" value="nv"/>
                </td>
            </tr>


        </table>
        <p>
            <input type="submit" class="button" value="Submit &raquo;" />
        </p>
    </form>
    <br/>

    <form action="${pageContext.request.contextPath}/param" method="post">
        真实姓名:<input type="text" name="realName"><br>
        年龄:<input type="text" name="age"><br>
        用户名:<input type="text" name="userName"><br>
        密码:<input type="password" name="password"><br>
        <input type="submit" value="登录"><br>
    </form>
    <br/>


    <a href="${pageContext.request.contextPath}/rest/admin/1">测试Resful地址中获得参数</a><br/>

    <a href="${pageContext.request.contextPath}/test/mav">测试通过ModelAndView向请求域共享数据</a><br>
    <a href="${pageContext.request.contextPath}/test/model">测试通过Model向请求域共享数据</a><br>
    <a href="${pageContext.request.contextPath}/test/modelMap">测试通过ModelMap向请求域共享数据</a><br>
    <a href="${pageContext.request.contextPath}/test/map">测试通过map向请求域共享数据</a><br>
    <br/>
    <h2>2,测试向session域提供数据:</h2>
    <a href="${pageContext.request.contextPath}/test/session">测试向会话域共享数据</a><br>
    <br/>
    <h2>3,测试向applcation域提供数据:</h2>
    <a href="${pageContext.request.contextPath}/test/application">测试向应用域共享数据</a><br>
</body>
</html>

<%--
  Created by IntelliJ IDEA.
  User: zg
  Date: 2022/9/7
  Time: 13:22
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h1>Success!!!++++</h1>
    <p>TTTTTTT</p>
    <p>${testModelAndView}</p>
    <p>${testModel}</p>
    <p>${TestModelMap}</p>
    <p>${TestMap}</p>
    <p>${sessionScope.TestSession}</p>
    <p >${applicationScope.testApplicationScope}</p>
</body>
</html>

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.zg</groupId>
    <artifactId>sboottest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>sboottest</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--jsp依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

        <!--mybatis依赖-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.7</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>

        <!--数据源依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.19</version>
        </dependency>

        <!--mysql依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.26</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.8</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

        <!--javaee规范-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>

        <!--引入jstl-->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
        </dependency>


    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.zg.sboottest.SboottestApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

## 应用名称
#spring.application.name=sboottest
## 应用服务 WEB 访问端口
#server.port=8080


# 应用名称
# 应用服务 WEB 访问端口
server:
  port: 8080
  servlet:
    context-path: /zg #项目名称 8080后加的
    jsp:
      init-parameters:
        development: true #热部署


spring:
  mvc:
    view:
      prefix: / #/ems/
      suffix: .jsp
  application:
    name: ems #微服务用
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/dormitory?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
    username: root
    password: 123456



mybatis:
  configuration:
    map-underscore-to-camel-case: true
  mapper-locations: classpath:com/zg/sboottest/mapper/*.xml #mapper配置文件的位置
  type-aliases-package: com.zg.sboottest.entities



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潘诺西亚的火山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值