SpringMVC的Form表单的使用

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

效果的展示:表单的样式

SpringMVC的Form表单的使用_第1张图片

结果样式:

SpringMVC的Form表单的使用_第2张图片


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



下面给出源代码:

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

SpringMVC的Form表单的使用_第3张图片


表单界面源代码:

<%--
  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" %>

html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


    http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </span>测试AnnotationFormatterFactory 接口<span style="color:#e8bf6a;">


测试表单数据格式化

action="getForm" method="post">
type="text" id="birthday" name="birthday">
type="text" id="total" name="total">
type="text" id="discount" name="discount">
id="submit" type="submit" value="提交">

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" %>

html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


    http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </span>测试AnnotationFormatterFactory<span style="color:#e8bf6a;">


测试表单数据格式化

<%-- modelAttribute 获取绑定的model属性 --%> <form:form modelAttribute="user" method="post" action="">
日期类型: <form:input path="birthday"/>
整数类型: <form:input path="total"/>
百分数类型: <form:input path="discount"/>
form:form>


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

web.xml:

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

 


    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">
        
        
            springmvc
            org.springframework.web.servlet.DispatcherServlet
            
                contextConfigLocation
                /WEB-INF/applicationContext.xml
            
            1
        
        
        
            springmvc
            /
        

    
    
        characterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
    
    
        characterEncodingFilter
        /
    
    

pox.xml:

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">
  4.0.0
  cn.nk
  sping
  war
  1.0-SNAPSHOT
  sping Maven Webapp
  http://maven.apache.org
    
    
        4.2.5.RELEASE
    
    
    
      junit
      junit
      3.8.1
      test
    

      
      
          org.springframework
          spring-core
          4.2.5.RELEASE
      
      
          org.springframework
          spring-web
          4.2.5.RELEASE
      
      
          org.springframework
          spring-oxm
          4.2.5.RELEASE
      
      
          org.springframework
          spring-tx
          4.2.5.RELEASE
      
      
          org.springframework
          spring-jdbc
          4.2.5.RELEASE
      
      
          org.springframework
          spring-webmvc
          4.2.5.RELEASE
      
      
          org.springframework
          spring-context
          4.2.5.RELEASE
      
      
          org.springframework
          spring-context-support
          4.2.5.RELEASE
      
      
          org.springframework
          spring-aop
          4.2.5.RELEASE
      

      
          org.springframework
          spring-test
          4.2.5.RELEASE
      


        
        
            com.alibaba
            fastjson
            1.1.26
        

        
        
            javax.servlet
            servlet-api
            2.5
            provided
        

        
            mysql
            mysql-connector-java
            5.1.37
        

    

  
    sping
      
          
              org.apache.maven.plugins
              maven-compiler-plugin
              
                  1.8
                  1.8
              
          
      
  



你可能感兴趣的:(SpringMVC)