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