SpringMVC Learning (II) ------ four data submission

The first mention bulk ------

  1. Write a form submission data
<%@ 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. Data output controller assembly LoginAction.java submitted
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. Because of the way the data submitted are different post and get the Chinese can not automatically resolve, you need to add the character encoding filter in web.xml
    Here Insert Picture DescriptionHere Insert Picture Description
    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. operation result
    Here Insert Picture Description
    Here Insert Picture Description

The second object encapsulates ------

  1. Create a package pojo in the oracle directory and create a Student class in its next
    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. Create a new form, create a new method in the 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. operation resultHere Insert Picture DescriptionHere Insert Picture Description

The third application alias ------

1. Create a new form, create a new method in the 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. operation result
    Here Insert Picture Description
    Here Insert Picture Description

The fourth ------ use the request object

1. Create a new form, create a new method in the 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. operation result
    Here Insert Picture Description
    Here Insert Picture Description
Published 19 original articles · won praise 6 · views 1041

Guess you like

Origin blog.csdn.net/weixin_43288999/article/details/104655437