SpringMVC学习(二)------四种数据提交方式

第一种------散提

  1. 编写一个表单提交数据
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  <!--<a href="/admin/login.action">访问action跳到controller</a>-->
  <!--第一种数据提交方式:单个数据,散提-->
  <form action="${pageContext.request.contextPath}/admin/login.action" method="post">
    <p>姓名:<input name="stuname"> </p>
    <p>密码:<input type="password" name="stupwd"></p>
    <p>年龄:<input name="stuage"></p>
    <p><input type="submit" value="提交"><input type="reset" value="重置"></p>
  </form>
  </body>
</html>

  1. 在控制器组件LoginAction.java中输出提交的数据
package com.oracle.controller;

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

@Controller
@RequestMapping("/admin")
public class LoginAction {
    //所有方法必须注解,接受第一种数据提交方式:散提
    @RequestMapping("/login")
    public String mylogin(String stuname,String stupwd,int stuage){
        System.out.println("----------"+stuname+"-------"+stupwd+"-------"+stuage);
        return "main";
    }

}

  1. 由于数据提交的方式是post与get不同无法将中文自动解析,需要在web.xml中添加字符编码过滤器
    在这里插入图片描述在这里插入图片描述
    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">
    <!--字符编码过滤器,配置在所有内容之前-->
    <filter>
        <filter-name>encode</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encode</filter-name>
        <url-pattern>*.action</url-pattern>
    </filter-mapping>
    <!--注册Springmvc框架-->
    <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.xml</param-value>
        </init-param>
    </servlet>
      <servlet-mapping>
          <servlet-name>springmvc</servlet-name>
          <url-pattern>*.action</url-pattern>
      </servlet-mapping>
</web-app>
  1. 运行结果
    在这里插入图片描述
    在这里插入图片描述

第二种------对象封装

  1. 在oracle目录下创建一个包pojo并在其下创建一个Student类
    Student.java
package com.oracle.pojo;

public class Student {
    private String stuname;
    private String stupwd;
    private int stuage;

    public Student(String stuname, String stupwd, int stuage) {
        this.stuname = stuname;
        this.stupwd = stupwd;
        this.stuage = stuage;
    }

    public Student() {
    }

    public String getStuname() {
        return stuname;
    }

    public void setStuname(String stuname) {
        this.stuname = stuname;
    }

    public String getStupwd() {
        return stupwd;
    }

    public void setStupwd(String stupwd) {
        this.stupwd = stupwd;
    }

    public int getStuage() {
        return stuage;
    }

    public void setStuage(int stuage) {
        this.stuage = stuage;
    }

    @Override
    public String toString() {
        return "Student{" +
                "stuname='" + stuname + '\'' +
                ", stupwd='" + stupwd + '\'' +
                ", stuage=" + stuage +
                '}';
    }
}

  1. 创建一个新的表单,在LoginAction中创建一个新的方法
 <!--第二种数据提交方式:提交的数据自动封装到对象中-->
  <form action="${pageContext.request.contextPath}/admin/two.action" method="post">
    <p>姓名:<input name="stuname"> </p>
    <p>密码:<input type="password" name="stupwd"></p>
    <p>年龄:<input name="stuage"></p>
    <p><input type="submit" value="提交"><input type="reset" value="重置"></p>
</form>

    //第二种数据提交方式:自动封装在对象中
    @RequestMapping("/two")
    public String two(Student stu){
        System.out.println("--------"+stu.getStuname()+"--------"+stu.getStupwd()+"-------"+(stu.getStuage()));
        return "main";
    }
  1. 运行结果在这里插入图片描述在这里插入图片描述

第三种------别名应用

1.创建一个新的表单,在LoginAction中创建一个新的方法

<!--第三种数据提交方式:别名应用-->
<form action="${pageContext.request.contextPath}/admin/three.action" method="post">
  <p>姓名:<input name="stuname"> </p>
  <p>密码:<input type="password" name="stupwd"></p>
  <p>年龄:<input name="stuage"></p>
  <p><input type="submit" value="提交"><input type="reset" value="重置"></p>
</form>
   //第三种数据提交的方式:提交的变量名与方法参数的名称不一样,需要专门的注解@RequestParam
    @RequestMapping("/three")
    public String three(
            @RequestParam(value="stuname")
                    String name,
            @RequestParam(value = "stupwd")
                    String pwd,
            @RequestParam(value = "stuage")
                     int age

    ){
        System.out.println("-----"+name+"----------"+pwd+"--------"+age);
        return "main";
    }
  1. 运行结果
    在这里插入图片描述
    在这里插入图片描述

第四种------使用request对象

1.创建一个新的表单,在LoginAction中创建一个新的方法

 <!--第四种数据提交方式:request-->
  <form action="${pageContext.request.contextPath}/admin/four.action" method="post">
    <p>姓名:<input name="stuname"> </p>
    <p>密码:<input type="password" name="stupwd"></p>
    <p>年龄:<input name="stuage"></p>
    <p><input type="submit" value="提交"><input type="reset" value="重置"></p>
  </form>
 //第四种数据提交方式:使用HttpServletRequest对象提取数据
    @RequestMapping("/four")
    public String four(HttpServletRequest request){
        String name=request.getParameter("stuname");
        String pwd=request.getParameter("stupwd");
        int age=Integer.parseInt(request.getParameter("stuage"));
        System.out.println("--------"+name+"--------"+pwd+"-------"+(age));
        return "main";
    }
  1. 运行结果
    在这里插入图片描述
    在这里插入图片描述
发布了19 篇原创文章 · 获赞 6 · 访问量 1041

猜你喜欢

转载自blog.csdn.net/weixin_43288999/article/details/104655437