SpringMVC的Form表单的使用

预期的效果:填写一个表单,提交给Controller处理,返回结果界面。

效果的展示:表单的样式


结果样式:



程序的思路:填写一个表单,产生一个action,根据Action找到对应的@RequestMapping进行处理,我们用@ModelAttribute将传递过来的表单封装到User类中,然后将类传到model中,返回到结果页面,结果页面通过@ModelAttribute得到类数据,进行显示输出。



下面给出源代码:

类User:我们对类进行了格式化管理。



表单界面源代码:

<%--
  Created by IntelliJ IDEA.
  User: ly
  Date: 2018/3/13
  Time: 19:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>测试AnnotationFormatterFactory 接口</title>
</head>
<body>
<h3>测试表单数据格式化</h3>
<form action="getForm" method="post">
    <table>
        <tr>
            <td><label>日期类型: </label></td>
            <td><input type="text" id="birthday" name="birthday"></td>
        </tr>
        <tr>
            <td><label>整数类型: </label></td>
            <td><input type="text" id="total" name="total"></td>
        </tr>
        <tr>
            <td><label>百分数类型: </label></td>
            <td><input type="text" id="discount" name="discount"></td>
        </tr>

        <tr>
            <td><input id="submit" type="submit" value="提交"></td>
        </tr>
    </table>
</form>
</body>
</html>

Controller源代码:

 
package org.spring.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.spring.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class FormatterController
{

    private static final Log logger = LogFactory.getLog(FormatterController.class);

    @RequestMapping(value = "/{formName}")
    public String loginForm(@PathVariable String formName)
    {
        // 动态跳转页面
        return formName;
    }

    @RequestMapping(value = "/getForm", method = RequestMethod.POST)
    public String test(
            @ModelAttribute User user,
            Model model)
    {
        logger.info(user);
        model.addAttribute("user", user);
        return "success";
    }
}

<%--
  Created by IntelliJ IDEA.
  User: ly
  Date: 2018/3/13
  Time: 19:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>测试AnnotationFormatterFactory</title>
</head>
<body>
<h3>测试表单数据格式化</h3>
<%--
    modelAttribute 获取绑定的model属性
--%>
<form:form modelAttribute="user" method="post" action="">
    <table>
        <tr>
            <td>日期类型:</td>
            <td><form:input path="birthday"/></td>
        </tr>
        <tr>
            <td>整数类型:</td>
            <td><form:input path="total"/></td>
        </tr>
        <tr>
            <td>百分数类型:</td>
            <td><form:input path="discount"/></td>
        </tr>

    </table>
</form:form>
</body>
</html>


附:Maven的配置以及web.xml配置

web.xml:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

 <!-- <display-name>Archetype Created Web Application</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>-->


    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
   http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
             id="WebApp_ID" version="3.1">
        <!-- 定义Spring MVC的前端控制器 -->
        <servlet>
            <servlet-name>springmvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/applicationContext.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <!-- 让Spring MVC的前端控制器拦截所有请求 -->
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>

    <!--过滤设置-->
    <filter>
        <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
        <url-pattern>/</url-pattern>
    </filter-mapping>
    </web-app>

pox.xml:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.nk</groupId>
  <artifactId>sping</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>sping Maven Webapp</name>
  <url>http://maven.apache.org</url>
    <!-- spring版本号 -->
    <properties>
        <spring.version>4.2.5.RELEASE</spring.version>
    </properties>
    <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

      <!-- 添加spring核心依赖 -->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
          <version>4.2.5.RELEASE</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>4.2.5.RELEASE</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-oxm</artifactId>
          <version>4.2.5.RELEASE</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-tx</artifactId>
          <version>4.2.5.RELEASE</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-jdbc</artifactId>
          <version>4.2.5.RELEASE</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>4.2.5.RELEASE</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>4.2.5.RELEASE</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context-support</artifactId>
          <version>4.2.5.RELEASE</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-aop</artifactId>
          <version>4.2.5.RELEASE</version>
      </dependency>

      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-test</artifactId>
          <version>4.2.5.RELEASE</version>
      </dependency>


        <!-- 映入JSON -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.1.26</version>
        </dependency>

        <!-- servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.37</version>
        </dependency>

    </dependencies>

  <build>
    <finalName>sping</finalName>
      <plugins>
          <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <configuration>
                  <source>1.8</source>
                  <target>1.8</target>
              </configuration>
          </plugin>
      </plugins>
  </build>
</project>


  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringMVC中,表单的处理是非常常见的操作。而SpringMVC提供了form:标签库来简化表单的处理,使得表单的处理更加方便和简单。 使用form:标签库,需要在JSP页面中引入form标签库的命名空间,如下所示: ``` <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> ``` 引入命名空间后,就可以使用form标签库提供的标签来处理表单了。比如,使用form:form标签创建一个表单,如下所示: ``` <form:form modelAttribute="user" method="post" action="/user/save"> <table> <tr> <td><form:label path="username">Username:</form:label></td> <td><form:input path="username" /></td> </tr> <tr> <td><form:label path="password">Password:</form:label></td> <td><form:password path="password" /></td> </tr> <tr> <td colspan="2"><input type="submit" value="Submit" /></td> </tr> </table> </form:form> ``` 在上面的代码中,form:form标签用来创建一个表单,其中modelAttribute属性用来指定表单数据绑定到的对象,method属性用来指定表单提交时使用的HTTP方法,action属性用来指定表单提交的URL。form:label标签用来创建一个标签,path属性用来指定标签绑定到的对象属性,form:input标签用来创建一个文本框,path属性用来指定文本框绑定到的对象属性,form:password标签用来创建一个密码框,path属性用来指定密码框绑定到的对象属性。 使用form:标签库可以非常方便地处理表单,提高开发效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值