spring MVC 使用对象来接收表单提交的参数


介绍


本篇文章是关于利用对象来接收表单提交的参数.

这样可以避免代码过于臃肿,在项目中是很普遍使用的一种方法.

在本篇文章中,我将根据依据spring MVC的运作方式来一步步的给搞出来.

 

spring MVC框架思路


spring MVC的详见

http://www.cnblogs.com/dssjustdoit/articles/9387847.html

http://www.cnblogs.com/dssjustdoit/articles/9389087.html

http://www.cnblogs.com/dssjustdoit/articles/9390395.html

http://www.cnblogs.com/dssjustdoit/articles/9392477.html

http://www.cnblogs.com/dssjustdoit/articles/9395355.html

http://www.cnblogs.com/dssjustdoit/articles/9396743.html

 

 

目录


 

注:是在大模块中建的小模块(感觉有点废话了)

 

执行流程


 

 STEP 1 导入相关dependency和指定jdk版本(pom.xml)

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.dss</groupId>
  <artifactId>learnspringmvc1</artifactId>
  <version>1.0-SNAPSHOT</version>
  <modules>
      <module>06form</module>
  </modules>
  <packaging>pom</packaging>

  <name>learnspringmvc1 Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.0.4.RELEASE</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>learnspringmvc1</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.7.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.20.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

 STEP 2在浏览器中访问到jsp的表单提交页面(addStudent.jsp)

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <form method="post" action="/student/add.do">
        姓名:<input name="name">
        <br>
        年龄:<input name="age">
        <br>
        <input type="submit" value="提交">
    </form>
</body>
</html>

 注:这个当然也是通过tomcat来访问到这个表单页面的(但用不着spring MVC框架的咚咚),如下图

 注:那么当我点击提交按钮后,就出触发action,于是就产生了/student/add.do 这个url,于是乎,浏览器就会回应这个请求,访问tomcat.

STEP 3 访问tomcat(查询*.do的url配置)(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_3_1.xsd"
         version="3.1">

  <!--字符编码过滤器-->
  <!--最好配置在其他过滤器之前,因为过滤器的执行顺序是根据web.xml中的顺序一致-->
  <filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

    <!--手动指定字符编码-->
    <init-param>
      <!--因为当前filter中的成员变量叫做encoding,所以此处必须也写成encoding-->
      <param-name>encoding</param-name>
      <param-value>utf-8</param-value>
    </init-param>

    <!--强制指定字符编码,即使在request或者response中设置了字符编码,那么也会为其强制使用当前设置的字符编码-->
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!-- 注册spring MVC中央控制器 -->
  <servlet>
    <servlet-name>springMVC</servlet-name>
    <!-- spring MVC中的核心控制器 -->
    <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>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

</web-app>

  注:那么我在web.xml中配置了*.do的配置,就将url传给了DispatcherServlet中央调控器.中央调控器会在<init-param>中找到初始文件,即springmvc.xml.

 STEP 4 将url传给springmvc.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:p="http://www.springframework.org/schema/p"
       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">

    <!--注解驱动-->
    <mvc:annotation-driven/>

    <!-- 注册组件扫描器 -->
    <context:component-scan base-package="com.dss.*"/>

    <!-- 视图解析器 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

  注:我在springmvc.xml配置文件中配置了注册组件扫描器和,我就可以在com.dss包下面扫描所有的注释,找到controller

 STEP 5 找到controller(StudentController.java)

package com.dss.controller;

import com.dss.bean.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/student")
public class StudentController {

    @RequestMapping("/add.do")
    public ModelAndView addStudent(Student student) throws Exception{

        ModelAndView mv = new ModelAndView();
        mv.addObject("name",student.getName());
        mv.addObject("age",student.getAge());
        mv.setViewName("studentInfo");

        return mv;
    }
}

 注:找到Controller注释, RequestMapping注释意思是只要是以"/student"开头且以"/add.do"为结尾的url命令,都会交给这个controller来处理.

同时,我也在addStudent方法中传入了Student这个参数,那么controller就会把接收到的name和age传给student.getName()和student.getAge(),在接下来,将信息传给studentInfo.jsp文件

最后返回ModelAndView给中央控制器.

STEP 6 Student.java

package com.dss.bean;

public class Student {

    private String name;

    private int age;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

 注:这个Student.java和提交表单的jsp是没有直接关系的,只是在controller中把值传给了它,这也就是本篇文章所做的主要工作,使用对象来接收表单提交的参数!!

STEP 7 studentInfo.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
姓名:${name}
<br>
年龄:${age}
</body>
</html>

 

 注:在studentInfo.jsp文件接收传来的值.然后随ModelAndView返回给DispatcherServlet,再由DispatcherServlet将之相应给浏览器.

 

 

结论


 

这样,通过对象来接收表单提交的参数,当参数过多时,就可以避免在controller类中写一大堆的咚咚,避免代码过于臃肿.

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM